How to upload image and change file name in php

藏色散人
Release: 2023-03-10 14:50:01
Original
2247 people have browsed it

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.

How to upload image and change file name in php

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>
Copy after login

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" ).&#39;z139.cn&#39;.$_FILES["file"]["name"];
    //自定义文件名‘myname’,加上本来的文件名$_FILES["file"]["name"]
    $img= &#39;myname&#39;.$_FILES["file"]["name"];
    $type=$_FILES["file"]["type"];//上传文件的类型
 
    $size=$_FILES["file"]["size"];//上传文件的大小
 
    $tmp_name=$_FILES["file"]["tmp_name"];// 上传文件的临时存放路径
    //就是存放的位置更改
    $save_path =$_SERVER [&#39;DOCUMENT_ROOT&#39;].&#39;/upload/&#39;;
    //暂时没发现有什么用
    $save_url = &#39;/statics/uploads/&#39;.&#39;123&#39;.$img;}
 
if (! is_dir ( $save_path )) {@mkdir ( $save_path, 0777 );}
//保存图片到路径
move_uploaded_file($tmp_name, $upload_path=$save_path .$img);
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template