You've a form like this:
<form>
URL: <input name="url">
<input type="submit">
</form>
Then you can retrieve the url submitted with $_GET['url']
:
$image = file_get_contents($_GET['url']);
file_put_contents('/images/image.jpg', $image);
Anyhow, you must be careful on Local File Inclusion, because without further checks the users can choose path like /etc/passwd
, ../configuration.php
, etc, and if /images/image.jpg
can be view by the users, they can read file that shouldn't see.
A thing that you may want do is check if the $_GET['url
]` is a valid url. You can do that with:
if (!filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
throw new \InvalidArgumentException('The url is not valid.');
}
but that's not enough since file:///etc/passwd
is a valid url. So, instead, just be sure that the url start with http://
or 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
}