Upload files in PHP

Source: Internet
Author: User
Article title: implements file upload in PHP. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
PHP also supports file upload, but not all websites that support PHP support this function, especially free websites.
  
To implement the upload, you must first add the"   
           
When uploading files using PHP, 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 the size you specified.
  
   $ File_size_max = 1000000;
// Limit the maximum file upload capacity (bytes)
$ Store_dir = "/public/www/upload /";
// Storage location of uploaded files
$ Accept_overwrite = true;
// Allow reading and writing files
// Check the file size
If ($ upload_file_size> $ file_size_max ){
Echo "Sorry, your file capacity exceeds the limit ";
Exit;
}
// Check the read/write file
If (file_exists ($ store_dir. $ upload_file_name )&&&&! $ Accept_overwrite ){
Echo "the file already exists and cannot be copied ";
Exit;
}
// Copy the file to the specified directory
If (! @ Copy ($ upload_file, $ store_dir. $ upload_file_name )){
Echo "failed to copy the file ";
Exit;
}
Echo "file uploaded ";
?>
  
Note that PHP copies the file to the temporary directory (temp) on the server when uploading the file, and then uses PHP's "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.
  
Related Article

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.