通过 php 将 URL 中的图像保存在表单中
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要从文本区域中提取 url,然后对其进行循环:
显然,您应该清理、验证并仔细检查输入的信息:不仅要避免 Goatses,还要避免破坏应用程序的条目(例如白色线)。
You need to extract the urls from a text-area and then loop over that:
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).