1

I am trying to pass two variables from the view to the controller. One is the $id of a row that I want to delete from the database. This is working. The other is the $filename of an image that I want to remove from the uploads folder at the same time. I get a server error and I am not sure how else to go about doing this. I am sure it is something simple. Any ideas? Thanks.

//view

<input type="button" onClick="location.href = '<?php echo site_url("myfunction/delete/{$row['id']}/{$row['file_name']}"); ?>'" value="Delete" />

//controller

public function delete($id, $file_name) {

    $table_name = $this->table_name;
    delete_files('../uploads/$file_name');

    $this->load->model('delete', 'delete_model');
    $this->delete_model->delete_row($id, $table_name);

    redirect(site_url('site'));
}

1 Answer 1

3

Change

delete_files('../uploads/$file_name');

to

delete_files("../uploads/$file_name");

or

delete_files('../uploads/'.$file_name);

Using single quotes does not expand variables to their values:

$x = "onetwo";
$y1 = '$x';
$y2 = "$x";
$y3 = $x;

echo $y1; // $x
echo $y2; // onetwo
echo $y3; // onetwo
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply. That got rid of the server error! However my file is still not being deleted. It is in an "uploads" folder in the root along with the "application" and "system" folders. Am I calling to it wrong with ../ ? Thanks.
Can't say for sure, but I think it is relative to your model/controller, so if your code is in application/models/model.php then it should be ../../../uploads/...
The code is in application/controllers/mycontroller.php. I have tried a bunch of pathing variations ../../ ../../../ but still not deleting. Regardless, thanks for your help.
try echo'ing your file paths that CodeIgniter's upload class gives you when upload is complete, it could be also realpath() type paths. Feel free to ask for help anytime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.