Text file created when jpeg desired

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
Homer Simpson
Forum Newbie
Posts: 2
Joined: Wed Apr 01, 2009 4:21 pm

Text file created when jpeg desired

Post 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);
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Text file created when jpeg desired

Post 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.
(#10850)
Homer Simpson
Forum Newbie
Posts: 2
Joined: Wed Apr 01, 2009 4:21 pm

Re: Text file created when jpeg desired

Post 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.
Post Reply