Page 1 of 2

Script to upload files and merge their content.

Posted: Thu Nov 20, 2008 6:06 pm
by bugmenot
Hi.
I upload every day a file on my web site using this script:

Code: Select all

$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

Re: Script to upload files and merge their content.

Posted: Thu Nov 20, 2008 6:39 pm
by Eran
How exactly are the files to be merged? is the data appended at the end of the previous ?

Re: Script to upload files and merge their content.

Posted: Fri Nov 21, 2008 7:50 am
by bugmenot
Yes.
My package has a precise size (about 200 bytes) so each package could be written right after the previous one(s), at the bottom of the file.

Re: Script to upload files and merge their content.

Posted: Fri Nov 21, 2008 9:17 am
by Eran
You can use PHP file functions to read the old data, append the new and write to the file. Something like:

Code: Select all

 
$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";
}
 

Re: Script to upload files and merge their content.

Posted: Fri Nov 21, 2008 10:15 am
by bugmenot
Wow.
Your script is tiny but works flawlessly.

You are really good man! Thanks a lot. I think I will search for my PHP book :mrgreen:
Anyway, I need a lot of time to reach your level.

Re: Script to upload files and merge their content.

Posted: Fri Nov 21, 2008 10:52 am
by Eran
The best source for information in my opinion is the official manual at http://www.php.net
That and google :wink: . Since PHP is open-source and has such a large community, you can find almost everything online. And of course, you have this forum when you can't.

Re: Script to upload files and merge their content.

Posted: Fri Nov 21, 2008 11:05 am
by mmj
pytrin wrote:You can use PHP file functions to read the old data, append the new and write to the file. Something like:

Code: Select all

 
$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";
}
 
Why not use FILE_APPEND flag?

Re: Script to upload files and merge their content.

Posted: Fri Nov 21, 2008 11:15 am
by Eran
Wasn't aware of it. even better :)

Re: Script to upload files and merge their content.

Posted: Fri Nov 21, 2008 11:28 am
by bugmenot
Hi Pytrin.

There is a small problem with the script. It was not obvious until I wrote the program that breaks the concatenated file into its sub-parts and shows the data on screen.

Your script is adding an extra CRLF (enter) character between the records, breaking the format.
As I said, my file is binary, so adding/removing/changing even a single byte will destroy everything.

Re: Script to upload files and merge their content.

Posted: Fri Nov 21, 2008 11:36 am
by mmj
bugmenot wrote:Hi Pytrin.

There is a small problem with the script. It was not obvious until I wrote the program that breaks the concatenated file into its sub-parts and shows the data on screen.

Your script is adding an extra CRLF (enter) character between the records, breaking the format.
As I said, my file is binary, so adding/removing/changing even a single byte will destroy everything.
Try calling trim on $newfile before you append it.

Re: Script to upload files and merge their content.

Posted: Fri Nov 21, 2008 11:59 am
by bugmenot
mmj wrote: Try calling trim on $newfile before you append it.
I can't do that!! My file is binary!! :)
What if the last byte in my file is also a CR or LF character? It will be trimmed also.

The PHP manual doesn't say if "file_put_contents" can handle binary files. It seems that it can't.
-------------

EDIT:
I have a very simple solution for this. I can write a program to parse the file and remove the unwanted characters, based on their offset. But the problem is that I can't trust that PHP script anymore. I don't know what other alterations it may add into the file.

I will try to find if "file_put_contents" is a function for text-only files or it is also for binary files (in this case I need to find out why it alters the files).

Re: Script to upload files and merge their content.

Posted: Fri Nov 21, 2008 12:14 pm
by bugmenot
UHU!

Seems that the solution is "FILE_BINARY" flag for "file_get_contents".
I still don't have a clue on how to use "FILE_APPEND" flag.

--------------

My PHP version is 5.2.5 so "FILE_BINARY" flag is not supported in file_put_contents :banghead:

Re: Script to upload files and merge their content.

Posted: Fri Nov 21, 2008 12:35 pm
by mmj
bugmenot wrote:UHU!

Seems that the solution is "FILE_BINARY" flag for "file_get_contents".
1. FILE_BINARY is the default
2. FILE_BINARY flag is only available in PHP 6.0
bugmenot wrote:I still don't have a clue on how to use "FILE_APPEND" flag.
$result = file_put_contents($uploadfile, file_get_contents($newfile), FILE_APPEND);

.

PHP does not have a habit of adding CR and/or LF randomly. I suggested trim to just make sure nothing was being added.

If you are 100% sure a CR and/or LF are being added then you need to track it down.

There is no place in pytrin's code where he adds a CR and/or LF.

Re: Script to upload files and merge their content.

Posted: Fri Nov 21, 2008 1:44 pm
by Eran
As mmj said, reading files in binary mode is the default for file_get_contents().
I'm not sure where that line-break is coming from (maybe your editor is adding it? or PHP when breaking it into lines?). You can remove it by specifying the number of bytes you want to read from the old file (http://www.php.net/manual/en/function.f ... ntents.php), if you know the exact count (you said your packages have specific size).

Re: Script to upload files and merge their content.

Posted: Fri Nov 21, 2008 3:18 pm
by bugmenot
I have tried the new line of code "$result = file_put_contents($uploadfile, file_get_contents($newfile), FILE_APPEND);" but with the same result. At every upload 2 extra bytes are added.

Anyway. That was all. Thanks guys.
:drunk: