The optimum method to rewrite files

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
jateeq
Forum Newbie
Posts: 14
Joined: Mon Mar 01, 2010 12:16 pm

The optimum method to rewrite files

Post by jateeq »

Hello,

I have a web interface that allows users to upload files. If the user uploads an already existing file, I want that file to overwrite the existing file. As I see it, there a lot of different ways to do this, but since I haven't done this before, i wanted to pick more experienced programmers' opinion(s).

I know of the following ways, and currently I am going with #4, and in my opinion this is the best because it's simple and reliable :

1: use php's copy() function - although there's something about rewrite failing if the new and old files do not belong to the same owner...
2: open existing file for writing, open uploaded file for reading, copy/paste contents of latter to former (i guess the copy() function already does this)
3: delete existing file, mv uploaded file in its place
4: use linux's native command 'cp'.

Thank you for your time,
Jawad.
jateeq
Forum Newbie
Posts: 14
Joined: Mon Mar 01, 2010 12:16 pm

Re: The optimum method to rewrite files

Post by jateeq »

anyone....?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: The optimum method to rewrite files

Post by AbraCadaver »

jateeq wrote:Hello,

I have a web interface that allows users to upload files. If the user uploads an already existing file, I want that file to overwrite the existing file. As I see it, there a lot of different ways to do this, but since I haven't done this before, i wanted to pick more experienced programmers' opinion(s).

I know of the following ways, and currently I am going with #4, and in my opinion this is the best because it's simple and reliable :

1: use php's copy() function - although there's something about rewrite failing if the new and old files do not belong to the same owner...
2: open existing file for writing, open uploaded file for reading, copy/paste contents of latter to former (i guess the copy() function already does this)
3: delete existing file, mv uploaded file in its place
4: use linux's native command 'cp'.

Thank you for your time,
Jawad.
Regardless of which way you do it, the web server user will have to have write permissions to the original file. So for portability I would use #1.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply