0% found this document useful (0 votes)
37 views

PHP - File Upload: Move - Uploaded - File Function

The document discusses PHP's move_uploaded_file function for uploading files. It explains that move_uploaded_file requires the temporary file path and target path, and returns true on success or false on failure. An example is given demonstrating how to use move_uploaded_file and display success or error messages. A note warns that the simple file upload method shown could allow unauthorized access, so more security is needed for public use.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

PHP - File Upload: Move - Uploaded - File Function

The document discusses PHP's move_uploaded_file function for uploading files. It explains that move_uploaded_file requires the temporary file path and target path, and returns true on success or false on failure. An example is given demonstrating how to use move_uploaded_file and display success or error messages. A note warns that the simple file upload method shown could allow unauthorized access, so more security is needed for public use.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

PHP - File Upload: move_uploaded_file Function

Now all we have to do is call the move_uploaded_file function and let PHP do its magic. The
move_uploaded_file function needs to know 1) The path of the temporary file (check!) 2) The path where it is to
be moved to (check!).

PHP Code:
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

If the upload is successful, then you will see the text "The file filename has been uploaded". This is
because $move_uploaded_file returns true if the file was moved, and false if it had a problem.
If there was a problem then the error message "There was an error uploading the file, please try again!"
would be displayed.

PHP - File Upload: Safe Practices!

Note: This script is for education purposes only. We do not recommend placing this on a web page
viewable to the public.
These few lines of code we have given you will allow anyone to upload data to your server. Because of
this, we recommend that you do not have such a simple file uploader available to the general public. Otherwise,
you might find that your server is filled with junk or that your server's security has been compromised.
We hope you enjoyed learning about how to work with uploading files with PHP. In the near future we will
be adding an advanced lesson that will include more security and additional features!

You might also like