how do you create a file using php?

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-27-2008
brook
 
Posts: n/a
Default how do you create a file using php?

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

Reply With Quote
  #2 (permalink)  
Old 05-27-2008
Jerry Stuckle
 
Posts: n/a
Default Re: how do you create a file using php?

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
==================
Reply With Quote
  #3 (permalink)  
Old 05-27-2008
Leigh Finch
 
Posts: n/a
Default Re: how do you create a file using php?

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
Reply With Quote
  #4 (permalink)  
Old 05-27-2008
brook
 
Posts: n/a
Default Re: how do you create a file using php?

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


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:52 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0