Skip to content

Instantly share code, notes, and snippets.

@tranchausky
Last active December 22, 2023 03:27
Show Gist options
  • Save tranchausky/65520b6bf649361e31b32af6dcbc32b2 to your computer and use it in GitHub Desktop.
Save tranchausky/65520b6bf649361e31b32af6dcbc32b2 to your computer and use it in GitHub Desktop.
php return image from link if not have in server (help download image from server to dev environment)
RewriteEngine On
ErrorDocument 404 /public/404.php
<?php
//http://localhost:90/website1/public/menuActiveArrow.png
//https://www.tumlumlink.com/public/menuActiveArrow.png
$request_url = $_SERVER['REQUEST_URI'];
$character = '/public/';
if (strpos($request_url, $character) !== false) {
$bodytag = str_replace($character, "", $request_url);
//http://localhost:90/website1/public/menuActiveArrow.png
$file = 'https://www.tumlumlink.com/public/' . $bodytag;
return_image_content($file, $bodytag);
}
function return_image_content($file_out, $bodytag)
{
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $file_out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// grab URL and pass it to the browser
$urlContent = curl_exec($ch);
if (!curl_errno($ch)) {
$info = curl_getinfo($ch);
header('Content-Type: '.$info['content_type']);
echo $urlContent;
}
$page_directory = dirname($bodytag);
if (!file_exists($page_directory)) {
mkdir($page_directory);
}
// var_dump($page_directory);die;
file_put_contents($bodytag, $urlContent);
curl_close($ch);
die;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment