Reputation: 39
I have a folder called holder. Inside holder I also have two directories named pics and script.
Inside script directory I have a script for deleting images inside pics folder.
I may want to delete images inside pics.
Note that my script(file) to achieve this is inside the script folder for some reasons.
<?php
$dir="pics/";
$imgid=$id.".jpg";
unlink($dir.$imgid);
?>
Upvotes: 0
Views: 84
Reputation: 17711
You could do:
<?php
$path = dirname(__FILE__) . "../pics/";
$imgid = $id . ".jpg";
unlink($path . $imgid);
?>
Upvotes: 0
Reputation: 523
<?php
$dir="pics/";
$imgid=$id.".jpg";
unlink(dirname(__FILE__).$dir.$imgid);
?>
Upvotes: 0
Reputation: 2541
It's hard to figure out what do you need from your question but I guess your issue is that you are in the wrong directory. If you have the script inside script
directory (and you call it from that place!) then you have two options to solve that, switch working directory or just correct your path.
1) Switch working directory
chdir("..");
$dir="pics/";
$imgid=$id.".jpg";
unlink($idir.$imgid);
2) Correct the path to delete
$dir="../pics/";
$imgid=$id.".jpg";
unlink($idir.$imgid);
Upvotes: 1
Reputation: 9782
Try like this:
$dir= dirname(__FILE__) . "/../pics/";
$imgid=$id.".jpg";
if( file_exists( $dir.$imgid ) ) {
unlink($dir.$imgid);
}
Upvotes: 1