简体   繁体   English

在上传PHP时重命名文件

[英]Rename file on upload PHP

I am trying to build a script to upload and rename an image to a folder and store the path in my sql db. 我正在尝试构建一个脚本来上传图像并将其重命名为文件夹并将路径存储在我的sql db中。

Here is where I am at: The file get uploaded both to the folder and the pathname to the db but I cannot figure out how to rename the filename. 这就是我所处的位置:文件上传到文件夹和db的路径名,但我无法弄清楚如何重命名文件名。 Ideally I would like to make the filename unique so I don't duplicates. 理想情况下,我想使文件名唯一,所以我不重复。

<?php   
//preparing the patch to copy the uploaded file
$target_path = "upload/";

//adding the name of the file, finishing the path
$target_path = $target_path . basename( $_FILES['picture']['name']); 

//moving the file to the folder
if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path)) {
   echo "The file ".  basename( $_FILES['picture']['name']). 
   " has been uploaded";
} else{
  echo "There was an error uploading the file, please try again!";
}

//getting input from the form
$name = $_POST['name'];
$description = $_POST['description'];

//preparing the query to insert the values
$query = "INSERT INTO complete_table (name, description, picture) VALUES ('$name', '$description', '". $target_path ."')";

//opening connection to db
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}

 //selecting a db
mysql_select_db("wcs_venues", $link) or die(mysql_error());

//running the query
$result = mysql_query($query) or die (mysql_error());

//closing the connection
mysql_close($link);

?>

I am new with all this and I am really trying but after looking at many tutorial and answered questions on Stack-overflow, I realized I needed help. 我对这一切都是新手,我真的很努力但是在看了很多教程并回答了关于Stack溢出的问题之后,我意识到我需要帮助。 Thank you in advance for helping this newbie. 提前谢谢你帮助这个新手。

Well, this is where you set the name of the file being saved: 好吧,这是您设置要保存的文件的名称的位置:

$target_path = "upload/";
$target_path = $target_path . basename( $_FILES['picture']['name']);

In this case, you're building the file name in the variable $target_path . 在这种情况下,您将在变量$target_path构建文件名。 Just change that to something else. 只需将其更改为其他内容。 What you change it to is up to you. 你改变它取决于你。 For example, if you don't care about the name of the file and just want it to always be unique, you could create a GUID or a Unique ID and use that as the file name. 例如,如果您不关心文件的名称并且只希望它始终是唯一的,则可以创建GUID唯一ID并将其用作文件名。 Something like: 就像是:

$target_path = "upload/";
$target_path = $target_path . uniqid();

Note that this would essentially throw away the existing name of the file and replace it entirely. 请注意,这实际上会丢弃文件的现有名称并完全替换它。 If you want to keep the original name, such as for display purposes on the web page, you can store that in the database as well. 如果要保留原始名称(例如在网页上显示),也可以将其存储在数据库中。

First get the file extension: 首先获取文件扩展名:

$file_extension = strrchr($uploaded_file_name, ".");

Then rename the uploaded file with a unique id + file extension 然后使用唯一的id + 文件扩展名重命名上传的文件

$uploaded_file_name = uniqid() . $file_extension;

Example: 例:

在此输入图像描述

TIP: Save the original file name and other information in a database. 提示:将原始文件名和其他信息保存在数据库中。

First get the extension with pathinfo() 首先使用pathinfo()获取扩展名

Then create your unique name: 然后创建您的唯一名称:

$name = 'myname'.uniqid();

Then rename your file. 然后重命名您的文件。

$target_path = $target_path . $name.$ext);

move_upload_file takes the second parameters $destination, its where you are inserting the target_path ( where your file is going to be saved with that given name ). move_upload_file接受第二个参数$ destination,它是你插入target_path的位置(你的文件将以给定的名字保存在哪里)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM