PHP File Upload processing case analysis,

Source: Internet
Author: User
Tags upload php

PHP File Upload processing case analysis,

This document describes how to upload PHP files. We will share this with you for your reference. The details are as follows:

I have encountered a problem recently, and I have been in trouble for a long time. I want to talk about my initial ideas.

PHP's upload mechanism is completely encapsulated and can be implemented with just a few lines of code. Its implementation process is like this.

UPLOAD files to the temporary directory-> Use move_uploadde_file () to the specified directory

This is the PHP Upload process, or you can perform some verification in the middle. For example, determine whether a document is submitted by upload or whether the file extension is allowed.

And so on. The simple code I provided is a reference.

$ TargetFolder = '/uploads'; // defines 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 '); // extension of the allowed suffix $ fileParts = pathinfo ($ _ FILES ['filedata'] ['name']); if (in_array ($ fileParts ['extension'], $ fileTypes) {move_uploaded_file ($ tempFile, $ targetFile); echo '1';} else {echo 'upload the document illegally. ';}} else {echo "Illegal File Upload ";}}

The above method basically meets the requirements for file upload. But this is not what I need.

Upload function

Requirement: First Click Upload document, and then select Upload File. After JS is uploaded, a value is returned. The file with the name of the current timestamp is displayed in the Document Name box.

Click Submit to submit the form.

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

The problem also arises. The server is IIS, and then it is a virtual and directory implemented by ing.

In this case, $ _ SERVER ['document _ root'] is not in the ROOT directory of the PHP site.

Although the upload function is completed, the file cannot be downloaded. From the server's security considerations, I plan to put files in the PHP site.

Then I got stuck in a limited mindset.

Think about it again:

Why do I need to repeat the PHP Upload mechanism once.

Isn't PHP putting files in the temporary directory first? Then I assume that the directory after successful JS upload is also a temporary directory,

Then copy the file when submitting the form again. To the specified directory to complete the required functions.

PHP has a file replication function Copy (). Then, it works with the rename () function. In this way, you can complete the second movement and rename of the uploaded file.

Note: If you copy a zero-byte file on the window platform, copy () returns FALSE, but the file is also copied correctly.

Then, in terms of File Processing, PHP actively references the File Processing Mechanism of Liunx. PHP file operation efficiency, which is related to I/O writing and the operating system.

Conclusion: In fact, there are many ways to solve the problem, and do not draw circles for yourself to limit your own thinking.

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.