Home >Backend Development >PHP Problem >How to upload image and change file name in php
How to change the file name of uploaded images in php: 1. Create an html file for uploading; 2. Create an upload_file.php file, accept the uploaded file and save it to the path; 3. Go to "/project Just go to the "Address/upload" directory to see if there are pictures.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to upload pictures and change the file name in php? PHP uploads images and changes the file name
1, first create an html file for upload
<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html>
2. Now create an upload_file.php file, accept the uploaded file, and save it to the path
if ($_FILES["file"]["error"] > 0) {echo "Error: " . $_FILES["file"]["error"] . "<br />";}else {//获取数组里面的值 $img=$_FILES["file"]["name"];//上传文件的文件名 //获取当前时间为图片名 //$img= date( "YmdHis" ).'z139.cn'.$_FILES["file"]["name"]; //自定义文件名‘myname’,加上本来的文件名$_FILES["file"]["name"] $img= 'myname'.$_FILES["file"]["name"]; $type=$_FILES["file"]["type"];//上传文件的类型 $size=$_FILES["file"]["size"];//上传文件的大小 $tmp_name=$_FILES["file"]["tmp_name"];// 上传文件的临时存放路径 //就是存放的位置更改 $save_path =$_SERVER ['DOCUMENT_ROOT'].'/upload/'; //暂时没发现有什么用 $save_url = '/statics/uploads/'.'123'.$img;} if (! is_dir ( $save_path )) {@mkdir ( $save_path, 0777 );} //保存图片到路径 move_uploaded_file($tmp_name, $upload_path=$save_path .$img);
3. Then go to the /project address/upload directory to see if there are pictures
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to upload image and change file name in php. For more information, please follow other related articles on the PHP Chinese website!