Welcome Guest, Not a member yet? Register   Sign In
File upload
#1

[eluser]Unknown[/eluser]
Hello all,

I am facing a problem in file upload/delete related functions. Here's what is happening:

1. When I submit a file type field in a from my view, the file is uploaded successfully.

But when I edit the same record, and try to upload another file for the same record (such that previous file gets deleted and new selected file is uploaded) both delete and upload operations fail.

In my view I declare a form field name (receipt) and post this field's value to the controller.

Here is the code for this:-

<input name="receipt" type="file" id="receipt"/>

In the controller the post value is received and I assign it to the POST array too for additional processing:

$_POST['receipt'] = $_FILES['receipt']['name'];

Then the POST array is sent to the model.

In the Model, the insert operation works fine when I create the record for the first time. File uploads work fine.

But when I open the same record, and try to upload a new file, the previous file is not deleted, and the new file is also not uploaded.

Here's the code for it:

$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '3000';
$config['max_width'] = '1024';
$config['max_height'] = '768';

$this->load->library('upload',$config);
if ( !$this->upload->do_upload('receipt')){ // To upload the new file.

$this->template->set('system_message', 'Record Updated');
return false;
}
else{
$image_data = $this->upload->data();
}

$receipt_old_value = 'retrived from database';

unlink($receipt_old_value); // To delete the old file


Any help is appreciated.

Thanks,
Saahilwwaliaa
#2

[eluser]LuckyFella73[/eluser]
For your upload works the first time you add a file to a record
that routine seems to work. Therefore the issue might be to find
at the point you try to delete the old file. Since you didn't
set the fileupload config "overwrite" I assume the filename you
get in order to delete might be empty and that way the whole folder
content is deleted.
I would suggest to echo out (or use die() ) to get sure the filename
of the file to be deleted is correct.

PS: please use the CODE brackets the next time you post your code.
It makes reading much more comfortable. Many people here skip to
read posts if there are not formatted that way. ;-)




Theme © iAndrew 2016 - Forum software by © MyBB