Home  >  Article  >  Backend Development  >  How to upload files and rename them in php

How to upload files and rename them in php

PHPz
PHPzOriginal
2023-03-23 14:11:072077browse

PHP is a commonly used web development language. Many websites are developed and maintained using PHP, and one of the most common functions is file upload. In PHP, although the file upload process is relatively simple, sometimes you encounter situations where you need to change the name of the uploaded file.

This article will introduce how to upload files and change the name of uploaded files in PHP.

1. Upload files

In PHP, file upload uses the $_FILES super global array. This array contains information about the uploaded file, such as the name, size and type of the uploaded file.

The following is a simple file upload code example:

if(isset($_FILES['file'])){
    $file_name = $_FILES['file']['name'];
    $file_tmp_name = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    $file_error = $_FILES['file']['error'];

    //将上传的文件移动到指定目录
    move_uploaded_file($file_tmp_name, $file_name);
}

In the above code, first use the isset() function to check whether $_FILES['file'] is set, and then from the super global Get the detailed information of the uploaded file in the array, including file name, temporary file name, file size, file type and error information. Finally, use the move_uploaded_file() function to move the file from the temporary folder to the specified directory.

2. Change the name of the uploaded file

Suppose we need to rename the uploaded file to avoid file name conflicts or increase security. File name modification can be done using ready-made functions in PHP.

Function 1: rename()

You can use the rename() function to change the name of the uploaded file. The rename() function has two parameters, the original file name and the new file name.

if(isset($_FILES['file'])){
    $file_name = $_FILES['file']['name'];
    $file_tmp_name = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    $file_error = $_FILES['file']['error'];

    //将上传的文件移动到指定目录,并改变文件名称
    $new_name = "newfile.jpg";
    $new_path = "uploads/".$new_name;
    move_uploaded_file($file_tmp_name, $new_path);
    
    rename($new_path, "uploads/".rand(1000, 9999)."_".$new_name);
}

In the above code, first move the uploaded file to the specified directory and change the name of the uploaded file to newfile.jpg. Then, use the rename() function to change the file name to a random number with an underscore and the original file name newfile.jpg. Using random numbers and timestamps can avoid duplication of file names and improve the security of file uploads.

Function 2: move_uploaded_file()

When moving and uploading files, you can directly use the move_uploaded_file() function to change the file name.

if(isset($_FILES['file'])){
    $file_name = $_FILES['file']['name'];
    $file_tmp_name = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    $file_error = $_FILES['file']['error'];

    //修改上传文件的名称
    $new_name = "newfile.jpg";
    move_uploaded_file($file_tmp_name, "uploads/".rand(1000, 9999)."_".$new_name);
}

In the above code, first get the details of the uploaded file, then move the uploaded file to the specified directory, and change the file name to a random number plus an underscore and a new file name newfile.jpg.

Summary

In this article, we introduced two methods on how to upload a file in PHP and change the name of the uploaded file: using the rename() function and move_uploaded_file( )function. Regarding the security and stability of the file upload function, both methods have advantages and disadvantages, and you need to choose according to the actual situation. In any case, good coding practices should be followed to ensure the security and reliability of file uploads.

The above is the detailed content of How to upload files and rename them in php. For more information, please follow other related articles on 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