Tried all possible methods to copy an image from URL to remote server in PHP.
this is what i am doing:
$fp = fopen("photo/pqr.png", "w");
$url = "http://raghavrao.com/homeimage/reasontorely/infra1.png";
$handle = curl_init();
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($handle, CURLOPT_FAILONERROR, true);
curl_setopt($handle, CURLOPT_FILE, $fp);
curl_exec($handle);
curl_close($handle);
fclose($fp);
it also tried it like this:
$content = file_get_contents("http://raghavrao.com/homeimage/reasontorely/infra1.png");
//Store in the filesystem.
$fpe = fopen("photo/image.png", "w");
fwrite($fpe, $content);
fclose($fpe);
and also like:
copy('http://raghavrao.com/homeimage/reasontorely/infra1.png', 'photo/file.png');
in all 3 ways, i am getting the image on my server, but the images are corrupted and the size of image copied through all 3 method is 857 bytes.
Help!
I have "allow_url_fopen=On" in my php.ini file also.
AND
I need to copy images over HTTPS sometimes.
EDIT:
opening the image file in text editor shows the following:
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("79970eac6e958aea6b0d6577c65d6dc2");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://raghavrao.com/homeimage/reasontorely/infra1.png?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
You will not be able to do this without a considerable amount of work.
If you look at the 857 byte file you acquired with the CURL you will see the following (I formatted this to make it easier to read):
<html>
<body>
<script type="text/javascript" src="/aes.js" ></script>
<script>
function toNumbers(d){
var e=[];
d.replace(/(..)/g,function(d) { e.push(parseInt(d,16)) });
return e
}
function toHex(){
for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;
f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);
return e.toLowerCase()
}
var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("9f38f758d71e0c6e0e935c8c90e0cce1");
document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+";
expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";
location.href="http://raghavrao.com/homeimage/reasontorely/infra1.png?i=1";
</script>
<noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript>
</body></html>
When your browser loads this it does some math and then sets a cookie with an appropriate value then redirects the request and serves up the image. This is a way for the site to check to see if you are actually using a web browser to view the site.
You could rewrite the JavaScript code in PHP, set the appropriate cookie in your CURL request, then go get your image.
Additional notes: The numbers generated in JavaScript changed based on your source IP.