This is a discussion on how do you create a file using php? within the PHP Language forums, part of the PHP Programming Forums category; hey all - i´m new to php and having trouble writing a simple code which should create a file. here ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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: <?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? Just to be clear and a bit redundant: I want to use php to create a new file. if you look at the php manual 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.cheers.brook |
|
|||
|
brook wrote:
> 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: > > <?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 > Which is exactly correct. You cannot chmod() a file which doesn't exist. > I assume this is a permission thing but my permission for the > directory is set to 755 - this should be ok right? > Just to be clear and a bit redundant: I want to use php to create a > new file. if you look at the php manual it says: > No, the file doesn't exist. It has nothing to do with the directory. > 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. > Once you open it, it does exist. But your chmod() call comes before you try to open the file. > the key sentence being the last one - "if the file doesn´t exist, > attempt to create it". > But you never get this far. > 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.cheers.brook > That is correct. And if you execute the fopen() call, it will create the file. But you aren't getting that far. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|||
|
brook wrote:
> 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: > > <?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? > Just to be clear and a bit redundant: I want to use php to create a > new file. if you look at the php manual 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.cheers.brook > Hey Brook, The reason it is failing on the chmod is because the file does not exist. I would move the chmod after the creation of the file (if you need to chmod it at all). Perhaps something like this would work better. <?php $content = "my content"; $path = "test.txt"; $file = fopen($path, "w+"); if(fwrite($file, $content)) { echo "writing=Ok"; } else { echo "writing=Error"; } fclose($file); if(!chmod($path, 0744)) { echo "error, $path"; exit; } else { echo "chmod=OK"; } ?> Hope this helps, Cheers Leigh Finch www.phpmaniac.net |
|
|||
|
leigh´s suggestion worked exactly for the reason that jerry pointed
out . thanks a lot to you both of you. cheers.b On May 26, 7:49*pm, Leigh Finch <feiyan...@gmail.com> wrote: > brook wrote: > > 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: > > > <?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? > > Just to be clear and a bit redundant: *I want to use php to create a > > new file. if you look at the php manual 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.cheers.brook > > Hey Brook, > The reason it is failing on the chmod is because the file does not exist. > > I would move the chmod after the creation of the file (if you need to > chmod it at all). Perhaps something like this would work better. > > <?php > * * $content = "my content"; > * * $path = "test.txt"; > * * $file = fopen($path, "w+"); > * * if(fwrite($file, $content)) { > * * * * * * echo "writing=Ok"; > * * } else { > * * * * * * *echo "writing=Error"; > * * } > * * fclose($file); > > * * if(!chmod($path, 0744)) { > * * * * * * echo "error, $path"; > * * * * * * exit; > * * } else { > * * * * * * echo "chmod=OK"; > * * } > ?> > > Hope this helps, Cheers > Leigh Finchwww.phpmaniac.net |