【问题标题】:Rename file on upload PHP在上传 PHP 时重命名文件
【发布时间】:2014-01-15 03:39:38
【问题描述】:

我正在尝试构建一个脚本来上传图像并将其重命名为文件夹并将路径存储在我的 sql 数据库中。

这是我所在的位置:文件同时上传到文件夹和数据库的路径名,但我不知道如何重命名文件名。理想情况下,我想让文件名独一无二,这样我就不会重复。

<?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);

?>

我对这一切都很陌生,我真的很努力,但是在查看了许多教程并回答了有关 Stack-overflow 的问题后,我意识到我需要帮助。提前感谢您帮助这个新手。

【问题讨论】:

  • 你需要展示你已经尝试过的东西,并解释什么不起作用。在 PHP 中重命名文件是你可以很容易地用谷歌搜索的东西,你在哪里卡住了?
  • 你需要重命名$_FILES['picture']['name']
  • 你好,杰西卡,我用谷歌搜索过,最接近解决方案的是使用此文档php.net/manual/en/function.move-uploaded-file.php,但我只能在图像扩展后加上文本,如“upload/nameofimage.pnghereistheextratext”这对我来说没有帮助。我还尝试使用额外的参数创建一个新变量,它只会导致 sql 错误。
  • @Jessica,我希望你不会对我的尝试感到失望。

标签: php file upload renaming


【解决方案1】:

嗯,这是你设置要保存的文件名的地方:

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

在这种情况下,您在变量$target_path 中构建文件名。只需将其更改为其他内容即可。你把它改成什么取决于你。例如,如果您不关心文件名,只是希望它始终是唯一的,您可以创建 GUIDUnique ID 并将其用作文件名。比如:

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

请注意,这实际上会丢弃文件的现有名称并完全替换它。如果您想保留原始名称,例如为了在网页上显示,您也可以将其存储在数据库中。

【讨论】:

  • 这很好用,但它也会杀死文件的扩展名......当我检索显示图像的路径时会不会有问题?
  • @the_arthemis:不用于在网络上显示,不。文件扩展名实际上并不意味着什么。只要您在显示图像时发送正确的Content-Type 标头,浏览器就会知道如何处理它。上传文件时,应该在此处包含类型:$_FILES['picture']['type'],您应该将其与文件的其他信息一起存储在数据库中。
  • 你的意思是我的数据库中需要一个单独的列来存储类型吗?
  • @the_arthemis:建议这样做,这样您就可以在稍后提供文件时向浏览器提供文件类型。
  • 好吧,我不知道我是怎么做到的,但我现在用 '[uniqueid][originalname].[extension]' 重命名了文件,也就是说我不知道​​它是怎么发生的。
【解决方案2】:

首先使用pathinfo()获取扩展

然后创建您的唯一名称:

$name = 'myname'.uniqid();

然后重命名您的文件。

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

move_upload_file 采用第二个参数 $destination,它是您插入 target_path 的位置(您的文件将以该给定名称保存的位置)。

【讨论】:

    【解决方案3】:

    首先获取文件扩展名:

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

    然后用唯一ID + 文件扩展名

    重命名上传的文件
    $uploaded_file_name = uniqid() . $file_extension;
    

    示例:

    提示:将原始文件名和其他信息保存在数据库中。

    【讨论】:

      猜你喜欢
      • 2013-09-21
      • 2012-03-24
      • 1970-01-01
      • 2021-10-25
      • 2020-08-29
      • 2020-05-19
      • 2016-02-28
      • 2012-05-04
      • 1970-01-01
      相关资源
      最近更新 更多