简体   繁体   中英

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);

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在此处输入图片说明

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)                        
}
  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.

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 :)

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