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

PHP - File Upload: What's The PHP Going To Do?

The document discusses PHP file uploads and provides code to handle file uploads. It explains that PHP stores uploaded files temporarily and they need to be moved to a permanent location. It demonstrates getting the temporary file name from the $_FILES array, choosing a permanent file name and path, and moving the file to the uploads folder to save it permanently. The code example gets the original file name from $_FILES, constructs a target path in the uploads folder, and is ready to save the file permanently to the server in that location.

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)
28 views

PHP - File Upload: What's The PHP Going To Do?

The document discusses PHP file uploads and provides code to handle file uploads. It explains that PHP stores uploaded files temporarily and they need to be moved to a permanent location. It demonstrates getting the temporary file name from the $_FILES array, choosing a permanent file name and path, and moving the file to the uploads folder to save it permanently. The code example gets the original file name from $_FILES, constructs a target path in the uploads folder, and is ready to save the file permanently to the server in that location.

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: What's the PHP Going to Do?

Now that we have the right HTML form we can begin to code the PHP script that is going to handle our
uploads. Typically, the PHP file should make a key decision with all uploads: keep the file or throw it away. A
file might be thrown away from many reasons, including:

• The file is too large and you do not want to have it on your server.
• You wanted the person to upload a picture and they uploaded something else, like an executable file
(.exe).
• There were problems uploading the file and so you can't keep it.

This example is very simple and omits the code that would add such functionality.

PHP - File Upload: uploader.php

When the uploader.php file is executed, the uploaded file exists in a temporary storage area on the server.
If the file is not moved to a different location it will be destroyed! To save our precious file we are going to need
to make use of the $_FILES associative array.
The $_FILES array is where PHP stores all the information about files. There are two elements of this
array that we will need to understand for this example.

• uploadedfile - uploadedfile is the reference we assigned in our HTML form. We will need this to tell
the $_FILES array which file we want to play around with.
• $_FILES['uploadedfile']['name'] - name contains the original path of the user uploaded file.
• $_FILES['uploadedfile']['tmp_name'] - tmp_name contains the path to the temporary file that resides
on the server. The file should exist on the server in a temporary directory with a temporary name.

Now we can finally start to write a basic PHP upload manager script! Here is how we would get the
temporary file name, choose a permanent name, and choose a place to store the file.

PHP Code:
// Where the file is going to be placed
$target_path = "uploads/";

/* Add the original filename to our target path.


Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$_FILES['uploadedfile']['tmp_name'];

NOTE: You will need to create a new directory in the directory where uploader.php resides, called
"uploads", as we are going to be saving files there.
We now have all we need to successfully save our file to the server. $target_path contains the path where
we want to save our file to.

You might also like