我目前正在编写一个“编辑项目”脚本,该脚本允许用户替换我网站上的图像。这部分效果很好。
但是,我还希望允许上传新图像的选项- 这些不会替换现有图像。因此,总而言之,用户可以使用表单替换现有图像并上传新图像。
问题: 我如何告诉我的 PHP 一些图像是新的而不是替换的?我需要这样做,以便可以对新图像运行 INSERT INTO 查询。
我在下面附上了我的一部分代码:
PHP 处理现有文件的替换:
// get original name for ProjectImage update
$query3 = "SELECT * FROM ProjectImage as pi WHERE pid = '$id' ORDER BY pi.id";
$result3 = mysql_query ($query3); // Run the Query
$row3 = mysql_fetch_array($result3);
$originalname = $row3[1];
$number_of_file_fields = 0;
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory
for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
$number_of_file_fields++;
if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['images']['name'][$i];
if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
$number_of_moved_files++;
$url = $_FILES['images']['name'][$i];
list($width, $height, $type, $attr) = getimagesize("uploaded/".$_FILES["images"]['name'][$i]);
$pieces = explode("_", $url);
$ImageName = $pieces[1];
mysql_query("UPDATE ProjectImage SET Name = '$ImageName', Url = '$url', UrlHeight = '$height' WHERE Name = '$originalname' AND Pid = '$id'");
}
}
}
&
PHP 用于生成现有图像列表:
$query2 = "SELECT * FROM ProjectImage as pi WHERE pid = '$id' ORDER BY pi.id";
$result2 = mysql_query ($query2); // Run the Query
while ($row2 = mysql_fetch_array($result2))
{
?>
<div id="file_container" class="control-group">
<label for="image" class="control-label">Image:</label>
<div class="controls">
<input name="images[]" type="file" />
<img src="uploaded/<?php echo $row2['Url']; ?>" />
<a href="delete.php?id=<?php echo $row2['id']; ?>">Delete</a>
<br />
</div>
</div>
<?php
}
?>
<a href="javascript:void(0);" onClick="add_file_field();">Add another</a><br />
非常感谢任何指针:-)