Jump to content




Recent Status Updates

  • Photo
      30 Sep
    rhossis

    laptop hard disk seated beneath motherboard but with no access panel. 7 hours to replace :(

    Show comments (3)
  • Photo
      19 Sep
    Chall

    I love it when you go to write a help thread, then while writing, you reach an enlightenment, and figure it out yourself.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

move_uploaded_file(images/) [function.move-uploaded-file]: failed to open stream

stream

  • Please log in to reply
21 replies to this topic

#1 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 26 April 2011 - 01:16 PM

Hello,
I'm back with more problems.

I'm getting the errors:


Warning: move_uploaded_file(images/) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/billing/vhosts/yakimaimplement.com/used_test/add.php on line 23

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpmb59hD' to 'images/' in /home/billing/vhosts/yakimaimplement.com/used_test/add.php on line 23
Sorry, there was a problem uploading your file. Please try again.

With the following code, when it is executed:
<?php

//Directory where images will be stored - need to change?
$target = "images/";
$target = $target . basename( $_FILES['photo']['model']);

//Other information
$model=$_POST['model'];
$serial=$_POST['serial'];
$application=$_POST['application'];
$pic=($_FILES['photo']['model']);

// Connect to DB

mysql_connect("localhost", "", "") or die(mysql_error()) ;
mysql_select_db("yakimaimplement") or die(mysql_error()) ;

// Write information to DB

mysql_query("INSERT INTO `used_equip` VALUES ('$model', '$serial', '$application', '$pic')") ;

//Write the photo to server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if all worked
echo "The file ". basename( $_FILES['uploadedfile']['model']). " has been uploaded and your entry updated.";
}
else {

//Error if didn't work
echo "Sorry, there was a problem uploading your file.  Please try again.";
}
?>
It doesn't seem to be a permissions problem. Again, I'm pretty new to working with SQL in PHP, so hopefully it's something simple that I'm just missing.

Thanks in advance!

Edited by Alexander, 26 April 2011 - 02:37 PM.
(Removed credentials)

  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3,922 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 26 April 2011 - 02:40 PM

$target = $target . basename( $_FILES['photo']['model']);

The _FILES superglobal array contains only information about the file, not any fields. Did you mean to use $_POST['model']?

Your error happens due to the fact basename() is accessing a non-existent array key, and your target will be come images/ which is not a file path, but an directory path.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 26 April 2011 - 03:34 PM

Oh, ****. Okay, that makes more sense. God, slowly piecing this all together. Okay, so, that part worked and it's uploading the file. But, it's uploading it without the .jpg extension. I'm attempting to basically put the link, to where the photo is, in the 'photo' field in the database. That way, when it can be called from a IMG tag when I need to. Additionally, I didn't have an "or die" on the actual input of data and now it's saying:

Column count doesn't match value count at row 1

Which, I assume is because my table has an ID field that is auto_incremented. I assumed whenever I inputted new data, it would automatically just assign it to the next ID in order. Is that not how it works?
  • 0

#4 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 26 April 2011 - 03:48 PM

And, a simple web searched told me about that. Sorry. Thank you again!
Feel free to offer any further information, but I didn't realized that I still needed to put something in for id.
  • 0

#5 Alexander

Alexander

    YOL9

  • Moderator
  • 3,922 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 26 April 2011 - 04:01 PM

Did you solve your problems? With "auto increment" and "not null" attributes provided by MySQL on the ID column, you do not, and should not provide the ID column in your insert or update statements, and it should not be modified - only used in a (WHERE id = a number) context.

If you wish for your image to upload without stripping the name, simply remove the basename() function. This is often used to prevent people from uploading a .php file and have it run on the server, and is not required if you are 100% sure what you are uploading is an image and not a script.

You need to keep the text that is inside the basename script though, so it knows what the name is with the extension.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#6 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 26 April 2011 - 04:57 PM

Well, I realized that I was trying to do two things at once. So, I solved half the problems, but created more.

I did decide to simply store images in the database. So far so good. So, I think I actually solved all the upload/item input problems. However, I'm not having problems calling the data. I almost have my head wrapped around it, but in case you beat me to a response, here's the code:

<?php


$username = "";
$password = "";
$host = "localhost";
$database = "yakimaimplement";

mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

mysql_select_db($database) or die("Can not select the database: ".mysql_error());

$id = $_GET['id'];

