Forum Moderators: coopster

Message Too Old, No Replies

Script to upload files and merge their content.

         

Monitor

12:05 am on Nov 21, 2008 (gmt 0)

10+ Year Member



Hi.
I upload every day a file on my web site using this script:

$uploadfile = $uploaddir . basename($_FILES['nume_fis']['name']);
if (move_uploaded_file($_FILES['nume_fis']['tmp_name'], $uploadfile))
{echo "File is valid, and was successfully uploaded.\n";} else { }

The problem with the script is that it overwrites the file uploaded one day before. How can I do to merge the content of all uploaded files in a single file, so at the end of the week I can see all data that I have uploaded?
Note: the file that I upload in not TXT but binary. I am beginner in PHP :)
Thanks

cameraman

1:11 am on Nov 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have a look at fopen [us2.php.net] and fwrite [us2.php.net]. There is also an fread, but have a look at file_get_contents [us2.php.net] - fewer steps ;)

Monitor

1:48 pm on Nov 21, 2008 (gmt 0)

10+ Year Member



Thanks for the answer.

That was may original idea, the problem is that the actual script (see above) already overwrite the existing file. So, I get no chance to concatenate them.

Actually, I think my question should be: How to make the actual script not to overwrite the existent file?

Monitor

5:52 pm on Nov 21, 2008 (gmt 0)

10+ Year Member



I have found a solution:


$newfile = $_FILES['nume_fis']['tmp_name'];
$uploadfile = $uploaddir . basename($_FILES['nume_fis']['name']);
if( is_readable($newfile) && is_readable($uploadfile) ) { //If the old file exists and is readable
$result = file_put_contents($uploadfile, file_get_contents($uploadfile) . file_get_contents($newfile) ); //Appending new data and writing to file
} else {
$result = move_uploaded_file($newfile, $uploadfile);
}

if($result !== false) {
echo "File is valid, and was successfully uploaded.\n";
}

but there is a problem. The script above inserts an ENTER between records when it appends the files, breaking my binary format.

Any ideas how to get rid of this.

PS: Also, somebody suggested to use FILE_APPEND flag.