PHP pictures, files bulk upload code _php tutorial

Source: Internet
Author: User

PHP image, file bulk upload code


Whether it is a file or a picture batch upload our first is to do in HTML name=userfile[] This array variable, in PHP accept we do for ($_i=0; $_i<$_cont; $_i++) traversal so that the file can be uploaded in bulk, Let me take a look at an example

Example

The code is as follows

Session_Start ();
?>




<title>Bulk upload of PHP files</title>






Bulk upload of PHP files




if ($_get[' up ']==up) {
Prevent duplicate Submissions
if ($_session[' file ']==$_get[' Irand ')} {
$_cont=intval ($_post[' cont '); The total number of file boxes is received and converted to integer type
$_size=20000; Set Limit file size
$_dir= ' pdir/'; File Save Directory
function Size ($_size) {
Determines whether the file size is greater than 1024bit if it is greater than, the size is a value of KB, and so on
if ($_size>1024*1024) {
Return round ($_size/1024/1024,2). ' MB ';
}else if ($_size>1024) {
$_size=$_size/1024;
return Ceil ($_size). ' KB ';
}else {
return $_size. ' Bit ';
}
}
Set the upload image type, set the image upload size
$_upfiles = Array (' Image/jpeg ', ' image/pjpeg ', ' image/png ', ' image/x-png ', ' image/gif ');
Uploading files with a for loop
for ($_i=0; $_i<$_cont; $_i++) {
if (Is_array ($_upfiles)) {
if (!in_array ($_files[' userfile ' [' Type '][$_i],$_upfiles)}) {
Exit (' Please upload format: jpg,png,gif file
Return ');
}
}
Determine file size
if ($_files[' userfile ' [' Size '][$_i]>$_size) {
Exit (' Upload file cannot exceed: '. Size ($_size));
}
Detects if the file has been uploaded
if ($_files[' userfile ' [' Error '][$_i]>0) {
Switch ($_files[' userfile ' [' Error '][1]) {
Case 1:echo ' uploaded file exceeds the value of upload_max_filesize option limit in php.ini ';
Break
Case 2:echo ' the size of the uploaded file exceeds the value specified by the Max_file_size option in the HTML form ';
Break
Case 3:echo ' file is only partially uploaded ';
Break
Case 4:echo ' No files were uploaded ';
Break
Case 6:echo ' Unable to find temp folder ';
Break
Case 7:echo ' file write Failed ';
Break
}
Exit
}
Get file name extension
if (!is_dir ($_dir)) {
mkdir ($_dir,0700);
}
Number of essays generated
$_rand=mt_rand (0,100000);
Get file name extension
$_n=explode ('. ', $_files[' userfile ' [' name '][$_i]); Splitting a file name
$_file_len=count ($_n); Returns the array length
Ensure that the obtained extension is the last one. The back of the
$_name=$_dir.time (). ' _ '. $_rand. '. $_N[$_FILE_LEN-1];
Move a file to a specified directory
if (Is_uploaded_file ($_files[' userfile ' [' Tmp_name '][$_i])) {
if (! @move_uploaded_file ($_files[' userfile ' [' Tmp_name '][$_i],$_name)] {
Exit (' File move failed ');
}else {
echo ' File upload succeeded
';
Echo ' File path: '. $_name. '
';
echo ' File size: '. Size (filesize ($_name));
Echo '
Return to continue uploading ';
}
}else {
Exit (' The uploaded temporary file does not exist, cannot move the file to the specified folder ');
}
}
There are several ways to destroy session variables
First, destroy all session variables: Session_destroy ();
Second type: Destroy individual such as: $_session[' file ']= '
Session_destroy ();
Exit

}else {
Exit (' You have already submitted, you cannot repeat the submission
Return ');
}
}
?>





Core principle Analysis

In the multi-file upload, our front desk is the most important

The code is as follows




These lines of code with details of the friends will find Name= "userfile[", this is stored in an array, so that we can use JS to increase this way, then how to get in PHP?

Crossing PHP processing on multiple files is a key

code as follows

for ($_i=0; $_i<$_cont; $_i++) {
if (is_uploaded_file ($_files[' userfile '] [' tmp_name '][$_i])) {
if (! @move_uploaded_file ($_files[' userfile '] [' Tmp_name '][$_i],$_name)} {
Exit (' file move failed ');
} else {
echo ' file upload succeeded
';
Echo ' File path: '. $_name. '
';
Echo ' File size: '. Size (filesize ($_name));
Echo '
returns to continue uploading ';
}
}

Here it is very simple we will see that there is a for,for is traversing the array, traversing the userfile[] array, and then by $_files[' userfile ' [' Tmp_name '][$_i] to obtain a different file image and upload it, note [$_i] Is your multi-file upload item, just to protect the array.

http://www.bkjia.com/PHPjc/873234.html www.bkjia.com true http://www.bkjia.com/PHPjc/873234.html techarticle php pictures, files bulk upload code Whether it is a file or a picture batch upload our first is in HTML to do name=userfile[] This array variable, in PHP accepted we do for ($_i=0;

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.