【问题标题】:Is it possible to Copy images from web to your Hard Drive?是否可以将图像从网络复制到硬盘?
【发布时间】:2010-12-20 10:40:37
【问题描述】:

我正在尝试创建一个仅供个人使用的页面。我想要做的是,创建一个系统,通过该系统我可以直接通过像 WAMP 这样的 localhost 提供链接来将图像下载到我的本地硬盘。我想做的原因是,我希望图像自动分类到我的硬盘上。我的表单字段将是这样的

<form method="POST" action="process.php">
   <input type="text" name="URL" id="URL" />
   <input type="text" name="category" id="category" />
   <input type="text" name="subcategory" id="category" />
   <input type="submit">
</form>

现在,在process.php

//This is just a sample... So please ignore the roughness of the coding

copy($_POST['url'],$_POST['category']."/".$_POST['subcategory']."/somename.jpg");

// If you noticed, the categories and subcategories are actually the name of directory, where I want the picture to go be saved....

我认为我的方法是错误的。我怎样才能实现这样的目标?

错误

Warning: copy(images/image.jpg) [function.copy]: failed to open stream: No such file or directory in

【问题讨论】:

  • copy(source, destination),你的目的地在哪里?
  • @ajreal,请仔细阅读这个问题,我相信你会找到上面提到的$_POST['category']."/".$_POST['subcategory']."/somename.jpg" 的目的地..
  • ,您的代码使用的是相对路径
  • @ajreal,你有什么建议,我可以使用http://localhost/myimages/images/somename.jpg 作为我的目的地
  • NO,你应该使用绝对路径,并确保目录已经创建并且你的服务器有足够的权限写入

标签: php curl download file-management


【解决方案1】:

如果 allow_url_fopen 在 php.ini 中为 true,并且您的 PHPVERSION 为 >= 4.3.0,则您的代码应该可以工作。

【讨论】:

  • 唯一的问题似乎是你的路径。在您的 download.php 脚本所在的目录中,必须是您可以通过表单设置的目录...
  • 我认为复制功能只能从主机服务器复制文件。我说得对吗?
  • @Simer:可以使用copy() 从远程主机复制文件;-)
【解决方案2】:

好的,我使用 curl 来实现我的期望。这是一段代码

$img = $_POST['url'];
$fullpath = $_POST['category']."/".$_POST['subcategory']."/".basename($img);
$ch = curl_init ($img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);                   
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);

【讨论】:

    【解决方案3】:

    你可以先从指定的url从php脚本文件下载,然后给http-client链接下载本地存储的文件。或者将文档内容放到输出流中:

     $cont = file_get_contents($_GET['url']);
      header('Content-Type: application/octet-stream');
      header("Content-Transfer-Encoding: binary ");
      header('Accept-Ranges: bytes');
      header('Content-disposition: attachment; filename=' . $new_file_name));
      echo($cont);
    

    【讨论】:

    • 此代码是否提供了一个下载选项而不是保存文件。我希望文件自动保存到指定目录。
    • 不,我的服务器 php-script 无法直接保存。从系统安全的角度来看,这并不安全。只能通过下载。或在本地使用 php 脚本。从类似 cmd 的 php.exe 运行 download_img.php
    猜你喜欢
    • 2013-08-15
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 2015-03-07
    • 2015-04-26
    • 2012-07-10
    • 2023-04-04
    相关资源
    最近更新 更多