Page 1 of 1

The optimum method to rewrite files

Posted: Mon Mar 08, 2010 2:12 pm
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.

Re: The optimum method to rewrite files

Posted: Mon Mar 08, 2010 4:02 pm
by jateeq
anyone....?

Re: The optimum method to rewrite files

Posted: Mon Mar 08, 2010 4:32 pm
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.