Home >Backend Development >PHP Tutorial >How to handle file upload and download in PHP development?

How to handle file upload and download in PHP development?

WBOY
WBOYOriginal
2023-11-02 15:02:001442browse

How to handle file upload and download in PHP development?

How to handle file upload and download in PHP development?

File uploading and downloading is one of the common functions in web applications. In PHP development, handling file uploads and downloads is relatively simple, just use the built-in functions and specific file operation functions provided by PHP. This article will introduce in detail the methods and precautions on how to handle file upload and download in PHP development.

1. File upload

  1. Add a file upload field in the HTML form
    In the HTML form, use the tag to add a file upload field. For example, to upload an image file, you can add the following HTML code:

    <form action="upload.php" method="post" enctype="multipart/form-data">
     <input type="file" name="image">
     <input type="submit" value="上传">
    </form>
  2. Receive and process files on the server side
    On the server side, you need to write a PHP script to receive and processing uploaded files. First, use the $_FILES superglobal variable to check whether there is a file uploaded and obtain the uploaded file information. For example, to get uploaded image file information, you can use the following PHP code:

    $uploadedFile = $_FILES['image'];
    $fileName = $uploadedFile['name'];
    $fileType = $uploadedFile['type'];
    $fileSize = $uploadedFile['size'];
    $tmpFilePath = $uploadedFile['tmp_name'];
  3. Verify file type and size
    After receiving the uploaded file, the file type and size should be verified size to ensure that the uploaded file meets the requirements. This can be verified using the relevant built-in functions for file type and file size. For example, to verify whether the uploaded file type is an image, you can use the following PHP code:

    $allowedTypes = array('image/jpeg', 'image/png', 'image/gif');
    if (!in_array($fileType, $allowedTypes)) {
     echo '只允许上传JPEG、PNG或GIF格式的图片文件';
     return;
    }
    
    $maxFileSize = 10 * 1024 * 1024; // 最大文件大小为10MB
    if ($fileSize > $maxFileSize) {
     echo '文件大小超过了允许的最大限制';
     return;
    }
  4. Move the uploaded file to the target location
    After completing the file type and size verification, the upload will Move the files to the specified destination location. This can be achieved using the move_uploaded_file() function. For example, move the uploaded image file to the specified directory:

    $uploadDir = 'uploads/';
    $targetFilePath = $uploadDir . $fileName;
    if (move_uploaded_file($tmpFilePath, $targetFilePath)) {
     echo '文件上传成功';
    } else {
     echo '文件上传失败';
    }

2. File download

  1. Provide file download link
    In File download links are provided on the web page. You can use the tag to create a download link. For example, to provide a link to download a PDF file named "file.pdf", you can use the following HTML code:

    <a href="download.php?filename=file.pdf">点击下载文件</a>
  2. Handling the download request on the server side
    On the server On the client side, you need to write a PHP script to handle the download request. First, get the file name to be downloaded:

    $filename = $_GET['filename'];
  3. Set HTTP header information
    When processing the download request, you need to set the HTTP header information to send the file to the browser for downloading . First, set the Content-Type header information and specify the MIME type of the file to be downloaded. For example, to download a PDF file, you can use the following PHP code:

    header('Content-Type: application/pdf');
  4. Open the file and output it to the browser
    After setting the Content-Type, open the file to download , and output the file contents to the browser. This can be achieved using some specific file operation functions. For example, to download a PDF file named "file.pdf", you can use the following PHP code:

    $filePath = 'uploads/' . $filename;
    $handle = fopen($filePath, 'rb');
    if ($handle) {
     while (!feof($handle)) {
         echo fread($handle, 4096);
     }
     fclose($handle);
    }

Through the above steps, you can process file uploads in PHP development and download functions. In actual applications, some improvements and extensions can also be made according to specific needs, such as adding more uploaded file verification rules and implementing functions such as file lists and multi-file downloads.

The above is the detailed content of How to handle file upload and download in PHP development?. For more information, please follow other related articles on the PHP Chinese website!

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