Click here to get back home

create file and write content in same code block

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
create file and write content in same code block leon.san.email 07-31-2008
Get Chitika Premium
Posted by leon.san.email on July 31, 2008, 1:21 pm
Please log in for more thread options
This a modified part of a larger script I made. I couldn't get it
working so I made a smaller part of it - hoping to get it working.
Well it still doesn't work :(
The following code creates the new file, but does not write the
$content strings to the file. It then prints "Did nothing!" instead of
"done!".

<?php

$word = "2002";

$content1 = "one ";
$content2 = "two ";
$content3 = "three";

$fileName = '/home/leke/www/dic/' . $word . '.html';
$handle = fopen($fileName, 'w');
if (file_exists($fileName)) {
print "Did nothing!";
}
else
{
fwrite($handle, "$content1 . $content2 . $content3");
fclose($handle);
print "done!";
}
?>

I cant figure out the problem. Can anyone help me and maybe explain
what went wrong?
Thanks :)

Posted by macca on July 31, 2008, 1:43 pm
Please log in for more thread options
Maybe you need to change the permissions for the '/home/leke/www/dic/'
directory to allow write.

Posted by Robert Thomason on July 31, 2008, 2:27 pm
Please log in for more thread options
leon.san.email@gmail.com wrote:
> This a modified part of a larger script I made. I couldn't get it
> working so I made a smaller part of it - hoping to get it working.
> Well it still doesn't work :(
> The following code creates the new file, but does not write the
> $content strings to the file. It then prints "Did nothing!" instead of
> "done!".
>
> <?php
>
> $word = "2002";
>
> $content1 = "one ";
> $content2 = "two ";
> $content3 = "three";
>
> $fileName = '/home/leke/www/dic/' . $word . '.html';
> $handle = fopen($fileName, 'w');
> if (file_exists($fileName)) {
> print "Did nothing!";
> }
> else
> {
> fwrite($handle, "$content1 . $content2 . $content3");
> fclose($handle);
> print "done!";
> }
> ?>
>
> I cant figure out the problem. Can anyone help me and maybe explain
> what went wrong?
> Thanks :)

The fopen($filename,'w') function will create the file. Therefore, the
file exists, and file_exists() will return true, causing the code to
take the "Did nothing" branch.

Try this:

$fileName = '/home/leke/www/dic/' . $word . '.html';

if (file_exists($fileName)) {
print "Did nothing!";
}
else
{
$handle = fopen($fileName, 'w');
fwrite($handle, "$content1 . $content2 . $content3");
fclose($handle);
print "done!";
}

Of course, you probably want to add some error checking after the
fopen() call to make sure the file was created properly before writing
to it, but this sequence should work.


Posted by leon.san.email on July 31, 2008, 4:05 pm
Please log in for more thread options
Ok, I see what you mean. Originally I thought because fopen($fileName,
'w'); was assigned to $handle , it wouldn't be executed until $handle
was called in fwrite($handle, ...

Thanks for pointing that out :)

Similar ThreadsPosted
I want to write a function that removes the second CSV column of its content August 23, 2006, 7:20 pm
Create and Dispose dynamic content June 5, 2005, 8:03 am
automastically create and post an email to each customer of the same content October 26, 2004, 8:51 pm
help: how to organize collaboration to write PHP code? April 18, 2005, 4:10 pm
how to not write password in code for using to mysql? May 27, 2007, 5:15 am
php code of a form both readonly/write November 20, 2007, 6:31 am
Can i write php code to connect 2 MySQL DB. like this case. ? February 9, 2006, 1:35 am
UTF8: file_put_contents doesn't seem to write UTF8 content properly June 13, 2007, 4:25 pm
PHP/IIS: File Read/Write OK, File Unlink Denied March 22, 2006, 5:10 am
how to read out the content of a file named domain.com/file.php?X=ABC June 7, 2005, 3:15 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap