PHP Development notes series (8)-upload and download

Source: Internet
Author: User
PHP Development notes series (8)-upload and download ??? Recently, due to project requirements, GZBGY closed its doors and had in-depth exchanges with developers in the same industry. I dropped "PHP development Notes Series (XAMPP + PhpEclipse + XDebug)" and updated it today. this lecture is about uploading and downloading .???? Upload and download two PHP Development notes series (8)-upload and download

??? Recently, due to project requirements, GZBGY closed its doors and had in-depth exchanges with developers in the same industry. I dropped "PHP development Notes Series (XAMPP + PhpEclipse + XDebug)" and updated it today. this lecture is about uploading and downloading.

?

??? The upload and download functions are basic functions that are not normal. in java development, the download will use the File class to write the File content to the response in the form of a stream, set the corresponding header and http header so that the browser can identify this download. The same is true in PHP. in this article, "PHP Development notes series (8)-upload and download" will be the eighth article in "PHP development Notes Series (XAMPP + PhpEclipse + XDebug, describes how to use PHP to upload and download files.


???1. file download

??? The file download operation is divided into two parts, step: 1) set http header, 2) read the file. Here we write two php scripts, the first is the file-list.php, used to display all the 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 the enctype as "multipart/form-data" request, automatically uploads the content in the file domain to the tmp folder of php. we can go to $ _ FILES ["file domain name"] ["attribute"], for example, $ _ FILES ["file"] ["name"] indicates the original name of the uploaded file, $ _ FILES ["file"] ["type"] indicates the type of the uploaded file, $ _ FILES ["file"] ["tmp_name"] indicates the path of the uploaded file in the php temporary folder. Because the generated temporary file will be cleared, we need to use the move_uploaded_file () function to move the temporary file to our specified directory. The first parameter is the path of the temporary file, the second parameter is the path of the target 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";}?>
?

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