作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
有人可以使用以下代码向我解释如何在上传过程中将图像文件重命名为其他名称吗?
这是我正在使用的。
uploader.php
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
$dataType = mysql_real_escape_string($_POST["dataType"]);
$title = mysql_real_escape_string($_POST["title"]);
$fileName = basename($_FILES["image"]["name"]);
$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/".$fileName);
if (file_exists($target_path))
{
echo "An image with that file name already exists.";
}
elseif (move_uploaded_file($_FILES["image"]["tmp_name"], $target_path))
{
// The file is in the images/gallery folder. Insert record into database by
// executing the following query:
$sql="INSERT INTO images (data_type, title, file_name)"."VALUES('$dataType','$title','$fileName')";
$retval = mysql_query($sql);
echo "The image was successfully uploaded and added to the gallery :) <a href='index.php'>Add another image</a>";
}
else
{
echo "There was an error uploading the file, please try again!";
}
?>
然后这是我将图像上传到图库的代码。
<form enctype="multipart/form-data" action="uploader.php" method="POST">
Category: <select class="text" name="dataType">
<option value="treeremoval" selected="selected">treeremoval</option>
<option value="treetrimming" >treetrimming</option>
<option value="treebracing" >treebracing</option>
<option value="stumpgrinding" >stumpgrinding</option>
<option value="firewood" >firewood</option>
<option value="cleanup" >cleanup</option>
</select><br />
<br />
Caption: <input type="text" name="title"><br />
<br />
Image to upload: <input type="file" name="image"><br />
<br />
<input type="submit" value="Upload">
</form>
我对使用 php 和 mysql 很陌生,所以任何帮助都将不胜感激。我还有一些其他问题,但我想我应该一次问一个。 =)
谢谢!
最佳答案
我会尝试这样的事情,你将创建一个唯一的 ID 并将文件的扩展名附加到它,如果该名称存在你循环直到你有一个不存在的,然后你移动文件。
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
$dataType = mysql_real_escape_string($_POST["dataType"]);
$title = mysql_real_escape_string($_POST["title"]);
$fileData = pathinfo(basename($_FILES["image"]["name"]));
$fileName = uniqid() . '.' . $fileData['extension'];
$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName);
while(file_exists($target_path))
{
$fileName = uniqid() . '.' . $fileData['extension'];
$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName);
}
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_path))
{
// The file is in the images/gallery folder. Insert record into database by
// executing the following query:
$sql="INSERT INTO images (data_type, title, file_name)"."VALUES('$dataType','$title','$fileName')";
$retval = mysql_query($sql);
echo "The image was successfully uploaded and added to the gallery :) <a href='index.php'>Add another image</a>";
}
else
{
echo "There was an error uploading the file, please try again!";
}
?>
关于php - 上传PHP Mysql时重命名图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17602934/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!