开发者

how to access other file type through php

im considering to create a resource file in php to read other file type??

for ins开发者_StackOverflow中文版tance, 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");


Sounds like you want..

resource.php

<?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.


I don't really know what the question is, but perhaps you're looking for readfile?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