473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2031
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Frank Millman | last post by:
Hi all I need to generate potentially large reports from a database, and I want to offer the option of print preview before actually printing (using wxPython). I figure that the best way to...
9
by: Lauren Quantrell | last post by:
Is there a way to create a text file (such as a Windows Notepad file) by using a trigger on a table? What I want to do is to send a row of information to a table where the table: tblFileData has...
5
by: David Webb | last post by:
The problem started when the Working Folder for a project was somehow set to the folder of another project. I set the correct working folder in VSS and deleted the .vbproj files that had been...
3
by: anon | last post by:
I have been used to using DAO in the past, and then converted to ADO. Now I am having to use VB.Net(2000) and ADO.NET and am experiencing difficulties with the creation and population of an mdb....
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
8
by: barb | last post by:
So that the world at large benefits from our efforts, here is one fully documented way to use Windows Irfanview freeware to create thumbnail web galleries (http://www.irfanview.com). STEP 1:...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
1
by: ujjwaltrivedi | last post by:
Hey guys, Can anyone tell me how to create a text file with Unicode Encoding. In am using FileStream Finalfile = new FileStream("finalfile.txt", FileMode.Append, FileAccess.Write); ...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.