Mining PHP file Upload principles _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags php file upload
Mining PHP file Upload principles. We all know that PHP has powerful functions, which brings me a lot of convenience. let's take a look at the implementation of the PHP file Upload type. PHP supports file upload, but not all support. we all know that PHP has powerful functions, which brings me a lot of convenience. let's take a look at the implementation of the PHP file Upload type. PHP supports file upload, but not all websites that support PHP support this function, especially free websites.

To upload an object, you must first add the <FORM> Table of the object to be uploaded in HTML.

 
 
  1. <FormMethod=Post Action="Upload. php" ENCTYPE="Multipart/form-data">
  2. <InputType="File" Name="Upload_file">
  3. <InputType="Submit" Name="Submit" Value="Upload files">

When using PHP to upload a file type, you need to perform a detailed check on the content, such as whether to allow reading and writing files, the file format, and whether the file size is within your specified size.

 
 
  1. <?
  2. $File_size_max=1000000;
  3. // Limit the maximum file upload capacity (bytes)
  4. $Store_dir="/Public/www/upload /";
  5. // Storage location of uploaded files
  6. $Accept_overwrite=True;
  7. // Allow reading and writing files
  8. // Check the file size
  9. If ($ upload_file_size> $ file_size_max ){
  10. Echo "Sorry, your file capacity exceeds the limit ";
  11. Exit;
  12. }
  13. // Check the read/write file
  14. If (file_exists ($ store_dir. $ upload_file_name )&&&&! $ Accept_overwrite ){
  15. Echo "the file already exists and cannot be copied ";
  16. Exit;
  17. }
  18. // Copy the file to the specified directory
  19. If (! @ Copy ($ upload_file, $ store_dir. $ upload_file_name )){
  20. Echo "failed to copy the file ";
  21. Exit;
  22. }
  23. Echo "file uploaded ";
  24. ?>

It should be noted that during PHP file upload, the file is copied to the temporary directory (temp) on the server, and then the "copy () "function to copy files from the temporary directory to the storage directory you specified. Because the program uses a temporary directory for work, if the server blocks the above functions for security reasons, you cannot use the PHP Upload function. In addition, you also need to set the file mode to 777 (CHMOD 777) for the uploaded file directory, otherwise PHP will not have the permission to read and write the file.


Bytes. PHP supports file upload, but not all support...

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.