Bibin Paul
Bibin Paul

Reputation: 51

Deleting file in codeigniter 3.1.4

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

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

I got 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 file helper for this purpose as

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

The file is not deleted.file name is image.jpg folder name is files .Please help me to delete that file

Upvotes: 5

Views: 850

Answers (5)

Azad Bhagat Singh
Azad Bhagat Singh

Reputation: 41

If your files folder is exist in Application folder then use APPPATH like this:

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

Upvotes: 1

Astound
Astound

Reputation: 192

Hello don't use the base_url when you are giving path to unlink wihtout base_url give path

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

This always works for me, it must be work for your code to.

if unlink($path); gives error then try @unlink($path);

i hope this will work for you

Upvotes: 2

user4419336
user4419336

Reputation:

Use FCPATH

$path = FCPATH  . "/files/image.jpg";

unlink($path);

Upvotes: 2

Vladut
Vladut

Reputation: 121

If your codeigniter is in the server root you can use:

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

If you have it in a subfolder:

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

Upvotes: 1

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

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

Use __DIR__ to get to the file and then unlink it as you are doing it using http in the path and it doesn't allow to delete files like that.

Upvotes: 1

Related Questions