Home >Backend Development >PHP Tutorial >Implementing file upload function in PHP_PHP tutorial

Implementing file upload function in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:31:23926browse


 php(as the current mainstream development language) also supports the function of uploading files, but not all websites that support php(as the current mainstream development language) Support this feature, especially for free websites.
 
To achieve uploading, you must first add the "

" form for uploading files to the HTML.
 
 (as the current mainstream development language)" ENCTYPE="multipart/form-data">
   
Use php(as the current Mainstream development languages) When uploading, you need to check the content in detail, such as whether the file is allowed to be read and written, whether the file format and file size are within the size you specified, etc.
 
  $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 file size
if ($upload_file_size > $file_size_max) {
echo "Sorry, Your file capacity is larger than specified";
 exit;
 }
 //Check the read and write files
 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 "File copying failed";
exit;
}
echo "File upload completed";
? The current mainstream development language)
when uploading files is to copy the file to the server temporary directory (temp), and then use the "copy()" of php
(as the current mainstream development language) The function copies files from the temporary directory to the storage directory you specify. Since the program will use a temporary directory to work, if the server blocks the above functions due to security concerns, you will not be able to use the upload function of php(as the current mainstream development language).   In addition, the uploaded file directory also needs to set the file mode to 777 (CHMOD 777), otherwise php (as the current mainstream development language)
will not have the right to read and write files.

http://www.bkjia.com/PHPjc/509087.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/509087.htmlTechArticlephp (as the current mainstream development language) also supports the function of uploading files, but not all support php ( As the current mainstream development language) websites all support this feature, especially free...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn