duanchi19820419 2012-08-07 09:13
浏览 211

无法获取远程文件名到file_get_contents()然后存储文件

I want to download a remote file and put it in my server directory with the same name the original has. I tried to use file_get_contents($url).

Problem is that the filename isn't included in $url, it is like: www.domain.com?download=1726. This URL give me, e.g.: myfile.exe, so I want to use file_put_contents('mydir/myfile.exe');.

How could I retrieve the filename? I tried get_headers() before downloading, but I only have file size, modification date and other information, the filename is missing.

  • 写回答

3条回答 默认 最新

  • drexlz0623 2012-08-07 09:21
    关注

    file_get_contents() over the HTTP wrapper does not directly download the file if the file has been pre-parsed by the webserver.

    Take the following example: if you call file_get_contents() on a remove webpage (example.com/foobar.php), you won't be presented with the source code of foobar.php, but how the webserver of example.com parses the PHP file. So you will only be able to retrieve the generated HTML output.

    If the filename is not present in the URL and there is no way that you can fetch it from anywhere, then you are in a dead end. Data can't be just summoned from the transcendental field of datum.

    For alternative solutions, I can only suggest using the cURL library (it is used to handle queries from your server (as it were a client) to other servers with using URL, hence the name cient URL) or file sockets. Here is another question's answer on Stack Overflow which describes how to fetch filename using cURL.

    Also, you might try getting in touch with the administrator/maintainer/webmaster team of domain.com, asking if they had a publicly available API to fetch filenames and other metadata.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题