Page 1 of 1

fopen() create file problems

Posted: Sun May 25, 2008 9:32 pm
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

Re: fopen() create file problems

Posted: Mon May 26, 2008 2:05 am
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.

Re: fopen() create file problems

Posted: Mon May 26, 2008 7:28 am
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

Re: fopen() create file problems

Posted: Fri Jun 06, 2008 8:20 pm
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;
?>

Re: fopen() create file problems

Posted: Fri Jun 06, 2008 9:56 pm
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")) {".