How to use multiple file upload and file handling functions in PHP?

WBOY
Release: 2023-07-25 15:36:01
Original
842 people have browsed it

How to use multiple file upload and file processing functions in PHP?

File uploading is one of the common functions in web development, and it is very common to implement multiple file uploads in PHP. This article will introduce how to use PHP's file upload and file processing functions to implement the multi-file upload function, and comes with code examples.

  1. Create HTML form

First, we need to create a form in HTML for users to select and upload files. This form needs to set the enctype attribute to "multipart/form-data" to support file upload.

<form action="upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="files[]" multiple>
  <input type="submit" value="上传">
</form>
Copy after login

Among them, the type attribute of the input tag is set to "file", the name attribute is set to "files[]", and the multiple attribute is set to allow the user to select multiple files.

  1. Processing file uploads

In PHP, we need to write a script that handles file uploads. We can access the uploaded files through the $_FILES global variable.

<?php
if(isset($_FILES['files'])){
  $errors = [];
  $upload_dir = 'uploads/';

  // 循环处理每个上传的文件
  foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){
    $file_name = $_FILES['files']['name'][$key];
    $file_size = $_FILES['files']['size'][$key];
    $file_tmp = $_FILES['files']['tmp_name'][$key];
    $file_type = $_FILES['files']['type'][$key];
    
    // 检查文件类型,限制为图片文件
    $allowed_types = ['image/jpeg', 'image/png', 'image/gif'];
    if(!in_array($file_type, $allowed_types)){
      $errors[] = '文件类型不被允许';
      continue;
    }

    // 移动上传的文件到指定目录
    if(move_uploaded_file($file_tmp, $upload_dir . $file_name)){
      echo '文件上传成功: ' . $file_name . '<br>';
    }else{
      $errors[] = '文件上传失败: ' . $file_name;
    }
  }

  // 输出错误信息,如果有的话
  if(!empty($errors)){
    echo '上传出现以下错误:<br>';
    foreach($errors as $error){
      echo $error . '<br>';
    }
  }
}
?>
Copy after login

In this code, we first check the uploaded file type, only image files are allowed. Then, we use the move_uploaded_file() function to move the uploaded file to the specified directory. If the move is successful, the file name will be output and displayed on the browser using the echo statement. If any errors occur, an error message will be output.

  1. File Handling

Once we have successfully uploaded the files, we can use PHP's file handling functions to process the files. Here are some common examples of file processing functions:

<?php
// 获取上传文件的个数
$file_count = count($_FILES['files']['name']);

// 循环处理每个上传的文件
for($i = 0; $i < $file_count; $i++){
  $file_name = $_FILES['files']['name'][$i];
  $file_path = $upload_dir . $file_name;
  
  // 获取文件扩展名
  $file_extension = pathinfo($file_path, PATHINFO_EXTENSION);
  echo '文件扩展名: ' . $file_extension . '<br>';

  // 获取文件大小
  $file_size = filesize($file_path);
  echo '文件大小: ' . $file_size . '字节<br>';

  // 重命名文件
  $new_file_name = 'new_' . $file_name;
  $new_file_path = $upload_dir . $new_file_name;
  rename($file_path, $new_file_path);
  echo '文件重命名成功: ' . $new_file_name . '<br>';

  // 删除文件
  unlink($new_file_path);
  echo '文件删除成功: ' . $new_file_name . '<br>';
}
?>
Copy after login

In this code, we use several file processing functions. The pathinfo() function is used to obtain the file extension, the filesize() function is used to obtain the file size, the rename() function is used to rename the file, and the unlink() function is used to delete the file.

To sum up, this article introduces how to implement multi-file upload and file processing functions in PHP. From creating HTML forms to handling file uploads to processing using file handling functions, we explain the entire process in detail with code examples. I hope this article can provide you with some help in using PHP for file uploading and processing in web development.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template