【问题标题】:download image from wamp server and save in my folder loacated in same using php从 wamp 服务器下载图像并使用 php 保存在我的文件夹中
【发布时间】:2012-07-08 13:59:23
【问题描述】:

你好朋友,我想将图像从我的 wamp 服务器保存到我在下面的代码中使用的特定 wamp 项目文件夹,这段代码向我显示了保存图像的对话框,但我想从 wamp 服务器调用图像并将其保存在我的文件夹中

<?php 
    header('Content-type: image/png');
    $imageUrl = "http://ip/demo/images/itemimage/Platters.png";
    $filename = realpath(dirname("http://ip/demo/images/itemimage/Platters.png"))."/image1.png";
    $handle = file_get_contents($imageUrl); 
    file_put_contents($filename, $handle);

?>

此代码不会自动将图像下载到我的文件夹中,任何人都可以帮助我吗? 我用上面的代码得到了这个错误

【问题讨论】:

  • 在同一台服务器上??为什么不直接使用copy
  • 测试我使用相同的服务器然后将从服务器到我的设备,然后我将更改下载位置
  • var_dump(file_get_contents($imageUrl)); 输出什么?记得删除header进行调试。
  • 你应该使用本地文件路径而不是 url
  • $handle = file_get_contents($imageUrl);echo($handle) 在 echo 中给我我的图像只是这张我想保存在我的文件夹中的图像我编辑过的问题请检查

标签: php image download save directory


【解决方案1】:

问题是您在脚本上做了两件事。您将图像设置为通过 php 脚本传递图像。

<?php 
    header('Content-type: image/png');
    echo file_get_contents("http://ip/demo/images/itemimage/Platters.png");
?>

然后您应该获取图像并将带有回声的图像发送给用户。

当您想保存图像时,您只需使用 file_get_contents 获取图像,然后将它们放入您的硬盘中。

我认为在您的示例中 echo missing 这就是您收到错误的原因。您设置了网站的内容类型,但不提供任何 png 数据。

<?php 
    header('Content-type: image/png');
    $handle = file_get_contents("http://ip/demo/images/itemimage/Platters.png"); 
    file_put_contents(dirname(__FILE__).md5(time()), $handle);
    echo $handle;
?>

编辑:试试这样的。然后他应该将图像保存在相同的路径中。

【讨论】:

  • CO: file_get_contents 不是从另一台服务器获取内容的最佳实践,因为数据会全部放入内存中。但在这种情况下,它可能会被使用。
  • 回显图像信息。出现此错误是因为您没有向浏览器发送任何数据。
  • 好的,我已经这样做了,但我无法将图像下载到位于服务器(同一服务器)上的文件夹中,如果我提供任何驱动器的本地路径,它工作正常
【解决方案2】:

一种方式:

exec("wget http://ip/demo/images/itemimage/Platters.png /tmp/demo/images/itemimage/Platters.png");

【讨论】:

  • 另外,假设你有一些错误,你可以看到,因为使用 image-header。检查您的日志文件
猜你喜欢
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-20
相关资源
最近更新 更多