Php 是否可以将图像从web复制到硬盘?

Php 是否可以将图像从web复制到硬盘?,php,curl,download,file-management,Php,Curl,Download,File Management,我正在尝试创建一个仅供个人使用的页面。我想做的是,创建一个系统,通过它我可以直接将图像下载到本地硬盘,方法是通过本地主机提供链接,比如WAMP。我尝试这样做的原因是,我希望图像自动排序到我的硬盘上。我的表单字段将是这样的 <form method="POST" action="process.php"> <input type="text" name="URL" id="URL" /> <input type="text" name="category"

我正在尝试创建一个仅供个人使用的页面。我想做的是,创建一个系统,通过它我可以直接将图像下载到本地硬盘,方法是通过本地主机提供链接,比如WAMP。我尝试这样做的原因是,我希望图像自动排序到我的硬盘上。我的表单字段将是这样的

<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>
我认为我的方法是错误的。我怎样才能做到这一点

错误
如果php.ini中的
allow\u url\u fopen
为true,并且您的
PHPVERSION
为>=4.3.0,那么您的代码应该可以工作。

您可以首先从指定的url下载php脚本文档,然后提供http客户端链接以下载本地存储的文件。或将文档内容放入输出流:

 $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);

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

这段代码不是提供了一个donwload选项而不是保存文件吗。我希望文件自动保存到指定的目录。不,直接保存是不可能的,我的服务器php脚本的手段。从系统安全的角度来看,这是不安全的。只能通过下载。或者在本地使用php脚本。像php.exe一样从cmd运行下载_img.php
copy(source,destination)
,你的目的地在哪里?@ajreal,请仔细阅读这个问题,我相信你会发现上面提到的目的地是
$\u POST['category']。“/”$\u POST['subcategory']。/somename.jpg”
。不,你的代码使用的是相对的path@ajreal,你的建议是什么,我应该使用
http://localhost/myimages/images/somename.jpg
作为我的目标否,您应该使用绝对路径,并确保已创建目录,并且您的服务器具有足够的写入权限。唯一的问题似乎是您的路径。在download.php脚本所在的目录中,必须是可以通过表单设置的目录……我认为复制功能只能从主机服务器复制文件。我说的对吗?@Simer:可以使用
copy()
;-)从远程主机复制文件
 $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);
$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);