-1

Apparently, I am confused how to use site_url() while deleting a file.

My site_url responded with http://localhost/Project/index.php

For deleting a file, I use unlink command. However, How to use site_url into the unlink command in codeigniter. I am surprised!

Below is the path where the uploaded files get stored!

$config['upload_path']      = './assets/images';

I have used the following:

unlink($_SERVER['DOCUMENT_ROOT']."/NetSach/assets/images/".$_FILES['picture']['name']);

Message: unlink(C:/xampp/htdocs/NetSach/assets/images/sweepers cartoon.jpg): No such file or directory

Deepak Keynes
  • 2,291
  • 5
  • 27
  • 56
  • Down-voters mind commenting before giving down-vote! – Deepak Keynes Feb 21 '21 at 15:36
  • 3
    unlink means delete the file. What is the connection between deleting the file and the URL? * I'm not the one who voted down * – Eden Moshe Feb 21 '21 at 16:12
  • @EdenMoshe I need to delete the file from the folder itself and then update it with the new file. So, I need to set the url path. Hope it makes sense! – Deepak Keynes Feb 21 '21 at 16:42
  • 1
    The documentation says: [*filename: Path to the file.*](https://www.php.net/manual/en/function.unlink.php#refsect1-function.unlink-parameters) not *URL to the file*. Also note that it is a bad habit to ask why people are downvoting: https://meta.stackoverflow.com/questions/285081/am-i-still-supposed-to-explain-my-downvotes-or-not/285777 – β.εηοιτ.βε Feb 21 '21 at 16:42
  • @β.εηοιτ.βε Agreed on down-vote. Between, How to pass the URL to the unlink(). Any idea? – Deepak Keynes Feb 21 '21 at 16:51
  • 1
    You don't. You are not removing an url, you are removing an actual file on the filesystem of your server. – β.εηοιτ.βε Feb 21 '21 at 16:53
  • You probably want to use `FCPATH`, or one of the other [path constants](https://stackoverflow.com/questions/13992074/codeigniter-path-constants-definitions). – Don't Panic Feb 23 '21 at 16:02
  • 1
    What people are failing to tell you is that there is a difference between a file's _URL_ and its _path_ (that is, its location _on disk_). An URL might be `https://www.sample.com/sample.html` but the actual path might be `/var/www/html/sample.html`. So it's not so simple to identify a file's path based on its URL. But the solution is easy. Just use `unlink($config['upload_path'] . '/' . $filename);` – pbarney Jun 30 '21 at 14:31

1 Answers1

0

Sorry about the innocent question. I hope I understood the question than others would! However, I tried the below and found it working finally!

unlink($_SERVER['DOCUMENT_ROOT'].'/project_folder/assets/images/'.$res['image_url']);

$_SERVER['DOCUMENT_ROOT'] // Gives the root path of the project folder successfully.

I use the codeigniter way of the path. i.e.) The base_url() is not working in the unlink because it will not accept https://

I hope this answer will be useful to someone!

Deepak Keynes
  • 2,291
  • 5
  • 27
  • 56