Tuesday 9 September 2014

multiple file upload php script

In this php tutorial let us check multiple file upload functionality using php script. This multiple file upload php script will help you to upload multiple files to your website very easily. This multiple file upload php script will also help you to restrict upload file type. Eg: you can tell the script to upload pdf file only, or upload pdf,jpg,doc file only ect by simply giving some configuring option. Let us check how you can write the code for uploading multiple file in detail. First of all let us check the html part. In the html we need a file uploading functionality. So The HTML code for multiple file upload is
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]" multiple/>
<input type="submit" value="submit"/>
</form>
In the above html code it will give you an option to select multiple files. In the form for uploading files you need to specify enctype to multipart/form-data. This is an impotent think. if you didn’t give this option your file upload will not work. in the input field i specified name=”upload_files[]“ this is because if it will take care of multiple file upload_files[] will give an array of file names. also don’t forget to give multiple in the input filed. The next is the php section. In this php section we are going to write the php script to upload multiple file. Let us check it in detail.

function
upload_multiple_file($file,$file_dir="user_files") {

$overwrite=0;
$allowed_file_type = array("pdf","ppt","pptx","xls"."xlxs","doc","docx","jpg", "jpeg", "png", "gif");
$max_file_size_check=1;
$max_file_size = 2097152;

foreach($_FILES['files']['name'] as $fkey=> $fname){

$ext = pathinfo($fname, PATHINFO_EXTENSION);
if (!in_array($ext, $allowed_file_type)) {

return "unsupported file format";
break;
}

}

foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){

$file_name = $_FILES['files']['name'][$key];

$file_size =$_FILES['files']['size'][$key];

$file_tmp_name =$_FILES['files']['tmp_name'][$key];

$file_type=$_FILES['files']['type'][$key];

if($max_file_size_check < 1) {
if($file_size > $max_file_size){

$fsize=$max_file_size/1048576;
return    'File size must be less than '.$fsize.' MB';
break;

}
}

if(is_dir($file_dir)==false){

$status =  mkdir("$file_dir", 0700);

if($status < 1){

return "unable to create  diractory $file_dir ";

}

}

if(is_dir($file_dir)){

if($overwrite < 1){

move_uploaded_file($file_tmp_name,"$file_dir/".$file_name);

}

}

//  $file_upload_query="INSERT into user_uploads (`u_id`,`file_name`,`file_type`) VALUES('$user_id','$file_name','$file_size','$file_type'); ";
//mysql_query($file_upload_query);

}

return "Success";

}
In the above code upload_multiple_file is the function which is doing all multiple file upload functionality. The upload_multiple_file function need two parameter. The first one is upload file information, and the second one is the file directory where you need to upload the file. The $overwrite=0; variable is used for checking whether the file having the same name need to be overwrite or not. If you want to overwrite the file make $overwrite to 1. $overwrite=1; The $allowed_file_type array is having the list of allowed file types. If you want to remove or add any file type you can change the $allowed_file_type array accordingly. The $max_file_size_check=1 is for file size checking. If $max_file_size_check=1; the function will check the upload file size. If $max_file_size_check=0; the function will slip the file size check. The $max_file_size = 2097152; is the maximum allowed size of the upload file. In this I also added a mysql insert query $file_upload_query this query will help you to insert the uploaded file information to database. If you want to use the database insert functionality you can uncomment those lines. If you want to store the uploaded file information in database you need to create a database table. You can create sql table by using sql query below. Sql part .

CREATE
TABLE `test`.`user_uploads` (
`id` INT( 11 ) NOT NULL ,
`u_id` INT( 11 ) NOT NULL ,
`file_name` VARCHAR( 255 ) NOT NULL ,
`file_type` VARCHAR( 255 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;
[highlight textcolor="#00aae8" bgcolor="#fff"]The usage of the function[/highlight] is below.

if
(isset($_FILES['files'])){

$res =  upload_multiple_file($_FILES['files'],"user_files");
echo $res;

}
So let us put all code together, so that it will easy to understand multiple file upload php script tutorial.

<?php

if(isset($_FILES['files'])){
$res =  upload_multiple_file($_FILES['files'],"user_files");
echo $res;
}

function upload_multiple_file($file,$file_dir="user_files") {

$overwrite=0;
$allowed_file_type= array("pdf","ppt","pptx","xls"."xlxs","doc","docx","jpg", "jpeg", "png", "gif");
$max_file_size = 2097152;

foreach($_FILES['files']['name'] as $fkey=> $fname){

$ext = pathinfo($fname, PATHINFO_EXTENSION);
if (!in_array($ext, $allowed_file_type)) {

return "unsupported file format";
break;
}

}

foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){

$file_name = $_FILES['files']['name'][$key];

$file_size =$_FILES['files']['size'][$key];

$file_tmp_name =$_FILES['files']['tmp_name'][$key];

$file_type=$_FILES['files']['type'][$key];

if($max_file_size_check >0) {
if($file_size > $max_file_size){

$fsize=$max_file_size/1048576;
return    'File size must be less than '.$fsize.' MB';
break;

}
}

if(is_dir($file_dir)==false){

$status =  mkdir("$file_dir", 0700);

if($status < 1){

return "unable to create  diractory $file_dir ";

}

}

if(is_dir($file_dir)){

if($overwrite < 1){

move_uploaded_file($file_tmp_name,"$file_dir/".$file_name);

}

}

//  $file_upload_query="INSERT into user_uploads (`u_id`,`file_name`,`file_type`) VALUES('$user_id','$file_name','$file_size','$file_type'); ";
//mysql_query($file_upload_query);

}

return "Success";

}

?>

<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]" multiple/>
<input type="submit" value="submit"/>
</form>
Download Here