PHP Upload Files

Source: Internet
Author: User

Uploading file Forms

<title> Upload a file form </title>
<body>
<form enctype= "Multipart/form-data" action= "" method= "POST" >
Please select File:<br>
<input name= "Upload_file" type= "file" ><br>
<input type= "Submit" value= "Upload file" >
</form>
</body>

<?
$upload _file=$_files[' upload_file ' [' tmp_name '];
$upload _file_name=$_files[' upload_file ' [' name '];
if ($upload _file) {
$file _size_max = 1000*1000;//1M limit file upload maximum capacity (bytes)
$store _dir = "d:/";//storage location of uploaded files
$accept _w = false;//allows overwriting of the same file
$upload _file_size=$_files[' upload_file ' [' Size '];
Check File size
if ($upload _file_size > $file _size_max) {
echo "Sorry, your file size is greater than the provision";
Exit
}
Check read and write files
if (file_exists ($store _dir. $upload _file_name) && $accept _w) {
Echo "file with the same file name exists";
Exit
}
Copy files to the specified directory
if (!move_uploaded_file ($upload _file, $store _dir. $upload _file_name)) {
echo "Failed to copy file";
Exit
}
}
Echo "<p> you uploaded the file:";
echo $_files[' upload_file ' [' name '];
echo "<br>";
The original name of the client machine file.
Echo "file has a MIME type of:";
echo $_files[' upload_file ' [' type '];
The MIME type of the file requires the browser to provide support for that information, such as "Image/gif".
echo "<br>";
Echo "Upload file size:";
echo $_files[' upload_file ' [' Size '];
The size of the uploaded file, in bytes.
echo "<br>";
Echo "file was temporarily stored as:" After uploading;
echo $_files[' upload_file ' [' tmp_name '];
Temporary file names stored on the server after the files have been uploaded.
echo "<br>";
$Erroe =$_files[' upload_file ' [' Error '];
Switch ($Erroe) {
Case 0:
Echo "Upload success"; Break
Case 1:
Echo "uploaded a file that exceeds the value of the 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";
Case 4:
Echo "No files were uploaded";
}
?>

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.