Experience: example PHP file upload code _ PHP Tutorial

Source: Internet
Author: User
Tags php file upload
Experience: sample PHP file upload code. Today I read a book about PHP and learned how to upload files in PHP. The two main functions are move_uploade_file (temporary file, target location and file name) I read a book about PHP today with is_uploaded_fi and learned how to upload files in PHP. The two main functions are move_uploade_file (temporary file, target location and file name) and is_uploaded_file (). The former is used to move the files stored in the cache area of the server to the target file, and the latter is used to determine whether the files are uploaded successfully. In addition to the above two functions, we also need to explain that the value of enctype in the form label should be as follows:

 
 
  1. <formenctypeformenctype="multipart/form-data"method="post"name="upform">

Only multipart/form-data can ensure that files are uploaded in the correct encoding mode. "File" in the type attribute of the input tag"

 
 
  1. <inputnameinputname="upfile"type="file">

Another system function is $ _ FILES, $ _ FILES ['myfile'] ['name'] original name of the client file, $ _ FILES ['myfile'] ['type'] MIME type of the file, for example, "image/gif", $ _ FILES ['myfile'] ['size'] size of the uploaded file, in bytes, $ _ FILES ['myfile'] ['tmp _ name'] stores temporary file names, this is generally the system default, $ _ FILES ['myfile'] ['error'] error code related to the file upload. This function divides the information of the uploaded file into arrays and stores them in different array elements. for example, the file name value is stored in $ _ FILES ['myfile'] ['name. The following is a simple PHP file upload code written by myself:

PHP file upload code class saveupload. php

 
 
  1. Php
  2. If (is_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name']) {
  3. $Upfile= $ _ FILES ["upfile"]; // If you have selected the file to be uploaded, save its index in $ upfile
  4. // Upload the object names and types respectively.
  5. $Name= $ Upfile ["name"];
  6. $Type= $ Upfile ["type"];
  7. $Size= $ Upfile ["size"];
  8. $Tmp_name= $ Upfile ["tmp_name"];
  9. $Error= $ Upfile ["error"];
  10. // Set the Upload file type
  11. Switch ($ type ){
  12. Case 'image/pjpeg ':
  13. $OK=1;
  14. Break;
  15. Case 'image/jpeg ':
  16. $OK=1;
  17. Break;
  18. Case 'image/png ':
  19. $OK=1;
  20. Break;
  21. Case 'image/GIF ':
  22. $OK=1;
  23. Break;
  24. }
  25. // If the file type is valid and the value of $ error is 0, the upload is successful.
  26. If ($ OK & $Error= '0 '){
  27. Move_uploaded_file ($ tmp_name, 'up/'. $ name); // Move the files stored in the cache to the specified directory
  28. Echo "uploaded successfully ";
  29. }
  30. }
  31. ?>

PHP upload file code upload page upload. php

 
 
  1. >
  2. <HtmlxmlnsHtmlxmlns=Http://www.w3.org/1999/xhtml">
  3. <Head>
  4. <Metahttp-equivMetahttp-equiv= "Content-Type"Content= "Text/html;Charset=Utf-8"/>
  5. <Title>Upload Title>
  6. <StyletypeStyletype="Text/css">

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.