faq
faq

Reputation: 3076

Why does Unlink not work on this script?

What you see below is a part of my script.

The problem is that I need only the re-sized thumb image and not the original file. The image won't re-size if it is not uploaded, therefore the process should look like this:

Now this last part (deletion) does not work. I get this error:

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

It does not find it!

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 
}

Could someone explain why it doesn't delete it?

Upvotes: 3

Views: 3996

Answers (2)

Umut KIRG&#214;Z
Umut KIRG&#214;Z

Reputation: 2113

Be sure, $target_file holds the absolute path of the file. It seems that unlink cannot find the file you want to delete.

Upvotes: 1

Shakti Singh
Shakti Singh

Reputation: 86336

use double quotes or do not use any quote at all.

unlink("$target_file"); 

or

unlink($target_file); 

Variable surrounded with single quote are not parsed you needs to surround them with double quotes or do not use quotes at all.

Upvotes: 9

Related Questions