Home  >  Article  >  Backend Development  >  PHP file upload example code_PHP tutorial

PHP file upload example code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:19:01743browse

Project structure:

Upload home page:

Upload effect:


fileupload.php

Copy code The code is as follows:

name="uploadfile">Upload file:


//print_r($_FILES["upfile"]);
if( is_uploaded_file($_FILES['upfile']['tmp_name'])){
$upfile=$_FILES["upfile"];
//Get the value in the array
$name=$upfile[ "name"];//The file name of the uploaded file
$type=$upfile["type"];//The type of the uploaded file
$size=$upfile["size"];//Uploaded file The size of ':$okType=true;
break;
case 'image/jpeg':$okType=true;
break;
case 'image/gif':$okType=true;
break;
case 'image/png':$okType=true;
break;
}

if($okType){
/**
* 0: File uploaded successfully

* 1: File size exceeded, set in php.ini file

* 2: File size exceeded The value specified by the MAX_FILE_SIZE option

* 3: Only part of the file is uploaded

* 4: No file is uploaded

* 5: Upload file size is 0
*/
$error=$upfile["error"];//The value returned by the system after uploading
echo "================
";
echo "The name of the uploaded file is: ".$name."
";
echo "The type of the uploaded file is: ".$type."
";
echo "The uploaded file size is: ".$size."
";
echo "The value returned by the system after uploading is: ".$error."
";
echo "The temporary storage path of the uploaded file is: ".$tmp_name."
";

echo "Start moving the uploaded file
";
/ /Move the uploaded temporary file to the up directory
move_uploaded_file($tmp_name,'up/'.$name);
$destination="up/".$name;
echo "=== =============
";
echo "Upload information:
";
if($error==0){
echo "File uploaded successfully! ";
echo "
Image preview:
";
echo "";
//echo " alt=" Image preview:rFile name:".$destination."rUpload time:">";
}elseif ($error==1){
echo "Exceeded file size, in php.ini file ";
}elseif ($error==2){
echo "The file size exceeds the value specified by the MAX_FILE_SIZE option";
}elseif ($error==3){
echo "Only part of the file was uploaded";
}elseif ($error==4){
echo "No file was uploaded";
}else{
echo "The uploaded file size is 0" ;
}
}else{
echo "Please upload images in jpg, gif, png and other formats! ";
}
}
?>


In the fileupload.php file:


Copy code
The code is as follows: //Determine whether it is a pictureswitch ($type){
case 'image/pjpeg':$okType=true;
break;
case 'image/jpeg':$okType=true;
break;
case 'image/gif':$okType=true;
break;
case 'image/png':$okType =true;
break;
}


The above is to determine whether the file is an image type. For more file types, you can refer to the tomcat/conf/web.xml file, here A wide range of file types....


http://www.bkjia.com/PHPjc/325340.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/325340.htmlTechArticleProject structure: Upload homepage: Upload effect: fileupload.php Copy the code as follows: form action="" enctype= "multipart/form-data" method="post" name="uploadfile"Upload file:...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn