【问题标题】:Why does Unlink not work on this script?为什么 Unlink 不适用于此脚本?
【发布时间】:2011-09-20 08:37:57
【问题描述】:

您在下面看到的是我的脚本的一部分。

问题是我只需要调整大小的缩略图而不是原始文件。图片没有上传就不会重新调整大小,所以这个过程应该是这样的:

  • ->创建文件夹
  • ->上传原图
  • -> 调整拇指大小并存储
  • -> 删除原始文件。

现在这最后一部分(删除)不起作用。我收到此错误:

PHP 警告:unlink($target_file) [function.unlink]: No such file or directory in /path/file.php on line X

它没有找到它!

if (isset($_REQUEST['Submit'])) {

    mkdir($dirloc, 0755, true); 
    $i1=$_FILES['image']['name']; 
    $nw1="$dirloc/".$i1; 

    if ($i1) {
        $copy1 = copy($_FILES['image']['tmp_name'], $nw1);
    }

    $fileName = $_FILES["image"]["name"];
    $kaboom = explode(".", $fileName);
    $fileExt = end($kaboom);
    function ak_img_resize($target, $newcopy, $w, $h, $ext) {
        list($w_orig, $h_orig) = getimagesize($target);
        $scale_ratio = $w_orig / $h_orig;
        if (($w / $h) > $scale_ratio) {
            $w = $h * $scale_ratio;
        } else {
            $h = $w / $scale_ratio;
        }
        $img = "";
        $ext = strtolower($ext);
        if ($ext == "gif"){
            $img = imagecreatefromgif($target);
        } else if($ext =="png") {
            $img = imagecreatefrompng($target);
        } else {
            $img = imagecreatefromjpeg($target);
        }
        $tci = imagecreatetruecolor($w, $h);
        imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
        imagejpeg($tci, $newcopy, 80);
    }
    $target_file = "$dirloc/$fileName";

    $resized_file = "$dirloc/thumb.$fileExt";
    $wmax = 150;
    $hmax = 150;
    ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
    $xxx = $resized_file;
    $delete_target_file = unlink('$target_file');    

    $sql = "INSERT INTO $db_table(path,code,timecode,catg,description,title) values 
            ('$xxx','".mysql_real_escape_string(stripslashes($_REQUEST['code']))."',
            '".mysql_real_escape_string(stripslashes($times))."',
            '".mysql_real_escape_string(stripslashes($_REQUEST['catg']))."',
            '".mysql_real_escape_string(stripslashes($_REQUEST['area2']))."',
            '".mysql_real_escape_string(stripslashes($_REQUEST['fbox']))."')";

    if($result = mysql_query($sql ,$db)) {
        $codes = $_REQUEST['code'];
        $linkto = "?v=$codes";
        echo "<script>window.location = '$linkto'</script>";
    } else { 
        echo "ERROR: ".mysql_error();
    }
} else {
    // Here comes the form 
}

有人可以解释为什么它不删除它吗?

【问题讨论】:

  • 我认为“没有这样的文件或目录”不言自明。

标签: php image resize unlink


【解决方案1】:

使用双引号或根本不使用任何引号。

unlink("$target_file"); 

unlink($target_file); 

用单引号括起来的变量不被解析,你需要用双引号括起来或者根本不使用引号。

【讨论】:

  • @zol:有时我们需要新的眼光来发现这些类型的小错误。这是所有大多数开发人员每天都在发生的常见事情
【解决方案2】:

请确保,$target_file 包含文件的绝对路径。好像unlink找不到你要删除的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多