gpt4 book ai didi

php - 我如何在 php 中删除图像?

转载 作者:搜寻专家 更新时间:2023-10-31 21:03:23 25 4
gpt4 key购买 nike

我正在尝试删除 ajax php 文件中的图像。我有以下代码:

<?php
define('IS_AJAX', true);

$id = $db->real_escape_string($_GET['photo_id']);
$files = $db->query("SELECT * FROM uploaded_photos WHERE id=".$id);
$files = $files->fetch_object();

$file = $files->path;

if($file){
unlink("../uploads/".$file);
$db->query("DELETE FROM uploaded_photos WHERE id='".$id);

}

在这种情况下,路径是图像,例如1.jpg。它会删除表格行,但不会删除上传文件夹中的图像。希望有人可以帮助我。预先感谢您的努力。

最佳答案

您可能需要重新检查您的代码。您有一个未闭合的单引号。请参阅下面的代码...

        define('IS_AJAX', true);

$id = $db->real_escape_string($_GET['photo_id']);
$files = $db->query("SELECT * FROM uploaded_photos WHERE id=".$id);
$files = $files->fetch_object();

$file = $files->path;

// NOTE THAT if($file) WOULD ALMOST ALWAYS RETURN TRUE SO LONG AS IT CONTAINS ANY STRING
// HOWEVER file_exists WILL CHECK IF THE FILE PHYSICALLY EXIST ON THE PATH/LOCATION OR NOT.
// PREFER file_exists IN THIS SCENARIO INSTEAD.
if(file_exists("../uploads/" . $file)){
// CHANGE THE FILE PERMISSION ON THE IMAGE SO THAT YOU CAN WORK WITH IT...
chmod("../uploads/" . $file, 0755); // <== GIVES YOU PERMISSION TO DEAL WITH THE FILE...
unlink("../uploads/".$file);
// BY THE WAY; HERE IS A PROBLEM... YOU HAVE AN UNCLOSED SINGLE QUOTE AFTER THE EQUAL SIGN.
// I REALLY WONDER HOW THE TABLE ROW DELETES WITH THE TYPO...
// TRY REMOVING OR COMPLETING IT THOUGH:
//$db->query("DELETE FROM uploaded_photos WHERE id='".$id); <=== SEE THAT SINGLE QUOTE AFTER "=" THERE? THAT'S YOUR ENEMY.
$db->query("DELETE FROM uploaded_photos WHERE id=".$id);
}

更新:没有评论

    <?php

define('IS_AJAX', true);

$id = $db->real_escape_string($_GET['photo_id']);
$files = $db->query("SELECT * FROM uploaded_photos WHERE id='{$id}'" );
$files = $files->fetch_object();
$file = $files->path;

if(file_exists("../uploads/" . $file)){
chmod("../uploads/" . $file, 0777);
unlink("../uploads/".$file);
$db->query("DELETE FROM uploaded_photos WHERE id='{$id}'");
}

关于php - 我如何在 php 中删除图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37174531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com