saving a dynamically generated image to database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RCstp
Forum Newbie
Posts: 6
Joined: Fri Mar 25, 2011 1:09 pm

saving a dynamically generated image to database

Post by RCstp »

I have php code to get data from a database and put data into the database. The database is mySQL. The url below dynamically generates an image. I would like to save that image and put it into the database. Any ideas for saving the image that I see on the browser? If I right-click the image in the browser, I can use 'save-as' to save it to my hard drive, but I would like to do this through code.

http://chart.finance.yahoo.com/t?s=MMM& ... height=180

Thanks for your advice.
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: saving a dynamically generated image to database

Post by fugix »

try using simply the copy() function...copy(file,to_file)
RCstp
Forum Newbie
Posts: 6
Joined: Fri Mar 25, 2011 1:09 pm

Re: saving a dynamically generated image to database

Post by RCstp »

do you mean like this:
copy(http://chart.finance.yahoo.com/t?s=MMM& ... height=180,http://mywebsite.com/test.gif)
or
copy(http://chart.finance.yahoo.com/t?s=MMM& ... height=180,c:\mypictures\test.gif)

maybe like this ??

<?php
$contents = file_get_contents('http://chart.finance.yahoo.com/t?s=MMM& ... height=180');
$newcopy = "http://mywebsite.com/test.gif";
copy($contents,$newcopy);
?>
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: saving a dynamically generated image to database

Post by fugix »

yes I believe that the last snippet of code that you have there should work
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: saving a dynamically generated image to database

Post by litebearer »

Or...

Code: Select all

<?PHP
$file1="http://chart.finance.yahoo.com/t?s=MMM&";
$file2=file_get_contents($file1);
file_put_contents("mmm.png", $file2);
?>
<img src="mmm.png" alt="">

Post Reply