PHP Single File upload and multiple file uploads

Source: Internet
Author: User

PHP Upload file steps
1. Determine if the file is a form submission
2. Determine if there is a selection file
3. Determine if the file storage path exists
4. Determine if the file type matches
5. Determine the type of error
6. Determine if a temporary file exists
7. Determine if the upload was successful

Required files to be uploaded are word and ppt type

Single File Upload code:

<?
Print_r ($_files);
Extract ($_files["F1"]);
Determine if the form is being uploaded
if (!isset ($_post["sub"])) {
Exit ("Please upload via form");
}
Determine if there is a file upload
if (empty ($name)) {
Exit ("Please select File");
}
Determine if a folder exists
$dir = "f:/file/upload/";
if (!is_dir ($dir)) {
mkdir ($dir, 0777,true);
}
Determine if the document meets the requirements
$arr =array ("Doc", "docx", "xls", "xlsx", "ppt", "pptx");
$type =substr (STRRCHR ($name, "."), 1);
if (!in_array ($type, $arr)) {
Exit ("File type does not match");
}
Judging error messages
Switch ($error) {
Case 1:
echo "exceeds the size of server settings";
Break
Case 2:
echo "exceeds the size of hidden field settings";
Break
Case 3:
echo "partial file Upload";
Break
Case 4:
echo "no file Upload";
Break
Case 6:
echo "Temp folder does not exist";
Break
Case 7:
echo "Error writing to Folder";
Break
}
Determine if a temporary file exists
if (!is_uploaded_file ($tmp _name)) {
Exit ("Temporary file does not exist");
}
Determine if the file was uploaded successfully
$path = $dir. Rand (1000,9000). ".". $type;
if (!move_uploaded_file ($tmp _name, $path)) {
Exit ("File upload failed");
}else{
echo "File upload succeeded";
}

?>

Multiple file uploads:

<?
Print_r ($_files);
Extract ($_files["fi"]);

Determine if a file is a form submission
if (!isset ($_post["sub"])) {
Exit ("Please submit with form");
}
Determine if there is a file upload
if (empty ($name)) {
Exit ("Please select File");
}
Determine if the upload path exists
$dir = "f:/file/upload/";
if (!is_dir ($dir)) {
mkdir ($dir, 0777,true);
}
for ($i =0; $i <count ($name); $i + +) {
Determine if the file type matches
$arr =array ("Doc", "docx", "xls", "xlsx", "ppt", "pptx");
$type =substr (STRRCHR ($name [$i], "."), 1);
if (!in_array ($type, $arr)) {
echo "{$i} file types do not match";
Continue
}
Switch ($error [$i]) {
Case 1:
echo "{$i} files exceeding the size of server settings";
Continue
Case 2:
echo "{$i} files exceeding the size of hidden domain settings";
Continue
Case 3:
echo "{$i} files part of file upload";
Continue
Case 4:
echo "{$i} files without file upload";
Continue
Case 6:
echo "{$i} files temporary file does not exist";
Continue
Case 7:
echo "{$i} files write to folder error";

  continue;

}
Determine if a temporary file exists
if (!is_uploaded_file ($tmp _name[$i])) {
echo "Temporary file does not exist";
Continue
}
Determine if the upload was successful
$path = $dir. Rand (1000,9000). ".". $type;
if (!move_uploaded_file ($tmp _name[$i], $path)) {
echo "{$i} file upload failed";
Continue
}else{
echo "Upload success";
}

}
?>

PHP Single File upload and multiple file uploads

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.