Copy .png file from external server to webserver

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
drew2727
Forum Newbie
Posts: 4
Joined: Fri Nov 05, 2010 8:43 am

Copy .png file from external server to webserver

Post 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");
?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Copy .png file from external server to webserver

Post 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"));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
drew2727
Forum Newbie
Posts: 4
Joined: Fri Nov 05, 2010 8:43 am

Re: Copy .png file from external server to webserver

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Copy .png file from external server to webserver

Post 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;
  }
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
drew2727
Forum Newbie
Posts: 4
Joined: Fri Nov 05, 2010 8:43 am

Re: Copy .png file from external server to webserver

Post 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);
}
?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Copy .png file from external server to webserver

Post 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';
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
drew2727
Forum Newbie
Posts: 4
Joined: Fri Nov 05, 2010 8:43 am

Re: Copy .png file from external server to webserver

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