Sheyviruz
Sheyviruz

Reputation: 39

how can I delete image outside its path

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

Answers (4)

MarcoS
MarcoS

Reputation: 17711

You could do:

<?php
    $path = dirname(__FILE__) . "../pics/";
    $imgid = $id . ".jpg";
    unlink($path . $imgid);
?>

Upvotes: 0

Hardik Panseriya
Hardik Panseriya

Reputation: 523

<?php

    $dir="pics/";

    $imgid=$id.".jpg";

    unlink(dirname(__FILE__).$dir.$imgid);

?>

Upvotes: 0

Kelu Thatsall
Kelu Thatsall

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

jogesh_pi
jogesh_pi

Reputation: 9782

Try like this:

$dir= dirname(__FILE__) . "/../pics/";
$imgid=$id.".jpg";

if( file_exists( $dir.$imgid ) ) {
   unlink($dir.$imgid);
}

Upvotes: 1

Related Questions