Page 1 of 1
Rename and replace a file
Posted: Wed Jun 29, 2005 2:50 pm
by ericburnard
Hi again this may seem a bit long and tedious but i am wanting a script that lets me do the following-
- re-names the file "new.txt" to what ever i type into a text box (with .txt ending)
- Creates a new file called "new.txt" with a chmod at 777
I know this sounds simple to most people, but i have only been using php for a while and am still mastering most things.
Some responce would be great
thanks again
Eric
Posted: Wed Jun 29, 2005 3:06 pm
by Burrito
check these you should:
rename()
fwrite()
Posted: Wed Jun 29, 2005 3:17 pm
by ericburnard
Thanks for that
i have had a quick too and understand the renaming a file but not how to create a new file and set ther permissions.
Help Please
Thanks
Eric
Posted: Wed Jun 29, 2005 3:23 pm
by Burrito
straight out of the manual this is (only changed the a to w I did):
Code: Select all
<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
now chmod() the file:
Posted: Wed Jun 29, 2005 3:31 pm
by ericburnard
okey is it me or does this code only open the file and add something to it???
Is there no code that just creates a new file and the another one to chmod it??
Thanks
Eric
Posted: Wed Jun 29, 2005 3:33 pm
by Burrito
try it did you?
set the mode to w I did...
php manual wrote:
'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.
Posted: Wed Jun 29, 2005 3:42 pm
by ericburnard
I have tried it and just got it to say that the txt file is not writeable, even though my chomd for it is set to 777
Eric
Posted: Wed Jun 29, 2005 3:47 pm
by Burrito
Give permissions to the web user to write to the folder you must. If permissions errors you are seeing, the problem that probably is.
chmod the file you can not if already exists it does not...