469,950 Members | 2,360 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

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

Jun 2 '08 #1
3 1850
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.
js*******@attglobal.net
==================
Jun 2 '08 #2
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
Jun 2 '08 #3
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.comwrote:
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
Jun 2 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Frank Millman | last post: by
9 posts views Thread by Lauren Quantrell | last post: by
3 posts views Thread by anon | last post: by
5 posts views Thread by Michael Sperlle | last post: by
1 post views Thread by ujjwaltrivedi | last post: by
15 posts views Thread by lxyone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.