2011-06-22 63 views
3

你在下面看到的是我腳本的一部分。爲什麼Unlink無法在此腳本上運行?

問題是我只需要重新調整大小的縮略圖而不是原始文件。圖像不會調整,如果它不上傳,因此這個過程應該是這樣的:

  • - >創建一個文件夾
  • - >上傳原始圖像
  • - >調整大小和存儲拇指
  • - >刪除原始文件。

現在這最後一部分(刪除)不起作用。我得到這個錯誤:

PHP Warning: 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 
} 

有人可以解釋爲什麼它不會刪除它嗎?

+0

我認爲「沒有這樣的文件或目錄」是爲自己說話。 –

回答

9

使用雙引號或根本不使用任何引號。

unlink("$target_file"); 

unlink($target_file); 

變量,單引號包圍不被解析,你需要圍繞它們用雙引號或根本不使用引號。

+0

是的,謝謝,我怎麼沒有看到它:( – faq

+0

@zol:有時我們需要新鮮的眼睛來捕捉這些類型的小錯誤。這是所有大多數開發人員每天都在發生的常見事情 –

1

確定,$ target_file保存文件的絕對路徑。看起來,取消鏈接找不到要刪除的文件。

相關問題