CodeIgniter Forums
Delete file after download - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Delete file after download (/thread-71127.html)

Pages: 1 2


Delete file after download - omid_student - 07-10-2018

Hi guys
I have script file that user upload file to server with this script
After upload file,i update data in user's file and again send this file to user
I use force download from codeigniter helper
ooom i need after download file via user,the file delete from server
Maybe?


RE: Delete file after download - Pertti - 07-10-2018

Hosting usually only gives each site limited harddrive resources, so yes, it's good idea to delete any user files that are not needed anymore.

Check out unlink function for PHP.


RE: Delete file after download - omid_student - 07-10-2018

(07-10-2018, 01:14 AM)Pertti Wrote: Hosting usually only gives each site limited harddrive resources, so yes, it's good idea to delete any user files that are not needed anymore.

Check out unlink function for PHP.

I used unlink but not working
I try use ignore_user_abort(true); but file not deleting


RE: Delete file after download - InsiteFX - 07-10-2018

Check you folder and file permissions on them.


RE: Delete file after download - Pertti - 07-10-2018

(07-10-2018, 01:26 AM)omid_student Wrote: I used unlink but not working
I try use ignore_user_abort(true); but file not deleting

If file uploads, user can't really abort your PHP script executing. Once they access URL through their browser, even if they close the browser, the script will still finish what it's doing.

I'd check that you are trying to delete the right file.

Enable error messages and echo out some of your variables to check that it does actually hold data that you expect to be there.

PHP Code:
$file 'path/to/file.txt';

// ...

error_reporting(E_ALL);
echo 
$file;
unlink($file); 



RE: Delete file after download - omid_student - 07-10-2018

(07-10-2018, 03:27 AM)InsiteFX Wrote: Check you folder and file permissions on them.

It is 0777 permisson
I think after force download,other command dont execute


RE: Delete file after download - omid_student - 07-10-2018

(07-10-2018, 04:25 AM)Pertti Wrote:
(07-10-2018, 01:26 AM)omid_student Wrote: I used unlink but not working
I try use ignore_user_abort(true); but file not deleting

If file uploads, user can't really abort your PHP script executing. Once they access URL through their browser, even if they close the browser, the script will still finish what it's doing.

I'd check that you are trying to delete the right file.

Enable error messages and echo out some of your variables to check that it does actually hold data that you expect to be there.

PHP Code:
$file 'path/to/file.txt';

// ...

error_reporting(E_ALL);
echo 
$file;
unlink($file); 

Can i read file content to variable and delete file and echo content?


RE: Delete file after download - omid_student - 07-10-2018

I found problem
There is exit command in end of force download function :|


RE: Delete file after download - Pertti - 07-10-2018

Aha, yeah, that'd be it, well done.

I suppose in most cases it would make sense there is no additional output once you send the file, because that would end up in the end of the file on users harddrive.

You only need couple of header() calls to make browser download the file, so you can write your own version of force download function, or alternatively you could write cron job that goes and cleans files off once their old enough to def not be in use anymore.


RE: Delete file after download - Pertti - 07-10-2018

(07-10-2018, 05:05 AM)omid_student Wrote: Can i read file content to variable and delete file and echo content?

Depends on the file size, but technically, yeah. If the file is small, like few meg, could be a reasonable option.

Might actually be even better, because when you modify original file, you've already load it in memory.