[Home] [Welcome to PHP] [Data & Dynamic] [File system]

Uploading a file

To simplify the process, it is best that it is split between the html form and the php page which actually does the uploading. In the html form page, insert the following code, which will allow for choosing the file and uploading by sending the file contents to the php code that will save the file to the servers file system.





The actual code

In the second page, upload.php, place the following code. First we need to specify the directory to store the uploaded files. We store the directory name in $uploadDir. Note that PHP must have write access to $uploadDir or else the upload will fail. If you're web host using a Linux server you may need to set the permission for the upload directory to 777.



The key here is the move_uploaded_file() function. This function will move the uploaded files from the temporary upload directory to the location that we earlier ( $uploadDir . $fileName ). If for some reason the functioncannot move the file it will return false and we exit the script because continuing the script is no use.