Page 1 of 1
Text file created when jpeg desired
Posted: Wed Apr 01, 2009 4:25 pm
by Homer Simpson
I am trying to copy a file from a remote server to my server. But, when I use fopen to create the file on the server, it creates a text file instead of an image file. Opening urls from fopen is turned on.
$url is defined. this is only part of the code.
Code: Select all
$localfile ='images/'.rand().'.jpg';
$fp = fopen($localfile, 'w+');
fwrite($fp, file_get_contents($url));
fclose($fp);
Re: Text file created when jpeg desired
Posted: Wed Apr 01, 2009 4:48 pm
by Christopher
Not sure, but you might want to check to be sure that you are writing in binary mode and not text mode. I am assuming that $url contains a valid URL to an image and is not getting an error or HTML page.
Re: Text file created when jpeg desired
Posted: Wed Apr 01, 2009 6:56 pm
by Homer Simpson
arborint wrote:Not sure, but you might want to check to be sure that you are writing in binary mode and not text mode. I am assuming that $url contains a valid URL to an image and is not getting an error or HTML page.
I changed the mode for fopen to 'wb' but it had the same result. The jpeg file shows the URL instead of an image.
Edit: Figured it out. The text that appears is put there by mozilla. That means that the file does not contain any content. so file_get_contents() returns null.