你已经像这样的形式:
<form>
URL: <input name="url">
<input type="submit">
</form>
然后你可以检索与$_GET['url']
提交的网址:
$image = file_get_contents($_GET['url']);
file_put_contents('/images/image.jpg', $image);
无论如何,你一定要小心在本地文件包含,因为没有进一步的检查用户是否可以选择路径如/etc/passwd
,../configuration.php
等,如果/images/image.jpg
可以由用户查看,他们可以读取不应该看到的文件。
你可能想要做的事情是检查$_GET['url
]是否是一个有效的url。你可以这样做:
if (!filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
throw new \InvalidArgumentException('The url is not valid.');
}
但这还不够,因为file:///etc/passwd
是一个有效的网址。所以,相反,只要确保网址以http://
或https://
开头。
$isValid = false;
foreach(['http://', 'https://'] as $schema) {
if (strpos($_GET['url'], $schema) === 0) {
$isValid = true;
break;
}
}
if ($isValid && filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
$image = file_get_contents($_GET['url']);
file_put_contents('/images/image.jpg', $image); //Where to save the image on your server
}
@ abrad1212,'$ image = file_get_contents($ _ GET ['url']);',但只有在清理完$ _GET ['url']'避免提交'?url = index.php'或类似人的问题 – Federkun
如果图像较大,则会导致内存问题。 –
看看这个:http://stackoverflow.com/questions/724391/saving-image-from-php-url – Andreas