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

发布于 2024-10-08 03:05:32 字数 965 浏览 0 评论 0原文

我正在尝试创建一个仅供个人使用的页面。我想要做的是,创建一个系统,通过该系统我可以将图像下载到本地硬盘,直接通过 localhost(如 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>

现在,在 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

I am trying to create a page for just personal use. What I want to do is, create a system through which I can download images to my local hard disk, directly by providing the link through a localhost like WAMP. The reason I am trying to do is, I want the images to be automatically sorted onto my hard disk. My form field will be something like this

<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>

Now, in the 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....

I am thinking my approach is wrong. How can I achieve something like this?

Error

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎夕旧梦 2024-10-15 03:05:32

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

If allow_url_fopen is true in the php.ini and your PHPVERSION is >= 4.3.0 your code should work.

依 靠 2024-10-15 03:05:32

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

Ok, I used curl instead to achieve what i am expecting. Here is the piece of code

$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);
走过海棠暮 2024-10-15 03:05:32

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

you can first download from php script document from specified url, then give http-client link to download locally stored file. or put document content to output stream:

 $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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文