views:

52

answers:

2

Basically I could upload files based on a project. Whenever I create a project, a new directory is created with the directory name as the project_name e.g. this is a test -> this-is-a-test. But my problem is I couldn't delete a file in a directory.

function delete_image($id)
{
    $this->load->model(array('work_model', 'project_model'));
    $result = $this->work_model->get_work($id);
    $result = $this->project_model->get_project($result->project_id);
    $dir = str_replace(" ", "-", $result->project_name);
    $result = $this->work_model->delete($id);
    if (isset($result)){
        unlink('./uploads/' . $dir . '/' . $result->full_path);  
    }
    redirect('admin/project/view_project/' . $result->project_id);
}

Need help on this thanks.

A: 

Well the error message is self-explanatory.
$result is not an object.

Your problem is matter of debugging, not SO question. And matter of reading error messages, of course. Why do you ask here what's going wrong if you already have explanation from your PHP? And, on the other hand, noone here know your code and have no idea what type of variable get_work($id) supposed to return.

Col. Shrapnel
get_work($id) returns a database result. I'm not really a programmer so I don't get these trivial error messages.
Chester Sabado
Thanks I already found a solution.
Chester Sabado
A: 

This line is brilliant:

if (isset($result)){

Of course it is set, you have set it 3 times!

Use different variable names for each result you return. Why not use:

function delete_image($id)
{
$this->load->model(array('work_model', 'project_model'));
$work = $this->work_model->get_work($id);
$project = $this->project_model->get_project($work->project_id);
$dir = str_replace(" ", "-", $project->project_name);

if ($this->work_model->delete($id))
{
    unlink('./uploads/' . $dir . '/' . $project->full_path);  
}
redirect('admin/project/view_project/' . $project->project_id);
}

If that doesn't work, try some debug steps.

var_dump($this->work_model->delete($id));

That will tell you TRUE/FALSE, I'd assume right now it is FALSE which is why unlink isn't erroring or succeeding.

Debug is the way forward. We can't do it for you!

Phil Sturgeon
Yes thanks. I already found a soluton.
Chester Sabado