是否可以将图像从网络复制到硬盘?

时间:2010-12-20 10:40:38

标签: php curl download file-management

我正在尝试创建一个仅供个人使用的页面。我想要做的是,通过像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

3 个答案:

答案 0 :(得分:1)

如果php.ini中的allow_url_fopen为真且您的PHPVERSION为&gt; = 4.3.0,则您的代码应该有效。

答案 1 :(得分:1)

好吧,我用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);

答案 2 :(得分:0)

你可以先从指定的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);