Home  >  Article  >  Backend Development  >  How to modify the saving path of PHP uploaded files

How to modify the saving path of PHP uploaded files

PHPz
PHPzOriginal
2023-04-04 10:41:181542browse

PHP, as a server-side scripting language, is widely used in Web development. During the development process of web applications, it is often necessary to upload some files, such as pictures, documents, etc. PHP provides a convenient file upload function, but the uploaded files are stored in the temporary directory of the server by default, which is not conducive to file management and application.

Therefore, many web developers need to modify the saving path of PHP uploaded files so that the uploaded files can be stored in the specified directory. This article mainly introduces how to modify the saving path of PHP uploaded files, as well as some issues that need to be paid attention to when modifying the path.

1. Set the upload directory of PHP

In PHP, the path to save the uploaded file is controlled by the variable $_FILES['userfile']['tmp_name'] of. By default, this variable points to the temporary upload directory on the PHP server. The specific path is usually /tmp or /var/tmp. In order to save the uploaded file to the specified directory, you need to set the upload_tmp_dir variable to specify the directory path for file upload.

There are two specific setting methods:

  1. Set in the PHP configuration file php.ini.

Find the following two parameters:

upload_tmp_dir = /path/to/upload/dir
upload_max_filesize = 2M

Among them, upload_max_filesize is the maximum size of the specified file upload, in bytes. The above code indicates that the maximum size of the uploaded file is 2M.

  1. Use ini_set function setting in PHP code.

If you do not have permission to modify the php.ini configuration file, you can use the ini_set function in the PHP code to dynamically set the upload directory path. For example:

ini_set('upload_tmp_dir', '/path/to/upload/dir');
ini_set('upload_max_filesize', '2M');

These codes will set the PHP upload path to /path/to/upload/dir and limit the maximum size of the uploaded file to 2M.

2. Set the target path of the uploaded file

After setting the upload directory, PHP will store the uploaded file in the specified directory. However, so far, the final destination path for uploaded file storage has not been specified. This section explains how to set the destination path for uploaded files.

  1. Use the move_uploaded_file function

In PHP, you can use the built-in function move_uploaded_file to move the uploaded file to the specified target path. move_uploaded_file The function accepts two parameters, which are the temporary path and target path of the uploaded file. For example:

$uploadfile = '/path/to/upload/dir/' . basename($_FILES['userfile']['name']);
 
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "文件已经被成功上传!";
} else {
    echo "上传失败!";
}

The above code first defines the temporary directory for uploading files, and uses the move_uploaded_file function to move the uploaded files to the specified target path. In this way, the file can be saved in the specified directory with the specified file name.

  1. Check the type and size of the uploaded file

In order to avoid uploading illegal files and large files, we should also check the type and size of the uploaded file during the upload process. This can be achieved by using PHP's predefined variable $_FILES. For example:

$uploadfile = '/path/to/upload/dir/' . basename($_FILES['userfile']['name']);
 
$allowedExtensions = ['jpg', 'png', 'gif'];
$maxSize = 1000000;
 
if (in_array(pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION), $allowedExtensions)
    && $_FILES['userfile']['size'] <= $maxSize
    && move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
 
    echo "文件已经被成功上传!";
} else {
    echo "上传失败!";
}

In the code, we set the file types and file sizes that are allowed to be uploaded, and filtered accordingly. If the uploaded file type and size meet the requirements, it will be saved to the specified path.

3. Notes

  1. When setting the upload directory, please ensure the read and write permissions of the directory, otherwise files cannot be created in the directory.
  2. When setting the upload directory, it is best to use an absolute path to avoid path errors.
  3. The file size and type should be checked when uploading files to avoid uploading illegal files or large files that take up too many server resources.
  4. When uploading files, do not trust the uploaded file name and type. It's best to use SHA1 or a random string to rename files to prevent filename collisions and security issues.

Summary

This article introduces how to modify the saving path of PHP uploaded files and set the target path of uploaded files. During the specific implementation process, some precautions need to be followed to ensure the stability and security of file upload. I hope the above content can be helpful to everyone’s web application development!

The above is the detailed content of How to modify the saving path of PHP uploaded files. 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