Home > database >  Loading ANY Content Through PHP Script
Loading ANY Content Through PHP Script

Time:06-29

I have this script:

<?php
$id = $_GET["id"];
switch($id)
{
    default: echo "Bad ID!"; exit;
    case "MyPicture1": $file = "img/img1.jpg"; break;
    case "MyPicture2": $file = "img/img2.jpg"; break;
    case "MyPicture3": $file = "img/img3.jpg"; break;
    case "MyPicture4": $file = "img/img4.jpg"; break;
}
if(file_exists($file))
{
    $size = getimagesize($file);
    $fp = fopen($file, 'rb');
    if($size and $fp)
    {
    header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($file)).' GMT');
    header('Content-Type: '.$size['mime']);
    header('Content-Length: '.filesize($file));
    fpassthru($fp);
    }
    exit;
    }
    else
    {
    echo "Bad ID!";
}
?>

Which works fine for loading images like this <IMG Src="/Script.php?id=MyPicture1" />!

But the problem occurs when I try to load ANY other MIME content, to be displayed directly on the page or to switch page with it, but unsuccessfully... I tried with:

<?php
$file_name = $_GET["id"];
switch($file_name)
{
    default: echo 'Bad ID!'; exit;
    case "style1": $file_name = "csss/style_one.css"; break;
    case "style2": $file_name = "csss/style_two.css"; break;
    case "image1": $file_name = "images/image_one.jpg"; break;
    case "image2": $file_name = "images/image_one.png"; break;
    case "image3": $file_name = "images/image_one.bmp"; break;
    case "image4": $file_name = "images/image_one.gif"; break;
    case "video1": $file_name = "videos/video_one.avi"; break;
    case "video2": $file_name = "videos/video_two.mp4"; break;
    case "video3": $file_name = "videos/video_three.mov"; break;
    case "php_page_1": $file_name = "php_pages/php_one.php"; break;
    case "html_page_1": $file_name = "html_pages/html_one.html"; break;
    case "icon_fav_1": $file_name = "icons/icon_one.ico"; break;
    case "font_1": $file_name = "fonts/font_one.otf"; break;
}
if(file_exists($file_name))
{
    $size = getImageSize("$file_name");
    $fP = fOpen("$file_name", "rb");
    if($size and $fP)
    {
    header('last-modified: '.gmdate('D, d M Y H:i:s', filemtime($file_name)).' GMT');
    header('content-type: '.$size['mime'].');
    header('content-length: '.filesize($file_name).);
    fPassThru($fP);
    }
    exit;
}
else
{
    echo 'Bad ID!';
}
?>

If I load CSS sheet like /Script.php?id=MyCSS1, I want CSS file to be loaded properly on the page - that is I want CSS file to be loaded on the page as if it was loaded with <LINK rel="stylesheet" type="text/css" href="css/styles_one.css" />. The same applies to ANY and ALL other MIME content, including switching pages with ANY extension with it - like /Script.php?id=php_page_1 (if <a href="/Script.php?id=php_page_1">php page 1</a> is clicked).

Can you help?

CodePudding user response:

You can use the php function mime_content_type. Here is an example with some syntactic sugar.

<?php

$file_name = $_GET["id"];
switch($file_name)
{
    default: echo 'Bad ID!'; exit;
    case "style1": $file_name = "csss/style_one.css"; break;
    case "style2": $file_name = "csss/style_two.css"; break;
    case "image1": $file_name = "images/image_one.jpg"; break;
    case "image2": $file_name = "images/image_one.png"; break;
    case "image3": $file_name = "images/image_one.bmp"; break;
    case "image4": $file_name = "images/image_one.gif"; break;
    case "video1": $file_name = "videos/video_one.avi"; break;
    case "video2": $file_name = "videos/video_two.mp4"; break;
    case "video3": $file_name = "videos/video_three.mov"; break;
    case "php_page_1": $file_name = "php_pages/php_one.php"; break;
    case "html_page_1": $file_name = "html_pages/html_one.html"; break;
    case "icon_fav_1": $file_name = "icons/icon_one.ico"; break;
    case "font_1": $file_name = "fonts/font_one.otf"; break;
}

if(file_exists($file_name)) {

    header("last-modified: {gmdate('D, d M Y H:i:s', filemtime($file_name))} GMT");
    header("content-type: {mime_content_type($file_name)}");
    header("content-length: {filesize($file_name)}");
    readfile($file_name);

} else {

    echo 'Bad ID!';

}

exit;

If you are using php 8. You will have to write it like this.


<?php

$file_name = $_GET["id"];
switch($file_name)
{
    default: echo 'Bad ID!'; exit;
    case "style1": $file_name = "csss/style_one.css"; break;
    case "style2": $file_name = "csss/style_two.css"; break;
    case "image1": $file_name = "images/image_one.jpg"; break;
    case "image2": $file_name = "images/image_one.png"; break;
    case "image3": $file_name = "images/image_one.bmp"; break;
    case "image4": $file_name = "images/image_one.gif"; break;
    case "video1": $file_name = "videos/video_one.avi"; break;
    case "video2": $file_name = "videos/video_two.mp4"; break;
    case "video3": $file_name = "videos/video_three.mov"; break;
    case "php_page_1": $file_name = "php_pages/php_one.php"; break;
    case "html_page_1": $file_name = "html_pages/html_one.html"; break;
    case "icon_fav_1": $file_name = "icons/icon_one.ico"; break;
    case "font_1": $file_name = "fonts/font_one.otf"; break;
}

if(file_exists($file_name)) {

    $last_modified = gmdate('D, d M Y H:i:s', filemtime($file_name));
    header("last-modified: $last_modified GMT");

    $content_type = mime_content_type($file_name);
    header("content-type: $content_type");

    $content_length = filesize($file_name);
    header("content-length: $content_length");
    
    readfile($file_name);

} else {

    echo 'Bad ID!';

}

exit;

CodePudding user response:

getImageSize() is just for image files. The more general function for getting MIME types is mime_content_type().

if(file_exists($file_name))
{
    $type = mime_content_type("$file_name");
    $fP = fOpen("$file_name", "rb");
    if($type and $fP)
    {
    header('last-modified: '.gmdate('D, d M Y H:i:s', filemtime($file_name)).' GMT');
    header('content-type: '.$type.');
    header('content-length: '.filesize($file_name).);
    fPassThru($fP);
    }
    exit;
}
else
{
    echo 'Bad ID!';
}
  • Related