Reputation: 4979
im considering to create a resource file in php to read other file type??
for instance, instead of calling the image / javascript directly, i shall pass a query string to this resource.php indicating the type of file, and it would perform the same function as it is originally.
for instance:
using resource.php?type=js&file=alertme.js
which would read alertme.js that alert("hello world");
Upvotes: 1
Views: 130
Reputation: 490253
Sounds like you want..
<?php
if ( ! isset($_GET['type']) OR ! isset($_GET['file'])) {
die('missing params');
}
$type = $_GET['type'];
$file = $_GET['file'];
if ( ! $file = realpath($file)) {
die('file does not exist');
}
// Make sure the path does not go above our specific folder
$maxParentFolder = '/home/user/public_html/';
if ( ! preg_match('/^' . preg_quote($maxParentFolder, '/') . '/', $file)) {
die('invalid path');
}
$mime = getMimeByExt($type);
header('Content-Type: ' . $mime);
readfile($file);
You need a function to get MIME types. Look here for further info.
Also, if type
will always be the same as the file extension, you can omit it and get the extension using $ext = pathinfo($file, PATHINFO_EXTENSION)
.
Also, die()
isn't the most useful error reporting. I have used it here just for an example.
Upvotes: 3