chrizonline
chrizonline

Reputation: 4979

how to access other file type through php

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

Answers (2)

alex
alex

Reputation: 490253

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.

Upvotes: 3

deceze
deceze

Reputation: 522091

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

Upvotes: 1

Related Questions