PHP File Upload

PHP File Upload

In PHP file upload concept used to upload files to the server.

we need to upload file in web application such as upload resume in job portal site, upload profile picture in social networking site, upload images in gallery and upload file as attachment for send mail.
All this kind of uploading we use file upload concept to upload image file, text file or any document to the server.

HTML design form for upload file.

PHP File Upload Note :

  • Make sure the method=”post”
  • Must use attribute enctype=”multipart/form-data” in form tag.

HTML upload.html script for file upload

The output of above script :

PHP File upload
PHP File upload example

When click upload button the “fileupload.php” script will execute, write upload file code in fileupload.php script file.

The fileupload.php script code

IF the image uploaded successfully then display success message otherwise display error message.

Check File already exists or not

when we upload a file, if file already exists in directory then it will give us some error message.
Here, for check existence of file we use php file_exists() function.

Check file type

Some time we allow to upload only jpeg and gif image file, so we need to check file type.
In php we use $_FILES[“file”][“type”] to check the file type.

Check file size

We know that many website restricted to upload larger file. For make restriction on upload larger file we need to check file size before uploading file.
In php $FILE[“file”][“size”] used to check the file size in bytes.

Example of check file size

We assume that the uploaded file must be smaller than 800kb.

Now, lets take an example to check file type, file size and file directory where file uploaded.

PHP file upload example

The output of file upload example is :

PHP File uplaod
PHP File Upload Example

Leave a Reply

Your email address will not be published. Required fields are marked *