Introduction of File Upload _php instance via PHP simple example

Source: Internet
Author: User
Tags file upload php file upload temporary file storage

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.

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.