Rename and replace a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Rename and replace a file

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

check these you should:
rename()
fwrite()
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

Burrito wrote:check these you should:
rename()
fwrite()

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 :D

Thanks

Eric
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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:

Code: Select all

chmod("test.txt", 0777);
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

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