Deleting a file in codeigniter 3.1.4

I am using codeigniter 3.1.4. I am trying to delete a file in a folder in the root directory. When I use the unlink function as

$path=base_url()."files/image.jpg"; unlink($path); 

I got the following error:

 A PHP Error was encountered Severity: Warning Message: unlink(): http does not allow unlinking Filename: controllers/Deletion.php Line Number: 12 Backtrace: File: C:\xampp\htdocs\deletiontesting\application\controllers\Deletion.php Line: 12 Function: unlink File: C:\xampp\htdocs\deletiontesting\index.php Line: 315 Function: require_once 

When I use the file assistant for this purpose as

 $this->load->helper('file'); $path=base_url()."files/image.jpg"; delete_files($path); 

The file is not deleted. The file name is image.jpg the folder name is the files. Please help me delete this file.

+5
source share
5 answers

Use FCPATH

 $path = FCPATH . "/files/image.jpg"; unlink($path); 
+2
source

Hello, do not use base_url when you give the path to disable wihtout base_url give path

 $path="../files/image.jpg"; unlink($path); 

It always works for me, it should be work for your code.

If unlink($path); gives an error, try @unlink($path);

Hope this works for you.

+2
source

'Message: unlink(): http does not allow unlinking'

Use __DIR__ to access the file, and then disconnect it when you do this using http in the path and it does not allow to delete such files.

+1
source

If your code is in the root of the server, you can use:

  $path= $_SERVER['DOCUMENT_ROOT']."/"."files/image.jpg"; 

If you have this in a subfolder:

 $path= $_SERVER['DOCUMENT_ROOT']."/subfolder_name/"."files/image.jpg"; 
+1
source

If the file folder exists in the application folder, then use APPPATH as follows:

 $path = APPPATH . '/files/image.jpg'; //to set file path unlink($path); 
+1
source

Source: https://habr.com/ru/post/1268216/


All Articles