【发布时间】: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,我希望你不会对我的尝试感到失望。