Save Images from a webpage using Javascript or PHP
I wrote a little javascript application with jquery which fetches a rss feed via the Google Feed API and displays it as a grid of images.
The code then looks like :
<ul>
...
<li><a class="entry" href="LINK_TO_BIG_IMAGE" title="TITLE_OF_ENTRY"><img class="entry-img" src="THUMB_IMG" alt="" /></a></li>
...
</ul>
What I want to do is to be able t开发者_如何转开发o save the images all at once in a folder on the server. For now I'd be happy if it does it on page load. Is it possible via Javascript ? Do you have some direction to point me to ?
Thanks in advance !
Your comment about submitting the paths to a php script on the server would work ( I wouldn't recommend doing it that way but it works).
Your php could then download the images like so (untested and you'd need to parse the filename from the $url when you go to save the file).
<?php
$image_urls = isset($_POST['image_urls']) ? $_POST['image_urls'] : null;//assume it's an array of urls
if (!count($image_urls))//check that our array actually contains 1 or more urls
{
die('no urls provided');
}
$image_folder_path = '/home/somefolder/';
foreach ($image_paths as $index => $url)
{
$file_name = $image_folder_path . $index . '.jpg';//you will need to parse the url for the file name and extension for this to be accurate
$image = file_get_contents($url);//if fopen wrappers is enabled you can do this otherwise use something like cUrl to fetch the file
file_put_contents($file_name, $image);
}
?>
精彩评论