Welcome Guest, Not a member yet? Register   Sign In
Problem deleting file
#1

[eluser]macleodjb[/eluser]
I have this function which i am calling from my controller to delete files, but it's not allowing me to unlink the file and i'm not sure why.

This is the message i get.
Code:
Message: unlink() [function.unlink]: http does not allow unlinking

Here's my function
Code:
function delete_file_from_sys($id){
        $owner_query = $this->db->query("SELECT * FROM `files` WHERE (`file_id` = '$id')");
        $file = $owner_query->row_array();    
        $file_path = base_url(). $file['path'] . $file['file_name'];
        if(unlink($file_path)){
        return true;
        } else {
        return false;
        }
    }

and here's my controller function.
Code:
function delete_job_file(){
        if(!isset($_SESSION['uid'])){
        $this->session->set_flashdata('message','You must be logged in to delete files');
        redirect('login/login');
        }
        $this->load->model('File_model');
        $this->load->model('Edit_model');
        $this->load->model('Main_model');
        
        $file_id = $this->uri->segment(3);
        
        if(empty($file_id)){
            $this->session->set_flashdata('message','You must select a file to delete it');
            redirect('edit/job_files');
        }
        
        if(!$this->File_model->check_file_owner($_SESSION['uid'], $file_id)){
            $this->session->set_flashdata('message','It\'s not nice to try and delete other peoples files');
            redirect('edit/job_files');
        }
        
        if($this->File_model->delete_file_from_sys($file_id) && $this->File_model->delete_file_from_sql($file_id)){
            $this->session->set_flashdata('message','Your file has been deleted');
            redirect('edit/job_files');
        } else {
            $this->session->set_flashdata('message','There was a problem deleting your file');
            redirect('edit/job_files');
        }
        
        
    }//end of function
#2

[eluser]TheFuzzy0ne[/eluser]
Quite simply, because the filename you're trying to unlink starts with http://. The base_url() function returns your base_url. You shouldn't use it with unlink(). You just need to specify a relative or absolute path.
#3

[eluser]macleodjb[/eluser]
so all i need to specify is "files/filename.ext"
#4

[eluser]TheFuzzy0ne[/eluser]
Yes, assuming that files is a directory in the same directory as you're index.php file. Generally it looks nicer to do it like this:
Code:
unlink('./files/filename.ext');

although with that said, they essentially still do the same thing.
#5

[eluser]macleodjb[/eluser]
ok it works nicely now. I was curious if you knew how i could also integrate a pop up box that allows the user to confirm deletion by saying yes or cancel, something like that.
#6

[eluser]TheFuzzy0ne[/eluser]
Do you know any JavaScript?

For anything on my site that requires confirmation, I have a page that actually does the confirming, but also a javascript confirm() dialogue which bypasses the page.

Basically, I have a controller method called delete, which accepts two paramters. The first is the ID of the item to be deleted, and the second one accepts the word "confirm". If "confirm" is not passed as the second parameter, then the confirmation page is displayed. If JavaScript is enabled, the confirm dialogue pops up asking the user to confirm the delete, if they don't, the function returns FALSE (so nothing happens), if they do, then the URL for the link is rewritten, and "/confirm" is appended just before it's followed, so they don't get the confirmation page. Basically, if the user doesn't have JavaScript enabled, they still get a confirmation.

I hope this makes sense.
#7

[eluser]Vicente Russo[/eluser]
[quote author="TheFuzzy0ne" date="1236464186"]Yes, assuming that files is a directory in the same directory as you're index.php file. Generally it looks nicer to do it like this:
Code:
unlink('./files/filename.ext');
[/quote]

I`ve made a post on my blog with a few tips on CI. One of them was exactly this "problem". But I didn`t know using ./ is actually the index.php path. So I made a new constant on index.php

Code:
define('FULLPATH', pathinfo(__FILE__, PATHINFO_DIRNAME));

So if you`r right, and probably true, my constant is unecessary. Just for the record, my post with other tricks: blog post

V.




Theme © iAndrew 2016 - Forum software by © MyBB