Home > Mobile >  Downloading file, changing extension and merging into 1 file
Downloading file, changing extension and merging into 1 file

Time:03-03

Trying to download files via the URL, rename from the .ADM extension to .txt then put contents of each file into a single txt file However its saying the fputs param 2 is a resource The $logfile['name'] is the filename thats stored in the array

Heres my code

foreach($items as $logfile)
 {
  $getfile = $logfile['Download'];
  $newfile = file_put_contents(str_replace('ADM','txt',$logfile['name']), 
 file_get_contents($getfile));
 $name = str_replace('ADM','txt',$logfile['name']);
 $newfile = $name;
 $file = fopen($newfile, 'rb');
 $output = fopen('tmp/test.txt', 'wb');
 fputs($output, $file);
 fclose($output);
 fclose($file);
}

Its downloading each and renaming however its not moving the content & giving me this error

 Warning: fputs() expects parameter 2 to be string, resource given in 

CodePudding user response:

The second parameter of fputs is the content of a file. Not a file resource.

Instead of

$file = fopen($newfile, 'rb');

You‘ll need to get the contents of the file

$content = file_get_contents($newfile);

Or, if you like to use fopen, you can use fread:

$file = fopen($newfile, 'rb');
$content = fread($fp, filesize($newfile));

Then you can put this content into another file:

fputs($output, $content);
  • Related