markof cheney Posted October 28, 2010 Share Posted October 28, 2010 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); } ?> Link to comment https://forums.phpfreaks.com/topic/217142-php-code-to-transfer-images-to-my-server/ Share on other sites More sharing options...
markof cheney Posted October 29, 2010 Author Share Posted October 29, 2010 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); } ?> Link to comment https://forums.phpfreaks.com/topic/217142-php-code-to-transfer-images-to-my-server/#findComment-1127843 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.