Jump to content
Larry Ullman's Book Forums

Recommended Posts

I am attempting to add a feature to my website where users are able to upload images for companies, and when looking at the code for 10.3 I noticed something that confused be on line 28 & 29:

 

if (move_uploaded_file ($_FILES['upload']['tmp_name'], "../uploads/{$_FILES['upload']['name']
}")) { echo '<p><em>The file has been uploaded!</em></p>';

 

What confuses me about this line is that, up until this point it does not appear that the file has actually been moved to the permanent directory. How can we expect this conditional to ever come back TRUE (since it is a conditional...) if we've never established the permanent directory. It is never established at the end (the actual HTML form) nor the code before the conditionals (ie the HTML header area).

 

Are these two lines supposed to both move the temp file AND act as a conditional? Or have we established this somewhere in the PHP.ini file? Because the PHP.ini file has upload_tmp_dir but not the permanent directory we want it to transfer the temp files to.

 

Am I missing something here? Is this an error?

Link to comment
Share on other sites

thara is right, you have to create the uploads folder. I have a different edition of the book, but there is a step in the 4th edition that instructs you to create the directory.

 

The move_uploaded_file() does the actual moving, and will return true if successful, or false if it fails.The reasons for failure could be that it was not provided a valid filename, or if the file could not be moved due to permissions or a path error. If the destination folder already has a file by that name, it will be overwritten. Check out the User Contributed Notes on the manual page.

Link to comment
Share on other sites

Okay I have manually created an 'uploads' folder but what I'm trying to get at is that we don't use the move_uploaded_file function before we test if it was actually done.

 

It's like testing that a variable was set before actually setting the variable. At what stage or line during the script does the file get uploaded to the "uploads" folder. And is that folder supposed to be the temporary or permanent folder?

 

The lines I showed are testing a CONDITIONAL of whether or not the file was moved, right? That line (with the 'if') isn't actually moving the file. Do you at least understand what I'm trying to ask?

Link to comment
Share on other sites

I think everyone understands what you are trying to ask - the code may look a little misleading if you're not used to it.

When the html form is submitted, it is temporarily stored in the $_FILES superglobal. Your php script moves it to its permanent folder using

the 'if' line (below). As Paul said previously, move_uploaded_file moves the file AND checks that the move is successful.

if (move_uploaded_file ($_FILES['upload']['tmp_name'], "../uploads/{$_FILES['upload']['name']
}"))

You can code it this way because of the way the move_uploaded_function works - it moves the file to the specified directory AND returns a boolean to indicate the success or otherwise of the move. Check out the php manual. What error are you getting?

Link to comment
Share on other sites

Okay that makes perfect sense. Thank you for connecting those two points. I am not getting an error yet because I have not implemented the code yet. I am lifting much of it to insert into my own website and have not tested the script yet. I have some experience with form handling but not file uploading so the next 1-2 weeks working on this will be fairly rocky.

 

Thanks a ton for the help!

Link to comment
Share on other sites

 Share

×
×
  • Create New...