November 15 Morning File Upload

Source: Internet
Author: User
Tags save file

1. Submit the information displayed after uploading the file

<formAction= "chuli.php"Method= "POST"enctype= "Multipart/form-data"><!--enctype= "Multipart/form-data stands for uploading files -    <inputtype= "File"name= "File" />    <inputtype= "Submit"value= "Upload" /></form>
<? PHP Var_dump ($_files); // $_files is looking for documents.

Click Upload to display the following information.

The displayed data is a two-dimensional array, file represents the name value of the uploaded marquee, and the array inside the file is the basic information for the uploaded image.

(1) Name: file name is 001.png

(2) Type: Types of uploaded files

(3) Tmp_name: The path of temporary storage of uploaded files, if not operation will be deleted.

(4) Error: Incorrect message during upload.

(5) Size of the upload file, in bytes B.

2. Specific steps to upload a file

<formAction= "chuli.php"Method= "POST"enctype= "Multipart/form-data"><!--enctype= "Multipart/form-data stands for uploading files -    <inputtype= "File"name= "File" /><!--multiple files can be uploaded here, and a loop is used to upload the files in the chuli.php processing page.  -    <inputtype= "Submit"value= "Upload" /></form>
<?PHP//Var_dump ($_files);//$_files represents some of the things that need to be done before files//files are uploaded//1. Determine if the error//2. Control the format of the upload, the format should be strictly controlled in order to prevent incoming code class file attack database. 3. Control the size of the uploaded files, cannot upload large files, the general database will limit the size of the uploaded files. If you want to upload large files, you need to modify the PHP configuration file. 4. The control file name cannot be duplicated (if duplicate cannot be uploaded.)    )//How to make the file name not repeat//1. The file name is automatically generated, such as the name of the//2 with the filename + timestamp + random number. Using folders, each user uploads a file by using the program to build a folder. 3. Use the serial number is the most secure method, take the last file of the serial number and add 1.//5. Save Fileif(!$_files["File"] ["Error"])//determine if there is an error, if there are no errors. If 2 input is written in the form, it means that 2 files have been uploaded and the names of the files found in the array are taken out. {    if($_files["File"] ["Type"]== "Image/png" &&$_files["File"] ["Size"]<1024000)    //determine the type of file, from the type in the array to take. The file type is best to output the basic parameters of the file, from the parameters of the file type copied over.    Some filenames are hard to write. Also determine the size of the file    {        //$filename = "./file/";//To save the file to the target location, after the file name can be written, but more files are prone to problems. $filename = "./file/". $_files["File" ["name"];//so generally use the name of the file itself, after the storage location to spell the file name.         $filename= "./file/".Date("Ymdhis").$_files["File"] ["Name"]; $filename=Iconv("UTF-8", "gb2312",$filename); //The first parameter is the original encoding format, the second parameter is the encoding format to go to, and the third is the string to go to. Upload file encoding format for Utf-8, in the computer display format is GB, here to convert to the national standard encoding format.         if(!file_exists($filename))//determine if the file already exists        {            Move_uploaded_file($_files["File"] ["Tmp_name"],$filename);//upload the code, actually the file is already on the server, here just save the file.         }        Else        {            Echo"The file already exists"; }    }}Else{    Echo $_files["File"] ["Error"];//If there is an error, output a false message. }

3. Optimization of the Upload button

Here the selection of files can be converted to images, click on the image upload. Do a picture in the selection file, and then the Upload file button and the image size consistent, transparency is set to completely transparent, but to make the selection of files in the upper, so click on the image is actually clicked on the Select File button.

4. Upload the preview, upload the image preview, click Save or upload is the real upload.

Web pages and JS can only manipulate the server's files and cannot manipulate client files. The following is a double-pass Image preview processing code.


<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" " Xhtml1-transitional.dtd "><HTMLxmlns= ""><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Untitled Document</title></Head><Body><formID= "SC"Action= "yulanchuli.php"Target= "Hidden_frame"Method= "POST"enctype= "Multipart/form-data">    <DivID= "YL"style= "margin-left:0px; width:144px; height:170px; background-size:144px 170px; Background-image:url (images/ 2015042215293286617.jpg) ">        <inputtype= "File"name= "File"style= "width:144px; height:170px; float:left; opacity:0;"width= "146"onchange= "document.getElementById (' SC '). Submit ()"/>           </Div>    <inputtype= "button"value= "Save"/></form><iframestyle= "Display:none;"name= "Hidden_frame"ID= "Hidden_frame"></iframe></Body><Scripttype= "Text/javascript">//callback functionfunctionshowimg (aa) {varL=document.getElementById ("yl"); "URL ("+AA+")";}</Script></HTML>

Handling Page yulanchuli.php

<?PHP//Upload file Limit   if((($_files["File"] ["type"] = = "Image/gif") | | ($_files["File"] ["type"] = = "Image/jpeg") | | ($_files["File"] ["type"] = = "Image/png")) && ($_files["File"] ["Size"] < 10000000))  {      //File Upload error      if($_files["File"] ["error"] > 0)    {        Echo"Upload error:".$_files["File"] ["Error"]. "<br/>"; }      Else    {                //determine if the file already exists    $filename= "images/".$_files["File"] ["Name"]; $filename=Iconv("UTF-8", "gb2312",$filename); if(file_exists($filename))      {          Echo"<script language= ' JavaScript ' >alert (' File already exists! ') </script> "; }    Else      {          //move picture to save path, solve Chinese garbled problem            Move_uploaded_file($_files["File"] ["Tmp_name"],$filename); Echo"<script language= ' JavaScript ' >parent.showimg ('".$filename."‘) </script> "; }    }  }  Else  {      Echo"Upload Failed"; }?>

November 15 Morning File Upload

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: 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.