PHP - File Upload: Move - Uploaded - File Function
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/";
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.
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!