简体   繁体   中英

Can Not delete a file from directory using php

I have some files in the directory ../gallery/drawingimage/ i have to delete a file. I have written the following code. But it is not working.

<?php
include("../gallery/includes/connection.php");

$file = $_POST['fname'];

if($_POST['ptype']=='drawing'){
        $delete = mysql_query("DELETE FROM drawing WHERE pname = '$file'") or die(mysql_error());

         $data=$file.".jpg";
         $dir = "../gallery/drawingimage/".$data;
        // echo $dir;
         unlink('$dir');

    }


//  header("Location: ../cpanel.php");

remove the single quotes - use

unlink($dir);

instead of

unlink('$dir');

To clarify Zali's answer, php makes a distinction between single and double quotes, and no quotes at all. Single quotes are not parsed for variables. For example, the following:

$x = "Some Text"

print $x;

Some Text

print "$x";

Some Text

print '$x';

$x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM