Sunday, November 15, 2009

How to upload files in PHP

How to upload files in PHP?

Following example shows you how to upload files using php.

   When you are working with web applications, you may want to upload files to your web server. In php you can upload files very easily. But there are some configuration settings you have to check before you upload a file. Remember that you have to set the form attribute "enctype" to multipart/form-data in your html file. It is a good practice that use separate folder for keep uploaded file in your web server. Also you can create sub folders for keep images, csv files, audio file, etc. Before you upload file check that folder exist in the server. Also make sure that the folder permission is 0777.

   Remember that what are the file types that allow user to upload. Never allow users to upload files containing scripts such as file with .php, .asp, .aspx, .js, etc. extensions. If a user upload such a file it will be a big security risk to your web site. So make sure what are the file types allow to upload. First you have to filter files by extension to avoid this security risk. Also you have to check the files that already exists in the server with the same name. Otherwise previous file will be overwrite from the new one.

Demo- File uploading using PHP.

Configuration settings associated with file upload in PHP

There are some configuration settings in the php.ini file associated with file uploading.

» file_uploads (On/off) Whether to allow HTTP file uploads.
» upload_tmp_dir (C:\temp) Temporary directory for uploaded files.
» upload_max_filesize (2M) Maximum allowed size for uploaded files.
» post_max_size (8M) Maximum size of POST data that PHP will accept. If you want to upload large files you have to set post_max_size larger value than upload_max_filesize value.
» memory_limit (128M) Maximum amount of memory that script can consume.
» max_execution_time (30) Maximum execution time for a script, in seconds. When you need to upload large files you have to set this value to a higher value.
» max_input_time (60) Maximum amount of time each script may spend parsing request data.

$_FILES['my_file']['x'] is an associative array in php. It contains details of the uploaded file such as file name, file size and file type.
First file is uploaded to the temporary directory in the server machine as a temporary file. such as "C:\temp

PHP move_uploaded_file() uploaded function copied the uploaded temporary file to the save location. Temporary file is deleted after the upload.

» $_FILES["file"]["name"] Name of the uploaded file.
$_FILES["file"]["type"] Content type of the uploaded file.
$_FILES["file"]["size"] Size in bytes of the uploaded file.
$_FILES["file"]["tmp_name"] Name of the temporary copy of the file stored on the server.
$_FILES["file"]["error"] Error code regarding the file upload.
Try It!

Bookmark and Share

1. Example code for upload files using PHP



<title>File Upload in PHP </title>


<form action="upload.php" method="post" enctype="multipart/form-data" name="file_upload_form" id="file_upload_form">

<table border="0">


<td width="94">Select File </td>

<td width="96"><input type="file" id="my_file" name="my_file" /></td>





<input name="btn_upload_file" type="submit" id="btn_upload_file" value="Upload" />




copy above code into a notepad and save it as upload.html and copy following code into another notepad and save it as upload.php
PHP code


$file_name = $_FILES['my_file']['name'];

$file_type = $_FILES['my_file']['type'];

$file_size = $_FILES['my_file']['size']/1024;

$temp_path = $_FILES['my_file']['tmp_name'];


$uploaded = move_uploaded_file($temp_path,$file_save_path);


print("The file ".$file_name." has been uploaded successfully");

print("<br/> File Type : ".$file_type);

print("<br/> File Size : ".$file_size. "Kb");




print("Error while uploading the file ".$_FILES['my_file']['error']);



 »   How to create random validation image in PHP
 »   How to get extension of a file using PHP
 »   How to upload and encrypt file in PHP
 »   How to upload files in PHP
 »   How to create arrays in PHP
 »   How to create thumbnail images in PHP
 »   How to connect MySQL Database in PHP

©-Copyright By Duminda Chamara  
JavaScript Validation