mnybud Posted July 2, 2007 Share Posted July 2, 2007 Hi I am new to PHP and trying to extract images from a website url and save the image file to a directory on my server. So for example I would like to be able to take a url like this: http://www.google.com/intl/en_ALL/images/logo.gif and save the image "logo.gif" to a directory on my server. Can anyone help a newbie out with this? Quote Link to comment https://forums.phpfreaks.com/topic/58108-extract-image-and-save-to-directory/ Share on other sites More sharing options...
mnybud Posted July 2, 2007 Author Share Posted July 2, 2007 anyone? I know this cant be that hard. I just want to grab selected ebay auction images to add to my site. Any help is appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/58108-extract-image-and-save-to-directory/#findComment-288139 Share on other sites More sharing options...
john010117 Posted July 2, 2007 Share Posted July 2, 2007 Please bump your topic after at least an hour has passed. We all have lives, you know... Quote Link to comment https://forums.phpfreaks.com/topic/58108-extract-image-and-save-to-directory/#findComment-288143 Share on other sites More sharing options...
kathas Posted July 2, 2007 Share Posted July 2, 2007 <?php $website_contents = // the site's contents as string... maybe file_get_contents(); $has_matched = preg_match_all('/<img.*src="(.*)"/s',$website_contents,$matches); if ($has_matched) { var_dump($matches); //you will know how to get them after you see the structure of the $matches var } ?> The code is not tested but it propably works... if it doesn't repost here and i will correct it... Btw do not repost asking for an answer every 20 mins... Wait and you will get your answer... The expressions can be improved... this was just to give you an idea of how is this done... Edit: was posting did not see john's post he is right Quote Link to comment https://forums.phpfreaks.com/topic/58108-extract-image-and-save-to-directory/#findComment-288155 Share on other sites More sharing options...
mnybud Posted July 2, 2007 Author Share Posted July 2, 2007 Ok sorry for the fast bump. So I am sorry I am new to this. Can you please explain in a bit more detail how to configure your script. For example if I wanted to grab this image: http://i21.ebayimg.com/07/i/000/a7/63/131f_12.JPG and save it to a folder on my server. What would I need to change? Can you give me a working example? Sorry I am a bit slow Quote Link to comment https://forums.phpfreaks.com/topic/58108-extract-image-and-save-to-directory/#findComment-288209 Share on other sites More sharing options...
kathas Posted July 2, 2007 Share Posted July 2, 2007 you 're not slow you 're lazy.... the code i gave would be responsible for extracting the url of the images tha exist in the <img /> tag of a provided text... it is not responsible for saving the images locally... In few words all you need to do is extract the urls and then using the same way that you got the page's source code (file_get_contents(),file(),cURL,whatever) take the image's content and save it locally... if i were you i would take a look into preg_match_all() and generally regular expressions and then to the file system functions like fopen() or whichever you like... Those can be found in the php manual if you have any more questions or there is anything you don't understand ask here again... but dont ask for a working example before at least you try to make one yourself... Kathas Quote Link to comment https://forums.phpfreaks.com/topic/58108-extract-image-and-save-to-directory/#findComment-288217 Share on other sites More sharing options...
mnybud Posted July 2, 2007 Author Share Posted July 2, 2007 Well I have already grabbed the image url with my script, now I just need to save the image locally...this is the part I am confused by. I honestly dont get how your example works and I have tried it using other ways I found by searching the forums with no success.....lastly using this format... file_put_contents('file_name.jpg', file_get_contents('http://host.com/image_file.png')); not trying to be lazy just getting frustrated.... I will continue looking for the solution..... Quote Link to comment https://forums.phpfreaks.com/topic/58108-extract-image-and-save-to-directory/#findComment-288228 Share on other sites More sharing options...
kathas Posted July 2, 2007 Share Posted July 2, 2007 simply ... use fopen() to create a new file named the same as the image... the write into the file you created the string that you got from reading the image with file_get_contents()... that's all... Quote Link to comment https://forums.phpfreaks.com/topic/58108-extract-image-and-save-to-directory/#findComment-288274 Share on other sites More sharing options...
mnybud Posted July 2, 2007 Author Share Posted July 2, 2007 well I still dont get it but I guess I am just to green for this forum since you only point me in a vague direction which is where i was to begin with......remember I am a newbie :-\ thanks anyways, I will just keep reading....... Quote Link to comment https://forums.phpfreaks.com/topic/58108-extract-image-and-save-to-directory/#findComment-288292 Share on other sites More sharing options...
kathas Posted July 2, 2007 Share Posted July 2, 2007 ok I 'll give u the answer u want... although the point was to find it yourself... <?php // you said u have the urls so $image = file_get_contents($url); $i = strrpos($url,'/'); $name = substr($url,$i+1); file = fopen($name); if (!$file) // action for problem opening file $write = fwrite($file,$image); if ($write === FALSE) // action problem writing to file fclose($file); ?> now u can use this code but you learned nothing... Quote Link to comment https://forums.phpfreaks.com/topic/58108-extract-image-and-save-to-directory/#findComment-288311 Share on other sites More sharing options...
mnybud Posted July 3, 2007 Author Share Posted July 3, 2007 Actually no I learned a lot. Now I know how to do it and apply it properly in the future. I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/58108-extract-image-and-save-to-directory/#findComment-288419 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.