Page 1 of 1

Copy .png file from external server to webserver

Posted: Fri Nov 05, 2010 2:07 pm
by drew2727
I'm trying to copy a .png image file from an external server to use on my server. This is because certain browsers don't like me pulling in info from domains that are not part of the current domain structure. But every method i've tried gives me a stream error: failed to open stream: Connection timed out in...yada yada yada
I've chaned the folder i'm trying to copy to, to chmod 777. I've run phpinfo() and safe_mode is off and allow_url_fopen is on. still nothing. Below are some of the things i've tried.
:banghead:

Code: Select all

<?php
function LoadPNG($imgname)
{
    //Attempt to open
    $im = @imagecreatefrompng($imgname);

    //See if it failed
    if(!$im)
    {
        //Create a blank image
        $im  = imagecreatetruecolor(150, 30);
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);

        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);

        //Output an error message
        imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
    }

    return $im;
}

header('Content-Type: image/png');

$img = LoadPNG('http://www.somesite.com:81/folder/some.png');

imagepng($img);
imagedestroy($img);
?>

Code: Select all

<?php copy('http://www.somesite.com:81/folder/some.png', 'some_image/some.png'); ?>

Code: Select all

<?php
header("Content-type: image/png");
$im = imagecreatefrompng("http://www.somesite.com:81/folder/some.png");
imagepng($im, 'some_image/some.png');
imagedestroy($im);*/
?>

Code: Select all

<?php
function copyFile($url,$dirname){
    @$file = fopen ($url, "rb");
    if (!$file) {
        echo"<font color=red>Failed to copy $url!</font><br>";
        return false;
    }else {
        $filename = basename($url);
        $fc = fopen($dirname."$filename", "wb");
        while (!feof ($file)) {
           $line = fread ($file, 1028);
           fwrite($fc,$line);
        }
        fclose($fc);
        echo "<font color=blue>File $url saved to PC!</font><br>";
        return true;
    }
}
copyFile("http://www.somesite.com:81/folder/some.png","some_image/some.png");
?>

Re: Copy .png file from external server to webserver

Posted: Fri Nov 05, 2010 2:57 pm
by AbraCadaver
I would try this:

Code: Select all

file_put_contents("some_image/some.png", file_get_contents("http://www.somesite.com:81/folder/some.png"));

Re: Copy .png file from external server to webserver

Posted: Fri Nov 05, 2010 3:15 pm
by drew2727
Thanks for the reply, unfortunately the server i'm hosting on is still at PHP Version 4.4.9

I did try it on my localhost (which has the option for php5) and it worked beautifully. Theoretically i should be able to call fopen(), fwrite() and fclose() to accomplish the same thing. I'll go give that a shot now.

Re: Copy .png file from external server to webserver

Posted: Fri Nov 05, 2010 4:12 pm
by AbraCadaver
drew2727 wrote:Thanks for the reply, unfortunately the server i'm hosting on is still at PHP Version 4.4.9

I did try it on my localhost (which has the option for php5) and it worked beautifully. Theoretically i should be able to call fopen(), fwrite() and fclose() to accomplish the same thing. I'll go give that a shot now.

Code: Select all

if(!function_exists('file_put_contents')) {
  function file_put_contents($filename, $data) {
    if(!$fh = fopen($filename, 'w')) {;
      return false;
    }
    $bytes = fwrite($fh, $data);
    fclose($fh);
    return $bytes;
  }
}

Re: Copy .png file from external server to webserver

Posted: Fri Nov 05, 2010 4:17 pm
by drew2727
Ok i'm close, but my file is writting to the public_html folder and I don't want to make that folder writable. I tried to break it out into a funciton, but it is still writing to the directory that it is called from. Do you know how I can make it write to another directory that I can make writable and protect with htaccess or something? Here is what I have currently:

Code: Select all

<?php 
require_once("includes/functions.php");
get_image();
?>

Code: Select all

<?php
function get_image() {
$newfilename ='2911.png';
$myfile = file_get_contents('http://www.somesite.com:81/folder/some.png');
$newfile = fopen($newfilename , 'w');
fwrite($newfile , $myfile);
fclose($newfile);
}
?>

Re: Copy .png file from external server to webserver

Posted: Fri Nov 05, 2010 4:20 pm
by AbraCadaver
Well, you have to give it a path. If under public_html you make a dir called images and chmod it, then:

Code: Select all

$newfilename ='images/2911.png';
Or anywhere else:

Code: Select all

$newfilename ='/home/images/2911.png';

Re: Copy .png file from external server to webserver

Posted: Sat Nov 06, 2010 7:49 am
by drew2727
yeah...i don't get it. It works locally, but when I put it on the server it just gives me a timeout when it is trying to grab the file. I tried this:

Code: Select all

set_time_limit(0); 
still nothing.

I tried making my root directory(not really the root public_html directory) writable and then when I checked my error log I had this error:
[error] [client 24.160.105.249] Premature end of script headers: