Created
February 23, 2017 09:43
-
-
Save dannoso/9a815a848d2833533b4ab8a106fbbc1b to your computer and use it in GitHub Desktop.
On demand, dynamic, invisible remote files fetcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This is very useful when you have a broken html with missing files | |
* ie: you save an html page but you miss all the resources | |
* | |
* @author Davide Lattanzio | |
* @email [email protected] | |
* | |
*/ | |
/* | |
* Put this in your .htaccess | |
* | |
* <IfModule mod_rewrite.c> | |
* RewriteEngine On | |
* RewriteBase / | |
* RewriteRule ^index\.php$ - [L] | |
* | |
* # This hack loads the the remote original file if missing | |
* # Or fetch it locally the first time it called | |
* RewriteCond %{REQUEST_FILENAME} !-f | |
* RewriteCond %{REQUEST_FILENAME} !-d | |
* RewriteCond %{REQUEST_URI} \.(jpg|gif|png|mp4|m4v|ogv|webm)$ # modify on your needs | |
* # RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,QSA] | |
* RewriteRule ^(.*)$ /fetcher.php?path=%1 [L,QSA] | |
* | |
* | |
*/ | |
$origin = "http://www.domain.tld/"; | |
// Fetch the remote original file | |
$fileurl = substr($_SERVER['REQUEST_URI'], 1); | |
$ch = curl_init(); | |
$source = $origin . $fileurl; | |
curl_setopt($ch, CURLOPT_URL, $source); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$data = curl_exec($ch); | |
// get the mime type for the later redirect | |
$mimetype = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); | |
curl_close($ch); | |
// Save in the matching local path | |
mkdir(dirname($fileurl), 0755, 1); | |
$file = fopen($fileurl, "w+"); | |
fputs($file, $data); | |
fclose($file); | |
// redirect to original for now | |
header('Location: ' . $source); | |
// alternatively we can serve the file just saved | |
// header('Content-type: ' . $mimetype); | |
// print $data; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment