我正在设置CRUD并意识到如果用户想要上传新图像,我将需要删除存储在目录中的图像。
我有一个带有表单的网页,该表单使用id从数据库行中提取信息,然后将更新的值发布到麻烦所在的脚本。
我试图找到需要删除的文件:
$target_dir = "images/photo/";
$del_image = $_FILES["image"];
尝试使用以下方法设置文件的权限:
$change = chmod($del_image,0644);
然后尝试删除文件:
$delete = unlink($target_dir.$image);
在用这个更新所有内容之前:
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$file = $target_dir . basename($_FILES["ud_image"]["name"]);
$uploadOk = 1;
if (move_uploaded_file($_FILES["ud_image"]["tmp_name"], $file))
{
echo '<script type="text/javascript">';
echo 'alert("News Items Saved")';
echo '</script>';
} else {
echo "Sorry, there was an error with your file.";
}
$id = intval($_GET['id']);
$ud_headline = $_POST["ud_headline"]; //mysql_real_escape_string()
$ud_body = $_POST["ud_body"]; //mysql_real_escape_string()
$ud_image = $_POST["ud_image"]; //mysql_real_escape_string()
$query="UPDATE news SET
headline = '$ud_headline',
body = '$ud_body',
image = '$ud_image'
WHERE id='$ud_id'";
$mysqli->query($query)or die($mysqli->error);
if($mysqli->affected_rows>=1){
echo "<script type='text/javascript'>";
echo "alert('News Item Updated');";
echo 'document.location.href = "/pc.v.2/admin-news.php";';
echo "</script>;";
}
else
{
echo "<script type='text/javascript'>";
echo "alert('News Item Not Updated'. $mysqli->error);";
echo "</script>";
//echo "Error deleting record: " . $conn->error;
}
我得到的错误告诉我,我甚至没有正确找到目录,更不用说文件了。
这是表格:
<form action="update.php" method="post" class="newNews">
<input type="hidden" name="ud_id" value="<?=$id;?>">
<!-- <input type="hidden" name="old_id" value="<?=$image;?>"> -->
<label for="title">Title</label>
<input type="text" name="ud_headline" value="<?=$headline;?>"/><br />
<label for="text">Body</label>
<textarea name="ud_body" rows="15" cols="21" value=""><?=$body;?></textarea><br />
<p>Current Photo</p>
<img src="<?=$target_dir.$image?>" alt=''><br />
<input type="file" name="ud_image" class="newsImage" ><br />
<input type="submit" name="submit" value="Update news item" class='addNew' />
</form>
我该如何解决这个问题?
答案 0 :(得分:0)
PHP move_uploaded_file()
实际上会覆盖旧文件,因此您实际上并不需要执行多余的unlink()
。
由于我们无法访问您的服务器,因此我们无法确切地告诉您出了什么问题。但它可能是其中一个(或更多):
$target_dir
前面有$_SERVER['DOCUMENT_ROOT'].'/'
或服务器的根路径,例如: '/var/www/'
。我这样做是为了解决问题:
// Derive target paths
$target_dir = $_SERVER['DOCUMENT_ROOT'].'/images/photo/';
$target_path = $target_dir . basename($_FILES["ud_image"]["name"]);
// Check target dir exists and is writable
if (!file_exists($target_dir )) {
// Try to automatically create the folders
umask(0);
if (!mkdir($target_dir , 0777, true)) { // or whatever permissions
// Do your error handling here
echo 'Sorry, target directory does not exist and we could not create it automatically.';
exit(1);
}
}
if (!is_writable($target_dir)) {
// Do your error handling here
echo 'Sorry, the directory is not writable.';
exit(1);
}
// Check for file errors
if (!isset($_FILES["ud_image"]["tmp_name"])) {
echo 'Sorry, no upload file detected.';
exit(1);
}
if ($_FILES["ud_image"]["error"] > 0) {
echo 'Sorry, there was an error with the file. Error code: '.$_FILES["ud_image"]["error"]);
exit(1);
}
// Move uploaded file
if (!move_uploaded_file($_FILES["ud_image"]["tmp_name"], $target_path)) {
// Do your error handling here
echo "Sorry, there was an error with the upload operation.";
exit(1);
}
// If you reach here, the upload should have succeeded. Go on to do whatever else you need to do
echo '<script>alert('News Item saved')</script>';
顺便说一下,服务器端代码中的echo
语句可能不是一个好主意,但除了这一点之外。最好将表示和逻辑分开,以便于维护和简化故障排除。