PHP file Upload settings and processing (single file)

Source: Internet
Author: User

<!--upload.php Content--
<?php
/*
Modify the settings for PHP.ini
File_uploads must be on
Upload_max_filesize set the size of the upload file, which is less than post_max_size
Post_max_size set the maximum value that the Post method can receive
Upload_tmp_dir temporary directory where uploaded files are stored
*/
Header ("Content-type:text/html;charset=utf-8");
/*
Array
(
[pic] = Array
(
[Name] = 195_4074_831a070561e20a0.jpg
[Type] = Image/jpeg
[Tmp_name] = C:\wamp\tmp\php45.tmp
[ERROR] = 0
[Size] = 43274
)
)
*/
First step: Judging errors
if ($_files[' pic ' [' ERROR '] > 0) {
Switch ($_files[' pic ' [' ERROR ']) {
Case 1:
echo "indicates that the size of the uploaded file exceeds the agreed value. The maximum size of the file is specified in the PHP configuration file, and the instruction is:upload_max_filesize<br> ";
Break
Case 2:
echo "indicates that the upload file size exceeds the maximum value specified by the Max_file_size element of the HTML form's hidden field property 1m<br>";
Break
Case 3:
echo "indicates that the file is only partially uploaded <br>";
Break
Case 4:
echo "means not uploading any files <br>";
Break

Default
echo "Last known error <br>";
Break
}
Exit
}

Step two: Determine the type
$arr = Explode (".", basename ($_files[' pic ' [' name ']));
$hz = Array_pop ($arr);

$allowtype =array ("gif", "png", "JPG", "jpeg");

if (!in_array ($hz, $allowtype)) {
echo "The type of upload is not legal";
Exit
}

Step three: Determine the size
$maxsize = 1000000;
if ($_files[' pic ' [' size '] > $maxsize) {
echo "uploaded file exceeded, {$maxsize} bytes!";
Exit
}

The fourth step, the file name after uploading must be set
$tmpfile = $_files[' pic ' [' tmp_name '];
Set Random file names
$srcname = "./uploads/". Date ("Ymdhis"). Rand (100, 999). ".". $hz;

Copy the uploaded file under the temporary directory to the directory I specified, and the specified name can be uploaded.
if (Move_uploaded_file ($tmpfile, $srcname)) {
echo "Upload succeeded!";
}else{
echo "Upload failed!";
}
?>

<!--upform.html Content--
<!--upload a form, you must use the Post method, enctype= "Multipart/form-data" must be used--
<form action= "upload.php" method= "post" enctype= "Multipart/form-data" >
Name: <input type= "text" name= "username" value= "/><br>
<!--hide the form, the effect is to be prompted when the file exceeds 1m, just a friendly hint--
<input type= "hidden" name= "max_file_size" value= "1000000"/>
Up Pic: <input type= "file" name= "pic" value= "" ><br>

<input type= "Submit" value= "Upload"/><br>

</form>

PHP file Upload settings and processing (single file)

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.