


Detailed example of how to redefine the name of an uploaded file
When uploading files, you need to specify the upload file path. However, if you face different files uploaded by a large number of users, it is inevitable that there will be the possibility of duplicate names. This will cause file replacement, resulting in file loss, so the upload file Name redefinition is very important. This chapter will take you through it. How to define the name of the uploaded file?
First we use the strstr() function to intercept the name of the uploaded file and obtain the suffix of the uploaded file.
Then obtain the current timestamp of the system through the time() function and redefine the name of the uploaded file
Use the move_uploaded_file() function to upload the file.
Before executing file upload, in order to prevent potential attacks and illegal management of files that cannot be interacted with through scripts, the is_uploaded_file() function should be used to determine whether the specified file was uploaded through HTTP POST. If so, then returns a true.
is_uploaded_file() function
is_uploaded_file() function determines whether the specified file is uploaded through HTTP POST. Its syntax is as follows:
is_uploaded_file(filename)
Parameter filename A variable similar to $_FILES['filename']['tmp_name'] must be specified. The file name uploaded from the client cannot be used.$_FILES['filename']['name']
Using the is_uploaded_file() function to judge uploaded files can ensure that malicious users cannot trick scripts into accessing inaccessible files, such as /etc/passwd.
move_uploaded_file() function
The move_uploaded_file() function uploads a file to the specified location in the server. Returns true if successful. Otherwise, return false, and the syntax is as follows
move_uploaded_file(filename,destination)
The parameter filename specifies the temporary file name of the uploaded file, that is, $_FILES[tmp_name]; the parameter destination refers to the new path and name saved after the file is uploaded. If the parameter filename is not a legal uploaded file, no operation will occur,
move_uploaded_file() will return false; if the parameter filename is a legal uploaded file, but for If it cannot be moved for some reason, no operation will occur. move_uploaded_file() will return False and a warning message will be issued.
Finally use the rand function and use the random integer it randomly generates as the new name of the uploaded file.
Application Example
This example redefines the name of the uploaded file, select the name of the file to be uploaded, and click "Upload" button, the file will be uploaded to the upfile folder under the local path specified in the program, and the file name will be redefined.
##The key code for this example is as follows:
<?PHP header("content-type:text/html;charset=utf-8"); if(!empty($_FILES[up_picture][name])){ //判断上传文件是否为空 if($_FILES['up_picture']['error']>0){ //判断文件是否可以上传到服务器 echo "上传错误"; switch($_FILES['up_picture']['error']){ case 1: echo "上传文件大小超出配置文件规定值"; break; case 2: echo "上传文件大小超出表单中约定值"; break; case 3: echo "上传文件不全"; break; case 4: echo "没有上传文件"; break; } }else{ if(!is_dir("./upfile/")){ //判断指定目录是否存在 mkdir("./upfile/"); //创建目录 } $path='./upfile/'.time().strstr($_FILES['up_picture']['name'],'.');//定义文件名称和存储位置 if(is_uploaded_file($_FILES['up_picture']['tmp_name'])){ //判断是否通过HTTP POST上传的 if(!move_uploaded_file($_FILES['up_picture']['tmp_name'],$path)){ //执行上传 echo "上传失败"; }else{ echo "文件".time().$_FILES['up_picture']['name']."上传成功,大小为:".$_FILES['up_picture']['size']; } }else{ echo "上传文件".$_FILES['up_picture']['name']."不合法!"; } } } ?>
Defining the name of the uploaded file must be an indispensable function in the file upload program. Through it, it can avoid the replacement of files with the same name, which may lead to the existence of many files with the same content. For more information, please pay attention to PHP file processing topic
PHP file upload-detailed explanation of the use of the move_uploaded_file() function
The above is the detailed content of Detailed example of how to redefine the name of an uploaded file. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In PHP applications, we sometimes need to save or upload files using the current date as the file name. Although it is possible to enter the date manually, it is more convenient, faster and more accurate to use the current date as the file name. In PHP, we can use the date() function to get the current date. The usage method of this function is: date(format, timestamp); where format is the date format string, and timestamp is the timestamp representing the date and time. If this parameter is not passed, it will be used

Tips for dealing with garbled Chinese file names in PHP During the development process, we often encounter the problem of garbled Chinese file names, especially when processing files uploaded by users. In PHP, how to correctly handle garbled file names is a common and important problem. This article will introduce some techniques for dealing with garbled Chinese file names and provide specific code examples to help readers better deal with this challenge. Problem description: When users upload files, the Chinese file names sometimes appear to be garbled. This is because different operating systems and browsers

1. First find the file or folder that you need to delete. Because the file name is too long, the system will prompt when performing the deletion operation, [The file or directory cannot be deleted because the file name is too long]; as shown in the figure: 2 . At this time, use the shortcut key [Win+R] to open Run, enter [cmd], and open the command prompt after confirmation; as shown in the figure: 3. First switch to the disk where the file is located, mine is in D, enter [D:] , and then switch to the folder where the file is located. Mine is in [1], enter [cd1], and then enter [dir] to see all the files in the directory. In my directory, there is only one file that needs to be deleted. ;As shown in the figure: 4. Enter [del*.png] at this time, that is, all files with the suffix png in the directory will be

Use the path/filepath.Split function to split the path into two parts: directory and file name. When developing programs or processing files, it is often necessary to split the path into two parts: directory and file name. In Go language, you can use the Split function in the path/filepath package to achieve this function. This article will introduce the usage of the Split function and give code examples. The Split function is defined as follows: funcSplit(pathstring)(di

What should I do if the file name turns blue in win7 system? The win7 system is currently the most popular Windows system. It has a very long history of development and is deeply loved by users. However, the win7 system also has problems, big and small. When many friends use the win7 system, they will find that the file names on the computer turn blue, and other newly created folders will also turn blue. This is What is the situation and how to solve it? Below, the editor will bring you a method to restore the win7 system file name to blue. Recovery method of win7 system file name turning blue 1. First, right-click the disk with blue file name and open properties. 2. Find the option to compress this drive to save disk space in the properties window and remove it.

Java is a popular programming language with powerful file handling capabilities. In Java, traversing a folder and getting all file names is a common operation, which can help us quickly locate and process files in a specific directory. This article will introduce how to implement a method of traversing a folder and getting all file names in Java, and provide specific code examples. 1. Use the recursive method to traverse the folder. We can use the recursive method to traverse the folder. The recursive method is a way of calling itself, which can effectively traverse the folder.

Methods for batch modifying file names include using the mv command, using the rename command, using the find command combined with the mv command, using the mmv command, etc. Detailed introduction: 1. Use the mv command. The mv command is a command used to move files or rename files in the Linux system. Through the mv command, you can move a file to another location and modify the file name at the same time; 2. Use rename command, the rename command is a powerful batch file renaming tool, which can match and replace file names through regular expressions, etc.

Sometimes, when some friends want to download HP drivers, they may not know the name of the driver file, so they do not know which one to download. In fact, the name of the driver is different according to different hardware devices. Below, let’s learn more about it together. What are HP driver file names? Different hardware devices will have an impact on the file names, causing them to have different names on different devices. This is caused by different operating systems, file systems, or file naming conventions. For example, if we create a file and name it "my_file.txt" on Windows operating system, then it may be named "my_file.txt" or "myfil" on Mac operating system
