Using file function to implement PHP file upload case Analysis _php tutorial

Source: Internet
Author: User
Tags php file upload
If you are a computer enthusiast, do not know PHP words you are too outdated, if you want to know the knowledge of PHP, we will take a look at the php file upload implementation. This code is divided into two files, one for upload.html and one for upload.php.

 
 
  1. < form enctype="multipart/form-data" action=" upload.php " method=" POST ">
  2. < input type="hidden" name="max_file_size" value="100000">
  3. < input name="userfile" type="File" >
  4. < input type="Submit" value="Upload file">
  5. form>

Among them, please note that This is a label, we want to implement the file upload, must be specified as Multipart/form-data, otherwise the server will not know what to do. It is worth noting that the hidden domain value of the form option max_file_size in file upload.html can limit the size of uploaded files by setting their value. The value of max_file_size is just a suggestion to the browser, in fact it can be simply bypassed. Therefore, do not put restrictions on the browser to hope for this value. In fact, PHP settings in the php file upload maximum value, is not invalidated. But it's best to add max_file_size to the form because it avoids the hassle of having to wait for a large file to be uploaded before the user finds it too big.

 
   
  
  1. upload.php
  2. $ F =& $HTTP _post_files[' Myfile '];
  3. $ Dest_dir = ' uploads ' ;//Set Upload directory
  4. $ dest = $dest _dir. '/'. Date ("Ymd"). " _ ". $f [' name '];//set file name to date plus file name to avoid duplication
  5. $ R = Move_uploaded_file ($f [' Tmp_name '], $dest);

Or

 
   
  
  1. copy($_files[myfile][tmp_name],$_files[myfile][name]); ?>

The contents of the $_files array in the example above are shown below. We assume that the name of the File upload field is UserFile (name can be arbitrarily named)

 
   
  
  1. $_files[' UserFile ' [' Name ']//the original name of the client machine file.
  2. The MIME type of the $_files[' userfile ' [' type ']//file requires the browser to provide support for that information, such as "Image/gif".
  3. $_files[' userfile ' [' Size ']//the size of the uploaded file, in bytes.
  4. $_files[' UserFile ' [' tmp_name ']//files are uploaded after the temporary file name stored on the server.

PHP File Upload value analysis:
Value: 0; No error occurred and the file upload was successful.
Value: 1; The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini.
Value: 2; The size of the uploaded file exceeds the value specified by the Max_file_size option in the HTML form.
Value: 3; Only part of the file is uploaded.
Value: 4; No files were uploaded.


http://www.bkjia.com/PHPjc/446562.html www.bkjia.com true http://www.bkjia.com/PHPjc/446562.html techarticle If you are a computer enthusiast, do not know PHP words you are too outdated, if you want to know the knowledge of PHP, we will take a look at the php file upload implementation. This piece of code is divided into ...

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