php single file upload and multiple file upload

php upload file steps
1. Determine whether the file is a form submission2
. Determine whether there is a selected file3 . Determine whether the file
storage path exists4 . Determine
whether the file type conforms Uploaded successfully



The uploaded files are required to be word and ppt types

Single file upload code:

<?
print_r($_FILES);
extract($_FILES["f1"]);
//Determine whether it is a form upload
if(!isset($_POST["sub"])){
exit("Please upload through a form") ;
}
//To determine whether there is a file to upload
if(empty($name)){
exit("Please select the file");
}
//To determine whether the folder exists
$dir="f:/file/upload/";
if( !is_dir($dir)){
mkdir($dir,0777,true);
}
//Determine whether the file 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");
}
//judgment Error message
switch($error){
case 1:
echo "Exceeded size set by server";
break;
case 2:
echo "Exceeded size of hidden field setting";
break;
case 3:
echo "Partial file upload";
break;
case 4:
echo "No file upload";
break;
case 6:
echo "Temporary folder does not exist";
break;
case 7:
echo "Error writing to folder";
break;
}
//To determine whether the temporary file exists
if(!is_uploaded_file($tmp_name)){
exit("The temporary file does not exist");
}
//To determine whether 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 uploaded successfully";
}

?>

Multiple file upload:

<?
print_r($_FILES);
extract($_FILES["fi"]);

//Determine whether the file is submitted in the form
if(!isset($_POST["sub"])){
exit("Please use the form to submit");
}
//Determine whether there is a file upload
if(empty($name)){
exit ("Please select a file");
}
//Determine whether the upload path exists
$dir="f:/file/upload/";
if(!is_dir($dir)){
mkdir($dir,0777,true);
}
for($i=0;$i<count($name);$i++){
//Determine whether the file type conforms to
$arr=array("doc","docx","xls","xlsx","ppt ","pptx");
$type=substr(strrchr($name[$i],"."),1);
if(!in_array($type,$arr)){
echo "{$i}th The file type does not match";
continue;
}
switch($error[$i]){
case 1:
echo "The {$i}th file exceeds the size set by the server";
continue;
case 2:
echo "The {$i}th file exceeds the size set by the hidden field";
continue;
case 3:
echo "The {$i}th file is partially uploaded";
continue;
case 4:
echo "The {$i}th file has no file uploaded";
continue;
case 6:
echo "The {$i}th file has no temporary file. exists";
continue;
case 7:
echo "Error writing the {$i} file to the folder";

  continue;

}
//To determine whether the temporary file exists
if(!is_uploaded_file($tmp_name[$i])){
echo "The temporary file does not exist";
continue;
}
//To determine whether the upload is successful
$path=$dir.rand(1000,9000 ).".".$type;
if(!move_uploaded_file($tmp_name[$i],$path)){
echo "The {$i}th file upload failed";
continue;
}else{
echo "Upload successful";
}

}
?>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324514156&siteId=291194637