Home  >  Article  >  Backend Development  >  PHP file upload processing case analysis

PHP file upload processing case analysis

高洛峰
高洛峰Original
2016-12-20 15:24:21988browse

The example in this article describes the method of PHP file upload processing. I would like to share it with you for your reference. The details are as follows:

I recently encountered something that troubled me for a long time. I want to share my initial thoughts.

PHP’s upload mechanism is fully encapsulated and can be implemented with just a few lines of code. , his implementation process is like this

UPLOAD the file 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 the document is submitted through upload, or whether the file extension is allowed by us

and a series of verifications. The simple code I gave can be regarded as a starting point.

$targetFolder = '/uploads'; // 定义根目录
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'); // 允许的后缀扩展
  $fileParts = pathinfo($_FILES['Filedata']['name']);
  if (in_array($fileParts['extension'],$fileTypes)) {
    move_uploaded_file($tempFile,$targetFile);
    echo '1';
  } else {
    echo '非法上传文档.';
  }
}else
{
  echo "非法上传文件";
}
}

The above method basically meets the requirements for file upload. But that's not what I need.

PHP file upload processing case analysis

Upload function

Requirements: First click to upload the document, and then select the upload file. After JS is uploaded, a value will be returned, showing the file with the current timestamp name in the document name box.

Then 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 it is a virtual directory implemented using mapping.

In this case, $_SERVER['DOCUMENT_ROOT'] is not in the root directory of the PHP site

Although the upload function is completed, this file cannot be downloaded. For server security reasons, I still plan to place the file in the PHP site.

Then I fell into a thinking limitation.

Think about it again:

Why don’t I need to implement PHP’s upload mechanism again?

Don’t PHP put the files in the temporary directory first? Then why do I assume that the directory after the JS is uploaded successfully is also a temporary directory,

and then copy the file when submitting the form again. Go to the specified directory to complete the required function.

PHP has a file copy function Copy(); and then cooperates with the rename() function. In this way, the second move and renaming of the uploaded file can be completed.

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

Then PHP actively refers to Liunx’s file processing mechanism in file processing. PHP's file operation efficiency is related to I/O writing and the operating system.

Summary: In fact, there are many ways to solve problems, and don’t draw circles for yourself to limit your thinking.

I hope this article will be helpful to everyone in PHP programming.

For more articles related to PHP file upload processing case analysis, please pay attention to 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