[SOLVED] writing and CREATING a file from php
Moderator: General Moderators
[SOLVED] writing and CREATING a file from php
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
please be as specific as possible and include some code examples...
ty in advance,
Burr
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
$file_handle = fopen($filename, "w");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");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:
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
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)){
echo "yes";
}else{
echo "no";
}
$handle = fopen($filename, "r");
while (!feof($handle)){
echo $handle;
}
?>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
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.burrito wrote:Code: Select all
<? $filename = "c:\\inetpub\\wwwroot\\sample.txt"; if(is_readable($filename)){ echo "yes"; }else{ echo "no"; } $handle = fopen($filename, "r"); while (!feof($handle)){ echo $handle; } ?>
file_get_contents() will read in an entire file to a single string.
Btw, on windows you can also use forward slashes....
Code: Select all
// the lazy way ;)
$fp = fopen("c:/path/to/file.txt", "r");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
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
and with http://www.php.net/fstat you can figure out when the file was last modified etc..
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:
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:
as you suggested for windows boxes...I get the same error.
Edit: if the file name exists...it works fine???
here's what I've got:
Code: Select all
if (is_writable($wfiles)) {
if (!$handle = fopen("done-".$wfiles, 'w')) {
print "Cannot open file ($wfiles)";
exit;
}
if (!fwrite($handle, "done".$contents)) {
print "Cannot write to file ($wfiles)";
exit;
}
fclose($handle);
} else {
print "The file $wfiles is not writable";
}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')) {Edit: if the file name exists...it works fine???