通过 php 将 URL 中的图像保存在表单中

发布于 2024-10-27 17:09:34 字数 1388 浏览 7 评论 0原文

我有一个 php 脚本,它从外部 URL 获取图像,读取它并将其保存到我的服务器上的目录中。该脚本位于 php 文件中,包含:

<?php 
$image_url = "http://example.com/image.jpg"; 
$ch = curl_init(); 
$timeout = 0; 
curl_setopt ($ch, CURLOPT_URL, $image_url); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 

// Getting binary data 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 

$image = curl_exec($ch); 
curl_close($ch); 

$f = fopen('/home1/path/public_html/path/saved/image.jpg', 'w');
fwrite($f, $image);
fclose($f);
?>

一切都很好...

我想做的是让脚本为多个 URL 执行此操作。 URL 将写在表单文本区域中,用逗号(或其他)分隔。

然后,提交按钮会告诉脚本对表单中的所有 URL 执行此操作,并以任何名称保存它们,这并不重要(随机即可)。

我还是个新手,正在学习PHP。

预先感谢您的帮助!

编辑

我的代码现在看起来像这样:

<?php 
error_reporting(E_ALL);
$image_urls = explode('\n', $_POST['urls']); 



foreach ($image_urls as $image_url) {
$ch = curl_init(); 
$timeout = 0; 
curl_setopt ($ch, CURLOPT_URL, $image_url); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 

  $image = curl_exec($ch); 
  curl_close($ch); 

  $f = fopen('/home1/path/public_html/path/saved/'.rand().time().".jpg", 'w');
  fwrite($f, $image);
  fclose($f);

}
?>

它仅适用于第一个代码,并且不会返回任何错误...有什么想法吗?

感谢您的大力帮助!

I have a php script which grabs an image from an external URL, reads it and saves it into a directory on my server. The script is located in a php file and contains :

<?php 
$image_url = "http://example.com/image.jpg"; 
$ch = curl_init(); 
$timeout = 0; 
curl_setopt ($ch, CURLOPT_URL, $image_url); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 

// Getting binary data 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 

$image = curl_exec($ch); 
curl_close($ch); 

$f = fopen('/home1/path/public_html/path/saved/image.jpg', 'w');
fwrite($f, $image);
fclose($f);
?>

Everything works great there...

What I would like to do is have the script do it for multiple URLs. The URLs would be written in a form textarea, separated by comas (or else).

A submit button would then tell the script to do the trick with all the URLs in the form and save them with whatever name, it's not important (random would do fine).

I'm still a newbie, and I'm learning PHP.

Thanks in advance for your help !

EDIT

My code looks like this now :

<?php 
error_reporting(E_ALL);
$image_urls = explode('\n', $_POST['urls']); 



foreach ($image_urls as $image_url) {
$ch = curl_init(); 
$timeout = 0; 
curl_setopt ($ch, CURLOPT_URL, $image_url); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 

  $image = curl_exec($ch); 
  curl_close($ch); 

  $f = fopen('/home1/path/public_html/path/saved/'.rand().time().".jpg", 'w');
  fwrite($f, $image);
  fclose($f);

}
?>

It works only for the first one, and doesn't return any errors... Any ideas ?

Thanks for your great help !

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

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

发布评论

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

评论(1

我是有多爱你 2024-11-03 17:09:34

您需要从文本区域中提取 url,然后对其进行循环:

<?php 
$image_urls = explode('\n', $_POST['urls']); # Will create a list of urls, if each line contains one url.

#Basic settings and initializers need to be ran only once. 
$sequencer = 1;
$timeout = 0;

foreach ($image_urls as $image_url) {
  $ch = curl_init(); 

  curl_setopt ($ch, CURLOPT_URL, $image_url); 
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 

  // Getting binary data 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 

  $image = curl_exec($ch); 
  curl_close($ch); 

  $f = fopen("/home1/path/public_html/path/saved/image_$sequencer.jpg", 'w');
  fwrite($f, $image);
  fclose($f);
  $sequencer++;
}
?>

显然,您应该清理、验证并仔细检查输入的信息:不仅要避免 Goatses,还要避免破坏应用程序的条目(例如白色线)。

You need to extract the urls from a text-area and then loop over that:

<?php 
$image_urls = explode('\n', $_POST['urls']); # Will create a list of urls, if each line contains one url.

#Basic settings and initializers need to be ran only once. 
$sequencer = 1;
$timeout = 0;

foreach ($image_urls as $image_url) {
  $ch = curl_init(); 

  curl_setopt ($ch, CURLOPT_URL, $image_url); 
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 

  // Getting binary data 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 

  $image = curl_exec($ch); 
  curl_close($ch); 

  $f = fopen("/home1/path/public_html/path/saved/image_$sequencer.jpg", 'w');
  fwrite($f, $image);
  fclose($f);
  $sequencer++;
}
?>

Obviously, you should clean out, validate and doublecheck the inputted information: not only to avoid Goatses, but also to avoid entries that break your application (such as white lines).

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