Jump to content

Script to upload files and merge their content.


compiled

Recommended Posts

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

 

 

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?

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.