好的,所以我正在工作的后台工作。这是我对 PHP 的第一次体验,所以我的代码很可能不会那么好。
这是我正在做的事情:
客户必须能够上传产品的图片+标题+描述,它必须在主页上显示该产品。之后,他必须能够删除或修改该项目。
所以上传工作正常,我设法展示产品,删除它,修改它。
唯一的问题是:当我使用新图像进行修改时,它不会替换目录中的旧图像(图像采用 sql 输入的 id,例如 0.jpg -> 1.jpg -2.jpg 等)
所以我将在这里展示我的上传脚本并修改:
上传:
<?php
include("config.php");
$dir = "../images/";
$dir = $dir . basename( $_FILES['image']['name']);
$title = $_POST['title'];
$description = $_POST['desc'];
$position = $_POST['position'];
$image = ($_FILES['image']['name']);
$uploadedfile = $_FILES['image']['tmp_name'];
$uploadedfiletype = $_FILES['image']['type'];
if (!($uploadedfiletype =="image/pjpeg" OR $uploadedfiletype =="image/jpeg" OR $uploadedfiletype =="image/jpg")){
echo "Image must be jpg file";
}else if (move_uploaded_file($_FILES['image']['tmp_name'], $dir)){
$sql="INSERT INTO test (title, description, position) VALUES ('$title','$description','$position')";
if (!mysql_query($sql,$con)){
die('Error: ' . mysql_error());
}
$newname = "../images/" . mysql_insert_id() . ".jpg";
rename ("../images/$image","$newname");
$orig_image = imagecreatefromjpeg("../image/$newname");
$sm_image = imagecreatetruecolor(96,96);
imagecopyresampled($sm_image,$orig_image,0,0,0,0,96,96,imagesx($orig_image),imagesy($orig_image));
imagejpeg($sm_image, $newname, 100);
echo 'Upload good <a href="connected.php">Retour</a>';
}else{
echo "Problem";
}
?>
这是我的修改脚本(基本上是一样的,但使用 UPDATE for sql):
<?php
include("config.php");
$dir = "../images/";
$dir = $dir . basename( $_FILES['image']['name']);
$title = $_POST['title'];
$description = $_POST['desc'];
$position = $_POST['position'];
$id=$_POST['id'];
$image = ($_FILES['image']['name']);
$uploadedfile = $_FILES['image']['tmp_name'];
$uploadedfiletype = $_FILES['image']['type'];
unlink('../images/$id');
if (!($uploadedfiletype =="image/pjpeg" OR $uploadedfiletype =="image/jpeg" OR $uploadedfiletype =="image/jpg")){
echo "L'image doit être en .jpg ou .jpeg";
}else if (move_uploaded_file($_FILES['image']['tmp_name'], $dir)){
$sql="UPDATE test SET title='$title', description='$description' WHERE id='$id'";
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
$newname = "../images/" . mysql_insert_id() . ".jpg";
rename ("../images/$image","$newname");
$orig_image = imagecreatefromjpeg("../image/$newname");
$sm_image = imagecreatetruecolor(96,96);
imagecopyresampled($sm_image,$orig_image,0,0,0,0,96,96,imagesx($orig_image),imagesy($orig_image));
imagejpeg($sm_image, $newname, 100);
echo 'Modif réussi <a href="connected.php">Retour</a>';
}else{
echo "Un probleme est survenue";
}
?>
提前感谢您的帮助!