【问题标题】:Downloading a file and saving it locally with PHP下载文件并使用 PHP 将其保存在本地
【发布时间】:2014-01-08 19:23:08
【问题描述】:

我需要从外部 url 保存一个压缩文件并将其保存为临时文件以供使用。

有人建议我查看 tempnam() 和 sys_get_temp_dir(),但我不确定如何(或在何处)包含外部文件的 url。

谁能帮我指出正确的方向来使用正确的功能(以及如何使用)?我知道如何解压缩文件,它只是包含指向压缩文件等的外部 url 的位置。

【问题讨论】:

  • 所以问题是我如何从外部站点下载文件?

标签: php url zip temporary-files


【解决方案1】:

您可以简单地使用file_get_contents() 加载远程文件并使用file_put_contents() 将内容保存到临时文件。 您必须在 php.ini 中启用 allow_url_fopen

file_put_contents(
    'path/to/tmp/file.ext',
    file_get_contents( 'http://url_of_the_file.com/download.zip' )
);

【讨论】:

    【解决方案2】:

    您可以使用file_put_contents file_put_contents("Tmpfile.zip", fopen("yoururlwithhttp/file.zip", 'r'));

    第一个参数是要保存的文件名。如果您愿意,您可以从 tmp 文件夹中获取它,或者您可以使用带有当前时间戳的 php rename

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2014-02-26
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      • 1970-01-01
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多