PHP 5 image upload code, date folder, random file name

Source: Internet
Author: User
Index.htm formenctype = multipart/form-dataaction = upload. phpmethod = postinputtype = hiddenname = MAX_FILE_SIZEvalue = 2000000 inputname = userfiletype = fileinput

Index.htm



Upload. php

$ F = $ _ FILES ['Jeff _ img_upload '];

// Check whether a file is uploaded
If ($ f ['error'] = 4 ){
Echo ("script" alert ('select the file you want to upload. thank you! 'Your region location.href}'index.htm'; script ");
}
Else {
// Error determination
If ($ f ['error']) {
Echo "failed. the file is too large! The error code is ". $ f ['error'];
Exit;
}

// Type determination
If (substr ($ f ['type'], 0, 5) = 'image '){
Switch ($ f ['type']) {
Case 'image/jpeg ':
Case 'image/jpg ':
Case 'image/pjpeg ':
$ Ext = '.jpg ';
Break;
Case 'image/GIF ':
$ Ext = '.gif ';
Break;
Case 'image/png ':
Case 'image/x-png ':
$ Ext = '.png ';
Break;
Default:
Header ('content-type: text/html; charset = UTF-8 ');
Echo ("script" alert ('didn't you upload an image? 'Your region location.href}'index.htm'; </script> ");
Die ('redirecting ...');
Break;
}
} Else {
Header ('content-type: text/html; charset = UTF-8 ');
Echo ("script" alert ('didn't you upload an image? 'Your region location.href}'index.htm'; script ");
Die ('redirecting ...');
}

// Generate a random number
$ Randomnum = NULL;
For ($ j = 0; $ j <= 5; $ j ++) // The length of a random number. In this example, the length of a random number is 6.
{
Srand (double) microtime () * 1000000 );
$ Randomnumber = rand (! $ J? 1:); // generates a random number, which is not the first number with 0. in some special cases, 0 is omitted by the system.
$ Randomnum. = $ randomnumber;
}


$ Dest_dir = '/var/www/htdocs/upload'; // sets the upload Directory

// Generate the date directory. First, determine whether the date directory exists. if it does not exist, create it.
If (! File_exists (date ("Ymd "))){
Mkdir (date ("Ymd"), 0777 );}


$ Dest = $ dest_dir. '/'. date ("Ymd "). '/'. time (). '_'. $ randomnum. $ ext; // Set the file name to the timestamp and add a random number to avoid duplication.
$ R = move_uploaded_file ($ f ['tmp _ name'], $ dest );
Chmod ($ dest, 0777); // sets the attributes of the uploaded file
}
Echo "succeeded. the file address is:". $ dest;
?>

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.