Jump to content

PHP Code To Transfer Images to My Server


markof cheney

Recommended Posts

Hello everyone.. This is the first PHP script I've written and was hoping to get some feedback on any possible issues with it. I've pieced this together in an attempt to download remote images and store them on my server, instead of hotlinking images. The code will be used for a forum, called up by a BBCode tag. (The user will place an image URL into the BBCode, which will transfer to this PHP script). Again, this is the first time I've coded anything in PHP and was hoping to get some pointers on anything that needs changing.. thanks

 

<?php

$url = $_GET['url'];
$url_path = parse_url($url, PHP_URL_PATH);
$name = basename($url_path);
$FileExt= substr($name, -3); 
$FileTypeMIME= array("jpg" => "image/jpeg",
                         "png" => "image/png",
                         "gif" => "image/gif");

$ContentType= $FileTypeMIME[$FileExt];
if (empty($ContentType)) die("You are not allowed to access this file!");

header("Content-Type: " . $ContentType); 

$save = "../images/". strtolower($name); 

function wtf_image ($file) {
    switch($FileTypeMIME[$FileExt]){
        case "image/jpeg":
            $im = imagecreatefromjpeg($file); //jpeg file
       imagejpeg($im, $save, 0, NULL);   //save jpeg file 
        break;
        case "image/gif":
            $im = imagecreatefromgif($file); //gif file
       imagegif($im, $save, 0, NULL);   //save gif file
      break;
      case "image/png":
          $im = imagecreatefrompng($file); //png file
     imagePNG($im, $save, 0, NULL);   //save png file
      break;
    }
    return $im;
}

if (file_exists($save)) { 
readfile($save);  
} else { 
chmod($save,0755); 
$image = wtf_image($url);
//Runs wtf_image function on $url
imagedestroy($image);   
readfile($save); 
} 
?>

Ok, tried it out and got it working after making a few modifications. In case it may be of use to anyone..

 

<?php
$url = $_GET['url'];
$url_path = parse_url($url, PHP_URL_PATH);
$name = basename($url_path);
$save = "./hotimages/". strtolower($name); 
$FileExt= substr($name, -3);
$FileTypeMIME= array("jpg" => "image/jpeg",
                         "png" => "image/png",
                         "gif" => "image/gif");

$ContentType= $FileTypeMIME[$FileExt];
if (empty($ContentType)) die("You are not allowed to access this file!");

header("Content-Type: " . $ContentType);

if (file_exists($save)) {
readfile($save); 
} else {
    switch($FileTypeMIME[$FileExt]){
        case "image/jpeg":
            $image = imagecreatefromjpeg($url); //jpeg file
       imagejpeg($image, $save);   //save jpeg file
        break;
        case "image/gif":
            $image = imagecreatefromgif($url); //gif file
       imagegif($image, $save);   //save gif file
      break;
      case "image/png":
          $image = imagecreatefrompng($url); //png file
     imagePNG($image, $save);   //save png file
      break;
    }
imagedestroy($image);   
readfile($save);
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.