下载文件、更改扩展名并合并为 1 个文件

发布于 2025-01-11 13:33:34 字数 659 浏览 3 评论 0原文

尝试通过 URL 下载文件,将 .ADM 扩展名重命名为 .txt 然后将每个文件的内容放入一个txt文件中 然而它说 fputs 参数 2 是一个资源 $logfile['name'] 是存储在数组中的文件名

这是我的代码

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);
}

它下载每个文件并重命名,但是它不移动内容和内容给我这个错误

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

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2025-01-18 13:33:35

fputs 的第二个参数是文件的内容。不是文件资源。

而不是

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

Youll need to get the content of the file

$content = file_get_contents($newfile);

或者,如果您喜欢使用 fopen,您可以使用 fread

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

然后您可以将此内容放入另一个文件中:

fputs($output, $content);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文