PHP multiple file upload Script

Adding Photos to a gallery
We will learn how to upload multiple files using a single form. This is required if you are allowing members to upload more than one file and you don't know how many files you allow them to upload.

HTML Form for multiple file upload

In HTML5 we have a better option to use. One single button will be displayed for the user. On click of the button user can browse their local file system and select more than one file by pressing ctrl or shift key.
<form action='uploadck.php' method=post target='myiframe' enctype='multipart/form-data' >
<input type=hidden name=todo value='upload'>
<table >
<tr ><td class='data'>Images</td><td>
<input type=file name='file_up[]' multiple>
<input type=submit value='Upload Images'></td></tr>
</table>
</form>
Note the attribute multiple inside upload tag. With this we can press ctrl key in our keyboard and select multiple file at same time. We can select files as many as we want. The same will be available for the backend PHP script.

Before we upload the files , we will set some values inside our PHP script
set_time_limit (0);
$max_file_size=2000; // This is in KB

@$todo=$_POST['todo']; // getting other form data ( optional )

$path_upload="upload/";
$path_thumbnail="upload_thumb/";

/// for thumbnail image size //
$n_width=100;
$n_height=100;
$required_image_width=890; // Width of resized image after uploading
We will get a global array inside our PHP script to know about the files to be uploaded.
$_FILES['file_up']
We will check all files by looping and uploading them to server.
while(@list($key,$value) = each($_FILES['file_up']['name']))
{
$file_name=$value;
$add = $path_upload.$file_name; 
// check file extensions for allowing images only
// create thumbnail 
// Resize the image if size is more 
// display the thumbnail image ( for iframe inside upload.php) 
}
Allow only if images by checking file extensions
if (!($_FILES[file_up][type][$key] =="image/jpeg" OR $_FILES[file_up][type][$key] =="image/gif"))
{$msg=$msg."Your uploaded file must be of JPG or GIF. ";
$msg.="($file_name) Other file types are not allowed<BR>";
$file_upload_flag="false";
}else{
copy($_FILES['file_up']['tmp_name'][$key], $add);//upload the file to the server
chmod("$add",0777); // set permission to the file.
/// Continue with thumbnail creation and resizing if required 
}
Creating Thumbnail images
//////////ThumbNail creation //////////////////
if(file_exists($add)){
$tsrc=$path_thumbnail.$file_name; 
$im=ImageCreateFromJPEG($add); 
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height); 
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImageJpeg($newimage,$tsrc);
chmod("$tsrc",0777);
}// end of if
////////Ending of thumb nail ////////

Php Thumbnail image creation

Resize the image if size is more
//// Resize original image  if width is more than 890 /////
if($required_image_width < $width){// width is set at starting of this page
$adjusted_height=round(($required_image_width/$width) * $height);
//echo $adjusted_height . " - ".$height."<br>";
$im=ImageCreateFromJPEG($add); 
$newimage=imagecreatetruecolor($required_image_width,$adjusted_height); 
imageCopyResized($newimage,$im,0,0,0,0,$required_image_width,$adjusted_height,$width,$height);
ImageJpeg($newimage,$add);
chmod("$add",0777);
} // end of if width of image  is more 

Using iframe to upload multiple files

We are submitting the form to a different page to execute the code for uploading files. Without taking the browser to uploadck.php we can execute the uploading code by keeping one iframe. We will hide the border and only display the output displayed by uploadck.php file within the iframe. The hidden iframe ( without border ) src parameter will execute uploadck.php file.

Here is the change in code ( of upload.php ) to display the file uploading form with iframe.
<form action='uploadck.php' method=post target='myiframe' enctype='multipart/form-data' >
<input type=hidden name=todo value='upload'>
<table >
<tr ><td class='data'>Images</td><td>
<input type=file name='file_up[]' multiple>
<input type=submit value='Upload Images'></td></tr>
</table>
</form>

<iframe name='myiframe' src='uploadck.php' style="overflow: hidden; height: 100%; width: 100%; position: absolute;" frameBorder=\"0\"> 
</iframe>
As this script allows more than one file to upload so this script is likely to take more time than normal execution time. So it is better to set a higher value to manage maximum script execution time for this script.

Photo gallery script

By using file upload script we can develop a image gallery script where users can upload multiple photos. You can read more.
Photo gallery script with picture upload using PHP MySQL
Uploading file by using JQuery Ajax
Without refreshing pages we can upload files along with other input data by using multipart/form-data. The details we enter along with the uploaded file name can be stored in a MySQL database table.
Add Record & Images To MySQL table
Thumbnail Image generation from uploaded files
Php file upload ( single file )


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    meera venugopal

    17-09-2014

    example code please

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer