PHP File Upload processing case Analysis _php skills

Source: Internet
Author: User
Tags file copy php file upload

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

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

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

Upload to a file in a temporary 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 the extension of the file is not what we allow

And so on a series of validations. I can give you some simple code.

$targetFolder = '/uploads '; Defines the root
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 '); The 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 "illegal upload file";
}
}

The above method 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 timestamp 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 comes with the server being IIS, and then it is implemented in a way that maps 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, it was not possible to download the file. From the security considerations of the server, still intend to put the file in the PHP site.

And then I got stuck in a mental conditioning.

Think again:

Why I do not have to implement the PHP upload mechanism in the way repeated.

Does PHP not put files in the temp directory first? And then I'm assuming that the directory that JS uploaded successfully is also a temporary directory,

Then make a copy of the file when you submit the form 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 completes 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, the positive reference to the Liunx file processing mechanism. PHP's file manipulation efficiency, which is related to I/O to write and operating system.

Summary: In fact, there are many ways to solve problems, and do not give yourself a circle to limit your thinking.

More about PHP Interested readers can view the site topics: "PHP file Operation Summary", "PHP graphics and pictures Operating skills summary", "PHP Array" operation Skills Encyclopedia, "PHP Basic Grammar Introductory Course", "PHP Operations and Operator Usage Summary", " Introduction to PHP object-oriented programming program, "PHP Network Programming Skills Summary", "PHP string (String) Usage Summary", "Php+mysql database Operation Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

Related Article

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.