Page 1 of 2

[SOLVED] writing and CREATING a file from php

Posted: Thu Jan 20, 2005 9:26 am
by Burrito
I need to be able to CREATE and write a file to my hdd. The reason I emphasize create is because I alraedy know how to open an existing file and either append or overwrite what is already there, but I can not seem to figure out how to create the a new file. If I use the code I've got (pretty much straight out of the manual for fwrite) I get an error that the file doesn't exist...and of course it doesn't, I wanna create it.

please be as specific as possible and include some code examples...

ty in advance,

Burr

Posted: Thu Jan 20, 2005 9:34 am
by feyd

Code: Select all

$file_handle = fopen($filename, "w");
will create, if not existing; open and empty if existing.

note that on Windows machines at least, you really should use the binary/text modifiers as well:

Code: Select all

$file_handle = fopen($filename, "wb");
would create a binary file.

Posted: Thu Jan 20, 2005 10:42 am
by Burrito
ok thx feyd *again*.

on a little bit of a different note, how can I read the contents of a file and set them to a $var.

in other words I have a file called sample.txt which just has this in it "hello, I wish my name was Ted"

I just want to set $contents = whatever is in that file.

here's what I tried:

Code: Select all

<?
$filename = "c:\\inetpub\\wwwroot\\sample.txt";
if(is_readable($filename))&#123;
echo "yes";
&#125;else&#123;
echo "no";
&#125;
$handle = fopen($filename, "r");
while (!feof($handle))&#123;
echo $handle;
&#125;
?>
and that puts me into some kind of endless loop that just says "resource ID #1" or something like that. I've tried many derivations of that all to no avail.

I need this because I'm going to be parsing some files and I'd like to just be able to set the contents of my files to $somevar and then parse that var.

thx,

Burr

Posted: Thu Jan 20, 2005 10:48 am
by Burrito
nm...had another resource help me with this one..

here's what he came up with:

<?
$filename = "c:\\inetpub\\wwwroot\\sample.txt";
if(is_readable($filename)){
$filecontents = implode('', file($filename));
}else{
echo "no";
}
echo $filecontents;

?>


thx tho..

Burr

Posted: Thu Jan 20, 2005 10:49 am
by feyd
burrito wrote:

Code: Select all

<?
$filename = "c:\\inetpub\\wwwroot\\sample.txt";
if(is_readable($filename))&#123;
echo "yes";
&#125;else&#123;
echo "no";
&#125;
$handle = fopen($filename, "r");
while (!feof($handle))&#123;
echo $handle;
&#125;
?>
you need to echo/look at the return from fread() instead of trying to echo the $handle, which is an internal memory reference and shouldn't be used for display.

file_get_contents() will read in an entire file to a single string.

Posted: Thu Jan 20, 2005 10:53 am
by Burrito
ahh , I like that better...then I dont' have to convert from the array to a string.

excellent, ty again feyd.

Burr

Posted: Thu Jan 20, 2005 12:25 pm
by timvw
Btw, on windows you can also use forward slashes....

Code: Select all

// the lazy way ;)
$fp = fopen("c:/path/to/file.txt", "r");

Posted: Fri Jan 21, 2005 9:49 am
by Burrito
ok one last question for this thread that is along these lines.

how can I scan a directory for files and then loop over the files and then read them.

in other words I have a directory that has 4 files in it:
sample1.txt
sample2.txt
thissample1.cnf
thissample2.cnf

here's what I want to do:

first obtain the file names:
second read the files (this part I have now...thx feyd)
third parse the files and insert stuff do a db (don't have but can do on my own)
fourth somehow mark the files as "done" so they wont' get parsed again (unless they are changed) (need a little help with this)

ok, I'm going to confess somethign here, I got into web programming with cold fusion and doing it with CF would be a piece of cake. I'm sure it will be with php too, but I just don't know the functions etc to use.

please provide code samples and be as specific as possible....thx again for all your help.

Burr

Posted: Fri Jan 21, 2005 10:06 am
by feyd
there are 2 ways to get the file list from a directory: glob() or opendir()

read the docs for how to work with them. Note, that glob() requires a newish version of php.

Posted: Fri Jan 21, 2005 1:23 pm
by timvw
and with http://www.php.net/fstat you can figure out when the file was last modified etc..

Posted: Tue Jan 25, 2005 11:37 am
by Burrito
Feyd, I finally got around to testing the file creation...the topic of this thread and it doesn't work.

here's what I've got:

Code: Select all

if (is_writable($wfiles)) &#123; 
					if (!$handle = fopen("done-".$wfiles, 'w')) &#123; 
						print "Cannot open file ($wfiles)"; 
        				exit; 
					&#125; 
					if (!fwrite($handle, "done".$contents)) &#123; 
      					print "Cannot write to file ($wfiles)"; 
      					exit; 
					&#125;
					fclose($handle);
				&#125; else &#123; 
					print "The file $wfiles is not writable"; 
				&#125;
and this is what it yields:

Warning: fopen(done-logs/log4.txt): failed to open stream: No such file or directory in c:\inetpub\wwwroot\phonelogs\logparser.php on line 44
Cannot open file (logs/log4.txt)

sry it's taken me so long to actually try this, but it doesn't work...I've also tried:

Code: Select all

if (!$handle = fopen("done-".$wfiles, 'wb')) &#123;
as you suggested for windows boxes...I get the same error.

Edit: if the file name exists...it works fine???

Posted: Tue Jan 25, 2005 11:48 am
by feyd
since having the file exist already allows it to work, it may be a permissions things.

Posted: Tue Jan 25, 2005 11:51 am
by Burrito
I've got iusr_mymachine with write permissions on the folder though...

and just for sh!t's sake, I just gave everyone full control over the folder...same deal.

Posted: Tue Jan 25, 2005 11:57 am
by feyd
hit me up on IM.

Posted: Tue Jan 25, 2005 12:03 pm
by Burrito
would love to, but I can't use yim or aim from work...only msn...stupid firewall issues.

if you dont' have msn, I can hook you up with a little chat application and we can go that way...requires java though.

lemme know.