PHP Development Notes Series (eight)-Upload and download

Source: Internet
Author: User
Tags tmp folder
PHP Development Note Series (eight)-Upload and download

??? Recently, because of the project needs, in Gzbgy closed, with the peer developer in-depth exchanges, the results are not bad. Dropped the "PHP Development Note Series (Xampp+phpeclipse+xdebug)", updated today, this time to upload the download.

?

??? uploading and downloading two functions is a normal basic function, in Java development, the download will use the file class to write the contents of a stream into the response, and set the corresponding and HTTP header, let the browser recognize this is a file download. Similar in PHP, this article, "PHP Development Note Series (eight)-Upload and download" will be "PHP Development Note Series (XAMPP+PHPECLIPSE+XDEBUG)," the eighth chapter, how to use PHP to complete the file upload and download operations.


??? 1. File Download

??? The operation of the file download is divided into two parts, step: 1) to read the file by setting HTTP header,2. Here we write two PHP script, the first is file-list.php, used to display all folders and files under the current folder for download, the second is download.php, the specific download code. The code is as follows:

?

file:file-list.phpurl:http://localhost:88/download-upload/file-list.php
 
   ';        } ElseIf (Is_file ($entry))        {            echo ' [file] '. $entry. '
'; } } Closedir ($DP); echo "Upload";? >
?

?

File:download.phpurl:http://localhost:88/download-upload/download.php?filename=xxx
 
  
?

?? ? 2. File Upload

??? In PHP, file upload is very simple, because PHP has automatically recognized enctype as "multipart/form-data" request, automatically the contents of the inside of the file domain upload to the TMP folder in PHP, we can $_files["file domain name "[" property], such as $_files["file" ["name"] means the original name of the uploaded file, $_files["file" ["type"] means the type of the uploaded file, $_files["file"] ["Tmp_name"] Represents the path of the uploaded file in the PHP Temp folder. Because the generated temporary files are emptied, we need to use the Move_uploaded_file () function to move the temporary files to our specified directory, where the first parameter is the path to the temporary file, and the second parameter is the path to the destination file. The code is as follows:

?

file:upload-form.phpurl:http://localhost:88/download-upload/upload-form.php
?

?

file:upload.phpurl:http://localhost:88/download-upload/upload.php
 
   0) {    echo "Error:". $_files["File" ["Error"]. "
";} else{ echo "Upload:". $_files["File" ["Name"]. "
"; echo "type:". $_files["File" ["Type"]. "
"; echo "Size:". ($_files["File" ["Size"]/1024). " Kb
"; echo "Temp file:". $_files["File" ["Tmp_name"]. "
"; Move_uploaded_file ($_files["file"] ["Tmp_name"], $_files["file" ["name"]); echo "File-list";}? >
?

?? This address: http://ryan-d.iteye.com/blog/1546706

?

?

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