Home >Backend Development >PHP Tutorial >php file upload_PHP tutorial
php Tutorial File Upload
Before you can use PHP to manage your uploads, you first need to build an HTML form that serves as the user interface for uploading files
. Have an example look below and save an editable HTML code.
When there are some rules that need to be constructed Follow the HTML form. First, make sure the form uses the POST method. Second, the form
requires the following attributes: character encoding = "multiple/form-data". It specifies the content type used when submitting
information to the server. Without these requirements, your file cannot be uploaded.
Another thing to note is that the hidden form field named MAX_FILE_SIZE sets the value. Some web browsers
actually reflect this field and will not allow users to upload files larger than this number (in bytes). You should
set this value to match the maximum upload size, set in the php.ini file. This is a set with
upload_max_filesize, the default value is 2MB. But it still doesn't guarantee that your script won't deliver files that are
inches larger. The danger is that an attacker will try to send you a request for several large files and fill out the file system, which is where PHP stores the decoded files. Set the post_max_size directive in php.ini to the maximum
file size you want (must be greater than upload_max_filesize). The default value is 10MB. This directive controls the maximum size of POST data allowed within a
request. Also, make sure that file_uploads is set to On in your php.ini file
.
At least, there is a look at the input tag attribute: type="file". It is used to specify the input
element for file selection control. This provides a file URI in place that you need to type into a "Browse" button and can be used as an alternative to the
URI input.
After the user enters a file URI and clicks the submit button a copy of the file will be sent to the server and the user will
be redirected to upload.php. This PHP file will handle form data.
Back to top
Handling form data (PHP code)
When the file is uploaded and PHP creates a temporary copy of the file and sets up the superglobal variable $_FILES Array,
contains information about the file. For each file, there are 5 data. We have uploaded the field named
as 'uploaded_file', so the following data will be present:
Variable $_FILES ["uploaded_file"] ["name"] The original of the file uploaded from the user's machine Name
Variable $_FILES["uploaded_file"]["type"] The MIME type of the uploaded file (if the browser
provides the type)
variable $_FILES["uploaded_file"] ["tmp_name"], where the file is temporarily stored on the server
The following example accepts an uploaded file and saves it in the upload directory. It allows uploading only JPEG
images under 350Kb. The code itself is fairly self-explanatory, but we'll give it some explanation. There is an example in the look and save
save this PHP code as upload.php.
//Сheck that we have a file $filename = basename($_FILES['uploaded_file']['name' ]);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg") && ($_FILES["uploaded_file"] ["type"] == "image/jpeg")
&&
//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'/upload/'.$filename;
//Check if the file with the same name is already exists on the
server
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file']
['tmp_name'],$newname))) {
echo "It's done! The file has been saved as: ".$newname; occurred during file upload!";
}
} else {
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already
exists" ;
} else {
echo "Error: Only .jpg images under 350Kb are accepted for upload";
}
} else {
echo "Error: No file uploaded ";
}
?>
Before uploading the file you need to determine if the file is actually uploading anything. After that, we check the files uploaded on
, JPEG images, and their sizes are less than 350Kb. Next, we determine the path, which is the name of these files where we want to save this file and check if there is already a server. When all checks pass,
we copy the file to a permanent location using the move_upload_file() function. This feature also confirms that the
file you want to process is a legitimate file as a result of the user upload. If the file is uploaded successfully, the corresponding
message will appear.
Note: Make sure PHP has allowed reading and writing of temporary files in the directory where you want to copy the files.
This example is actually very simple. It is proposed to demonstrate how to use PHP to upload files. For example, you can add
new conditions and allow uploading of GIF and PNG images, or any other kind of files you need. If you are new to this tutorial
using PHP might be a good place to start.
www.bkjia.com