Home > Article > Backend Development > How to set the PHP file upload save path
PHP is a very popular server-side scripting language that can be used to develop various types of web applications. Among them, the file upload function is a very common requirement, but during the file upload process, we need to set the file upload save path so that we can easily find and manage the uploaded files in the future. In this article, we will introduce how to set the PHP file upload saving path.
Before setting the file upload path, we must ensure that we have installed the PHP programming language. We also need some basic server knowledge and web programming experience.
In PHP, we can use the "$_FILES" array to obtain the information of the uploaded file. By using the "move_uploaded_file" function, we can move the uploaded file to the specified directory and save it.
The following is a typical file upload code example:
<?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } ?>
In the above code, we first define a variable $target_dir
, which is used to store the uploaded file Table of contents. Next, we used the "basename" function to get the filename of the uploaded file and appended it to the file path. Finally, we used the move_uploaded_file
function to move the uploaded file to the target directory.
Please note that when using the move_uploaded_file
function, we need to pass it both the temporary path and the target path of the uploaded file. The function will return true if the file upload is successful, otherwise it will return false.
In addition to setting the file upload path in the PHP script, we can also set this option in the PHP.ini file. The advantage of this is that we can set the file upload path globally instead of having to set it in every script.
To modify the PHP.ini file, follow these steps:
In this article, we discussed how to set the saving path of uploaded files in PHP. You can set this in a PHP script using the move_uploaded_file function or globally in the PHP.ini file. Hope this article is helpful to you.
The above is the detailed content of How to set the PHP file upload save path. For more information, please follow other related articles on the PHP Chinese website!