Script to upload files and merge their content.

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

bugmenot
Forum Newbie
Posts: 23
Joined: Fri Oct 10, 2008 11:44 am

Script to upload files and merge their content.

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Script to upload files and merge their content.

Post by Eran »

How exactly are the files to be merged? is the data appended at the end of the previous ?
bugmenot
Forum Newbie
Posts: 23
Joined: Fri Oct 10, 2008 11:44 am

Re: Script to upload files and merge their content.

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Script to upload files and merge their content.

Post 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";
}
 
bugmenot
Forum Newbie
Posts: 23
Joined: Fri Oct 10, 2008 11:44 am

Re: Script to upload files and merge their content.

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Script to upload files and merge their content.

Post 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.
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Script to upload files and merge their content.

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Script to upload files and merge their content.

Post by Eran »

Wasn't aware of it. even better :)
bugmenot
Forum Newbie
Posts: 23
Joined: Fri Oct 10, 2008 11:44 am

Re: Script to upload files and merge their content.

Post 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.
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Script to upload files and merge their content.

Post 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.
bugmenot
Forum Newbie
Posts: 23
Joined: Fri Oct 10, 2008 11:44 am

Re: Script to upload files and merge their content.

Post 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).
bugmenot
Forum Newbie
Posts: 23
Joined: Fri Oct 10, 2008 11:44 am

Re: Script to upload files and merge their content.

Post 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:
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Script to upload files and merge their content.

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Script to upload files and merge their content.

Post 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).
bugmenot
Forum Newbie
Posts: 23
Joined: Fri Oct 10, 2008 11:44 am

Re: Script to upload files and merge their content.

Post 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:
Last edited by bugmenot on Fri Nov 21, 2008 4:55 pm, edited 2 times in total.
Post Reply