PHP File Upload processing case detailed

Source: Internet
Author: User
This article mainly introduces the PHP file upload processing method, combined with the example form analysis of PHP file upload operation encountered problems and related solutions, the need for friends can refer to the following

This article describes the php file upload processing methods. Share to everyone for your reference, as follows:

Recently encountered a thing, put yourself in the pit for a long time, I want to talk about the idea I started

PHP upload mechanism encapsulation is very complete, the basic few lines of code can be achieved, his implementation process is this

Upload to File to temp directory –> use Move_uploadde_file () to the specified directory

This is the PHP upload process, or you can do some verification in the middle. For example, whether the document is submitted by upload or if the extension of the file is not allowed by us

And so on a series of validations. I gave the simple code a call.

$targetFolder = '/uploads '; Define the root directory if (!empty ($_files)) {  $tempFile = $_files[' Filedata ' [' Tmp_name '];if (Is_uploaded_file ($tempFile)) {  $targetPath = $_server[' Document_root '). $targetFolder;  $targetFile = RTrim ($targetPath, '/'). '/' . $_files[' Filedata ' [' name '];  $fileTypes = array (' jpg ', ' jpeg ', ' gif ', ' PNG '); Allowed suffix extension  $fileParts = pathinfo ($_files[' Filedata ' [' name ']);  if (In_array ($fileParts [' extension '], $fileTypes)) {    move_uploaded_file ($tempFile, $targetFile);    echo ' 1 ';  } else {    echo ' illegally uploaded the document. ';  }} else{  echo "illegally uploading files";}}

The above approach basically satisfies the file upload. But that's not what I need.

Upload function

Requirements: First click Upload Document, and then select Upload file, JS upload will return a value, display the current time stamp asked the name of the file in the document name this box.

Then click Submit and submit the form.

The server processes the submitted form and renames the uploaded file.

The problem also follows, the server is IIS, and then it is implemented in a mapped way with the virtual directory.

In this case $_server[' document_root ' is not in the root directory of the PHP site

Although the upload function was completed, the file could not be downloaded. From the server security considerations, or to put the files in the PHP site.

Then I was trapped in a mental limit.

Think again:

Why I don't need to implement the PHP upload mechanism in a way that repeats once.

Isn't php putting files in a temp directory first? And then I assume that the directory after the successful JS upload is also a temporary directory,

The file is then copied when the form is submitted again. To the specified directory, this completes the required functionality.

PHP has a file copy function copy (); Then, in conjunction with the rename () function. This allows you to complete the second move and rename of the file after uploading.

Note: If you copy a 0-byte file under the window platform, copy () will return FALSE, but the file will also be copied correctly.

Then PHP in the file processing, actively reference the Liunx file processing mechanism. PHP's file operation efficiency, which is related to I/O to write and operating system.

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.