if(!isset($id) || empty($id) || !is_int($id)){
     die("Please select your image!");
}else{

$query = mysql_query("SELECT * FROM used_equip WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['photo'];


//Outputs the image and other data
//Need to add a LOOP of some sort, to tile entries down page.
echo "<table width="100%">";
echo "<tr valign="top">";
echo "<td rowspan="3">";
echo "<img src=".$info['photo'] . ">";
echo "</td>";
echo "<td style="padding-left: 20px;">";
echo "<b><h4 class="nomargin">Model:</h4></b> ".$info['model'] . "<br> ";
echo "<b>Serial:</b> ".$info['serial'] . "<br> ";
echo "<b>Application:</b> ".$info['application'] . "<br> ";
echo "<br />";
echo "</p>";
echo "</td>";
echo "</tr>";
echo "</table>";
}
?>

Now, I'm generating some pretty dumb errors. And, I'm frankenstiened the code accidentally. Because, previously I had intended on just pulling the link from the database. It keeps giving me errors around line 26. Basically, the output is not good coding, is what I am assuming. I'm missing something in the middle. I feel like I heard that you can't do a SELECT * FROM call on a table that has a BLOB field. Is this my issue? Do I need to run two seperate calls to get the data? Thanks again!

Edited by Julien Duponte, 27 April 2011 - 07:20 AM.

  • 0

#7 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 26 April 2011 - 05:45 PM

And, corrected that problem...
god, I'm going downhill. :) Obviously, that was my lack of "\" next to the quotes.

Now, i'm getting "
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/billing/vhosts/yakimaimplement.com/used_test/display_test.php on line 23"

I know whats causing it, but haven't figured out the solution yet.
  • 0

#8 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 26 April 2011 - 05:59 PM

Got it working now. But the photos come up as dead links. I feel like since they are BLOB fields, they should be handled differently. Incorrect? Code up to date:

<?php


$username = "";
$password = "";
$host = "localhost";
$database = "yakimaimplement";

mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

mysql_select_db($database) or die("Can not select the database: ".mysql_error());

$data = mysql_query("SELECT * FROM used_equip")
or die(mysql_error());

//Outputs the image and other data
//Need to add a LOOP of some sort, to tile entries down page.

while($info = mysql_fetch_array( $data ))
{
Print "<table>";
Print "<tr valign=\"top\">";
Print "<td rowspan=\"3\">";
Print "<img src=".$info['photo'] \">";
Print "</td>";
Print "<td style=\"padding-left: 20px;\">";
Print "<b><h4 class=\"nomargin\">Model:</h4></b> ".$info['model'] . "<br> ";
Print "<b>Serial:</b> ".$info['serial'] . "<br> ";
Print "<b>Application:</b> ".$info['application'] . "<br> ";
Print "<br />";
Print "</p>";
Print "</td>";
Print "</tr>";
Print "</table>";
}
?>

Edited by Julien Duponte, 27 April 2011 - 07:21 AM.

  • 0

#9 Alexander

Alexander

    YOL9

  • Moderator
  • 3,922 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 26 April 2011 - 07:57 PM

Yes, you must create a new page i.e, displayphoto.php and accept an ID.

A simplistic example for this new page.

$photoid = mysql_real_escape_string($_GET['id']);
(code here with a WHERE id = $photoid statement to get photo's binary data)

//output as in example JPEG image:
header('Content-Type: image/jpg');
echo $info['photo'];
exit;
You have to be aware that no whitespace before or after the <?php ?> can occur, and the only thing being displayed is the data in the echo statement. If your image was stored correctly, browsing displayphoto.php?id=2 should work. You can use your loop in your other script to display it like this:
print "<img src='displayphoto.php?id=" . $info['id'] . "'/>";
You must SELECT the id field as well to access this, if you haven't in your code (although you seem to be SELECTing *, which is valid, but can be slow in the long run as it fetches all data)
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#10 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 27 April 2011 - 07:24 AM

Okay, wonderful. Thank you. I dooo believe that I'm still missing something. So, if I were to make a second page, how exactly do I get the variables to transfer over from both the theoretical displayphoto.php & the actual display.php. Because, wouldn't that mean that the photos would be pulled for use, then the page would have to redirect to display.php? Can it hold all that data across the two?
  • 0

#11 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 27 April 2011 - 08:20 AM

Anddd, nevermind. Didn't realize that I could call the php function via the IMG tag. Gosh.. slowly but surely he is becoming "not an idiot."
  • 0

#12 Julien Duponte

Julien Duponte

    CC Newcomer

  • Just Joined
  • PipPip
  • 19 posts

Posted 27 April 2011 - 09:39 AM

Alright, new question! I decided to go with two seperate tables, used_equip and images. And, I've all the entry/updating/removing scripts working seperately. I'm having a little trouble figuring out how to link each entry to used_equip to the image that they upload with it?

I assume that I need to create a script that uses $_POST when they get to the upload page and then applies one of the field entries from the used_equip to a field in images? I assume that I'll figure it out before you get to me Alexander :) But, no hurt in asking I figure. Thank you again for everything. Even the parts I've figured out on my own, probably wouldn't have been reached without you leading in one direction or another.
  • 0