fopen() create file problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
brookisme
Forum Newbie
Posts: 3
Joined: Sun May 25, 2008 9:20 pm

fopen() create file problems

Post by brookisme »

hey all - i´m new to php and having trouble writing a simple code which should create a file. here is the most simplified version:

***** PLEASE USE CODE OR PHP TAGS WHEN POSTING SCRIPTS *****

Code: Select all

<?php
   $content = "my content";
   $path = "test.txt";
        if(!chmod($path, 0744)) {
            echo "error, $path";
            exit;
        } else {
        $file = fopen($path, "w+");     
        }  
   if(fwrite($file, $content)) echo "writing=Ok";
   else echo "writing=Error";
   fclose($file);   
?>
 
if test.txt exists the code works perfect. but if there isn't already a test.txt file it gives this error:

Warning: chmod() [function.chmod]: No such file or directory in /home/littlesc/public_html/bms/simple.php on line 4
error, test.txt

I assume this is a permission thing but my permission for the directory is set to 755 - this should be ok right?

thanks.cheers.brook
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: fopen() create file problems

Post by hansford »

fopen() http://us.php.net/fopen

If file doesn't exist it will give error warning.

Errors/Exceptions
If the open fails, the function an error of level E_WARNING is generated. You may use @ to suppress this warning.

Return Values
Returns a file pointer resource on success, or FALSE on error.
brookisme
Forum Newbie
Posts: 3
Joined: Sun May 25, 2008 9:20 pm

Re: fopen() create file problems

Post by brookisme »

hey thanks hansford - you´re exactly right, since the file doesn´t exist it throws that warning. however, the idea is to create the file. if you look at the php manual which you linked it says:

w : Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

the key sentence being the last one - "if the file doesn´t exist, attempt to create it".

Also in my search i´ve seen several other tutorials that, if i understood them correctly, used fopen in this way to create a file... (though i can´t get them to work either) - maybe i missing something.

if not fopen, is there some other way to write php code which creates and writes to a new file?

thanks again.brook
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: fopen() create file problems

Post by hansford »

Your code works fine except the for the
!chmod($path, 0744)) part. This is where I was getting an error. I removed it-did a check and the file was written.
----------------------------
<?php

$content = "my content";
$path = "test.txt";
//if(!chmod($path, 0744)) {
// echo "error, $path";
// exit;
//} else {
$file = fopen($path, "w+", TRUE);
// }
if(fwrite($file, $content)) echo "writing=Ok";
else echo "writing=Error";
fclose($file);
$str = file_get_contents($path);
echo $str;
?>
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: fopen() create file problems

Post by dbemowsk »

brookisme, the problem is not with the fopen, it is with the chmod. You cannot chmod a file that doesn't exist. As hansford said, the code works if he removes the chmod. Also, your error says that the issue is in line 4 which is your chmod line. It looks like you are trying to use chmod to find out if a file exists. This is a bad idea. Use "if (file_exists("filename.ext")) {".
Post Reply