Forum Moderators: coopster

Message Too Old, No Replies

PHP File Writing. With Folders

         

Tom_Cash

12:30 pm on Nov 10, 2010 (gmt 0)

10+ Year Member



Hey Peeps,
I am being asked to create several txt files for customers without using a database connection. However, I can't figure out how to write them into folders. I imagined this to be simple, but I can't do it!

I've done a few searches and got what looks like answers, yet I can't make heads or tales of them.

Here's my code so far:

$filename_ip = str_replace(".", "-", $ip);
$filename = $filename_ip.".txt";

if (file_exists($filename))
{
echo "You can't do this again...";
}
else
{
// CREATE THIS FILE
ob_start();
$completion_date = date(r);
$fp = fopen($filename, 'w');
fwrite($fp, $completion_date);
fclose($fp);
ob_end_flush();


Any help would be awesome!

Thanks in advance,
Tom.

enigma1

2:27 pm on Nov 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You use the mkdir php function to create subdirectories if you have to.

And you specify the folder with the filename when you do the fopen. Make sure the folder path is either beneath the current folder your script is executed from or it uses the physical server path. The directory must exist before using the fopen.

eg with relative path from where the script is executed:
$filename = 'tmp/' . $filename_ip.".txt";

eg with fully qualified path
$filename = '/www/sitename/tmp/' . $filename_ip.".txt";

btw why the buffering?

Tom_Cash

10:11 am on Nov 11, 2010 (gmt 0)

10+ Year Member



Thanks Enigma. You will have to forgive me with this as I've never written to files before without copying and pasting code and amending it! (The shame...)

I just assumed that ob_start and flush were compulsory...

Now I know otherwise...

Your solution didn't work for me however. This is what I had originally assumed would work. I knew I wasn't that much of an amateur...

I'm currently running the script from the root directory and trying to save the details in a temp folder called tmp. However, I did try just executing the script from tmp itself and it's still not working.

Any ideas?

enigma1

3:25 pm on Nov 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What did you change the code? Can you post the code again with the paths you specified or tried to create? The tmp folder should be located under the folder where you execute the script from, if you're using relative paths.

Tom_Cash

10:25 am on Nov 12, 2010 (gmt 0)

10+ Year Member



I'm using relative paths, yeah... Here's my setup.

index.php (where the script is being run)
tmp/ (where I want my IP files saved)


$filename_ip = str_replace(".", "-", $ip);
$filename = "tmp/".$filename_ip.".txt";

if (file_exists($filename))
{
echo "You can't do this again...";
}
else
{
// CREATE THIS FILE
$completion_date = date(r);
$fp = fopen($filename, 'w');
fwrite($fp, $completion_date);
fclose($fp);


Thank youuuu. :)

enigma1

2:20 pm on Nov 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tried it and works fine had to correct the date and setup the ip


<?php
$ip = $_SERVER['REMOTE_ADDR'];
$filename_ip = str_replace(".", "-", $ip);
$filename = "tmp/".$filename_ip.".txt";

if (file_exists($filename)) {
echo "You can't do this again...";
} else {
// CREATE THIS FILE
$completion_date = date('r');
$fp = fopen($filename, 'w');
fwrite($fp, $completion_date);
fclose($fp);
}
?>


Make sure the tmp folder exists the script has write access to it and the tmp folder is located right under the folder where the script runs from.

Tom_Cash

3:19 pm on Nov 12, 2010 (gmt 0)

10+ Year Member



How odd! I can't get your code to work either.

I will have to come up with an alternative solution... Perhaps I can prefix the files with "tmp-" so they all get bunched up.

It shouldn't have to come to this. How very, very odd.

Enigma, really, thanks a lot for your time! Much appreciated.