PHP File Upload Simple example, get file name, type, size and other related information, complete the file upload, for everyone to learn reference.
1, upload the file code:
Code
<?php//Judge whether the temporary file storage path contains the uploaded file if (Is_uploaded_file ($_files["UploadFile" ["Tmp_name"])) {//For more efficient, store the information in the variable $ upfile=$_files["UploadFile"];//use an array-type string to store information about the uploaded file//print_r ($upfile);//If printed, output an array of information like this ([name] => m.jpg [ Type] => image/jpeg [tmp_name] => C:\WINDOWS\Temp\php1A.tmp [ERROR] => 0 [size] => 44905) $name = $upfile ["Name "];//is convenient for later transfer of files named $type = $upfile [' type '];//upload file types $size = $upfile [size]];//upload file sizes $tmp _name= $upfile [" Tmp_name "];//
The temporary name of the user uploads the file $error = $upfile ["Error"];//in the process of uploading the wrong information//echo $name;
To judge the file type, determine whether to transfer the file, if the requirements set $ok=1 can transfer switch ($type) {case "image/jpg": $ok = 1;
<a href= "http://www.jb51.net/zt/break/" target= "_blank" class= "Infotextkey" >break</a>;
Case "Image/jpeg": $ok = 1;
Break
Case "Image/gif": $ok = 1;
Break
Default: $ok = 0;
Break ///If the file meets the requirements and there are no errors in the upload process ($ok && $error = = ' 0 ') {//Call Move_uploaded_file () function, file transfer Move_uploaded_file ($tmp _
Name, ' up/'. $name); Successful operation, the prompt echo "<script language=\" <a href= HTTP://WWW.Jb51.net/js/"target=" _blank "class=" Infotextkey ">javascript</a>\" >alert (' succeed ') </script> ";
}else{//If the file does not conform to the type or if there are errors in the upload process, the prompt fails echo "<script language=\" javascript\ >alert (' failed ') </script> ";} }?>
2, the form of submitting documents
Code
<form enctype= "Multipart/form-data" method= "post" name= "Uploadform" >
<input type= "file" name= UploadFile "value=" Upload File ">
<input type=" Submit "name=" Submit "value=" Upload ">
</form>
The above content is small to share with you through a simple example of php file upload, I hope you like.