Welcome Guest, Not a member yet? Register   Sign In
[Solved] Saving Temp File Outside of Doc Root
#1

[eluser]CoffeeBeanDesign[/eluser]
Code:
$table = '<table><tr><td>Data</td></tr></table>';

$filename = 'myfile.xls';
$fh = fopen($filename, 'w') or die("can't open file");
fwrite($fh, $table);
fclose($fh);

$this->load->library('email');
$this->email->attach($filename);

... Email stuff ...

$this->email->send();

unlink($filename);

Hi Guys,

Consider the above code (I've cut it down for brevity). I'm basically making a file on the fly, attaching it to an email and then deleting it. It all works fine but I'd prefer that the temp file is saved outside of the doc root just in case (for whatever crazy server madness) it is not deleted correctly. I've tried '../myfile.xsl' as the filename but am having no luck. Is there a better way to achieve this?? The file is not level 10 sensitive info but I want to be as secure as possible... within reason.

I'd appreciate your suggestions or alternative ideas.

Many thanks for your time.
#2

[eluser]tomcode[/eluser]
1. safety : You can keep Your whole system folder outside the web root by specifying the adequate path in ./index.php.

2. CI has a file helper.
#3

[eluser]CoffeeBeanDesign[/eluser]
Thanks for the reply. I've changed to the file helper which is simpler. I now have :

Code:
$filename = 'myfile.xls';

write_file($filename, $table);

$this->email->attach($filename);

... Email Stuff ...

delete_files($filename);

... It is writing the file in the correct place but not deleting it, is it a permissions thing??
#4

[eluser]tomcode[/eluser]
Maybe delete_files() is meant for directories, try unlink().
#5

[eluser]CoffeeBeanDesign[/eluser]
...You beat me to it!!

... I see that delete_files() is for deleting the contents of a directory rather than a specific file. unlink() works fine on it's own but I still haven't solved the problem of saving/deleting outside the doc root.
#6

[eluser]tomcode[/eluser]
Use an absolute path or realpath( ‘../myfile.xsl’) should work, too.
#7

[eluser]CoffeeBeanDesign[/eluser]
This is odd. I tried the following -

Code:
$filename = realpath('../tmp/myfile.xsl');

I removed the unlink part of the script to check where it was saving the file. The file was successfully attached and sent but I cant find the file anywhere on the server. There is no 'tmp' directory in my /var/www/ folder as expected, (one up from the doc root). I've searched everywhere for the file but can't find it.

I'm happy that the temp file is now outside the doc root but slightly concerned I have no idea where it is being saved...!!

There is one factor I haven't mentioned which shouldn't but may be affecting things, I'm using a temporary domain - http://myserver.com/mysite.com/ this could change the relative locations of directories (although I'm fairly sure it wouldn't).
#8

[eluser]tomcode[/eluser]
print $filename, that should give You the info where to look for it.
#9

[eluser]CoffeeBeanDesign[/eluser]
update...

I thought the email was working because it was showing up with the old paper clip in Outlook but on closer inspection the file wasn't attached. So that answers my mystery about where the file is, it doesn't exist...!!

I think my best plan is to check with the host. They might have some specific write permissions for var/www/.

Cheers for your help.
#10

[eluser]CoffeeBeanDesign[/eluser]
Sorted.

I needed to use the full path - '/var/www/myfile.xsl'.

Everything is good and happy and that little bit more secure.

All the best.




Theme © iAndrew 2016 - Forum software by © MyBB