ariferol01
ariferol01

Reputation: 249

How to unlink file from children dir of parent dir? PHP

I want to unlink an image file from children dir of a parent dir. I developing this project in a director folder under my website (mysite/project) but when i complete the project i will move to another hosting package. Whic code i need use for stable running in all directories?

I get this error:

Warning: unlink(../../images/urunler/deneme-urun.jpg): No such file or directory in /home/admin/public_html/eticaret/admin/includes/updateproduct.php on line 120

My folder structure:

click here to see my folder structure

$delete = unlink("../../images/products/".$img);

Upvotes: 2

Views: 634

Answers (3)

scytale
scytale

Reputation: 1399

  1. as the script is run from "public_html/eticaret/admin/includes/" you need to .. traverse back 3 directories to get to public_html which is a common ancestor folder for both image and script

  2. according to your directory image your images are in public_html/myproject/images/products/ and according to error detailed in your comment you are trying to delete from images/urunler/ I assume urunler is the actual project name.

if the folder containg your images is urunler try $delete = unlink("../../../images/urunler/".$img); if the container folder is products try $delete = unlink("../../../images/products/".$img);

if that fails try using an absolute path instead of a relative path.

Upvotes: 0

ariferol01
ariferol01

Reputation: 249

Thanks for all answers. I have a fault on this topic. Image name is xxx.jpg in my database but xxx.jpeg in the folder. but there is an interesting situation: if you have an image as .jpeg format, you can open on browser as .jpg format. I've tried a lot of methods to solve the above error, but I've found that. I using chrome browser now.

So i solved my problem yet :)

Upvotes: 0

MH2K9
MH2K9

Reputation: 12039

Before running unlink() command you can check the existence of the file to get rid of such error

$path = "../../images/products/".$img;
if (file_exists(path)) {   
    unlink($path)                        
}

Better is using the full directory path (absolute path). An example, let your project structure is enter image description here

and your unlinking code is in script.php, then your can get full absolute path the following way

$path = __DIR__ . "/../../images/products/{$img}";
if (file_exists(path)) {   
    unlink($path)                        
}

Upvotes: 2

Related Questions