Home > OS >  Change document type with PHP using URL file extension
Change document type with PHP using URL file extension

Time:02-05

I am trying to change the document type using PHP by getting string from URL. This because i made a one page php website. All the contents will load up in one single index.php whether it is HTML file, CSS file, Images (png, jpg, jpeg, etc), XML, etc

I noticed my files are loading fine in PHP but document type showing HTML by default for all type of content. I noticed this in developer tool -> network tab

To achieve this, i am trying :

$paraurl = $_SERVER['REQUEST_URI'];
$file_extension = pathinfo(strtok($paraurl, '?'), PATHINFO_EXTENSION);

$exclude_url_extension = ["php", "html", "asp"];
$image_extensions = ["jpg", "jpeg", "png", "gif", "bmp", "tiff", "svg", "webp"];
$application_extensions = ["pdf", "json", "xml", "js"];
$text_extensions = ["html", "txt", "css"];

//if (!in_array($file_extension, $exclude_url_extension) && strrchr($paraurl, '.' . $file_extension) !== false) {
if (!in_array($file_extension, $exclude_url_extension) && strpos($paraurl, '.' . $file_extension) !== false) {
  header('Content-Disposition: inline');

  if (in_array($file_extension, $application_extensions)) {
    header('Content-type: application/' . $file_extension . '; charset=utf-8');
  } elseif (in_array($file_extension, $image_extensions)) {
    header('Content-type: image/' . $file_extension);
  } elseif (in_array($file_extension, $text_extensions)) {
    header('Content-type: text/' . $file_extension . '; charset=utf-8');
  }
}

Issues i am facing :

  1. several files like .png, .jpg, .xsl, etc getting download automatically when it access through direct url (https://example.com/example.png) but it working fine when it embedded <img src="https://example.com.png" />
  2. sitemap.xml is always getting broken but all rest is working fine. even i tried adding charset=utf-8 in header but still sitemap.xml load broken
  3. What should i use for XML (text/xml or application/xml) and javascript (text/javascript or application/javascript or application/x-javascript)
  4. What should i use in if statement? strrchr or strpos

CodePudding user response:

Document : https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types

$paraurl = $_SERVER['REQUEST_URI'];
$file_extension = pathinfo(strtok($paraurl, '?'), PATHINFO_EXTENSION);

$exclude_url_extension = ["php", "html", "htm", "asp", "aspx"];
$image_extensions = ["jpg", "jpeg", "png", "gif", "bmp", "tiff", "webp", "avif"];
$application_extensions = ["pdf", "json", "xml", "zip"]; 
$text_extensions = ["html", "txt", "css", "csv"];
$audio_extensions = ["mpeg", "wav", "m4a", "mp3", "aac", "m4b"];
$video_extensions = ["mp4", "ogg", "webm", "quicktime", "x-msvideo", "x-flv", "mpeg", "3gpp", "x-ms-wmv", "x-matroska"];


if (!in_array($file_extension, $exclude_url_extension) && strpos($paraurl, '.' . $file_extension) !== false) {
    header('Content-Disposition: inline');
    
    if (in_array($file_extension, $application_extensions)) {
        header('Content-Type: application/' . $file_extension . '; charset=utf-8');
    } else if (in_array($file_extension, $image_extensions)) {
        header('Content-Type: image/' . $file_extension);
    } else if (in_array($file_extension, $text_extensions)) {
        header('Content-Type: text/' . $file_extension . '; charset=utf-8');
    } else if ($file_extension === "js") {
        header('Content-Type: application/javascript; charset=utf-8');
    } else if (in_array($file_extension, $video_extensions)) {
        header('Content-Type: video/' . $file_extension);
    } else if (in_array($file_extension, $audio_extensions)) {
        header('Content-Type: audio/' . $file_extension);
    } else if ($file_extension === "weba") {
        header('Content-Type: audio/webm');
    } else if ($file_extension === "oga") {
        header('Content-Type: audio/ogg');
    } else if ($file_extension === "ogv") {
        header('Content-Type: video/ogg');
    } else if ($file_extension === "ogx") {
        header('Content-Type: application/ogg');
    } else if ($file_extension === "svg") {
        header('Content-Type: image/svg xml');
    } else if ($file_extension === "xhtml") {
        header('Content-Type: application/xhtml xml; charset=utf-8');
    } else if ($file_extension === "xls") {
        header('Content-Type: application/vnd.ms-excel');
    } else if ($file_extension === "xlsx") {
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    } else if ($file_extension === "jpg") {
        header('Content-Type: image/jpeg');
    } else if ($file_extension === "doc") {
        header('Content-Type: application/msword');
    } else if ($file_extension === "docx") {
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    } else if ($file_extension === "ppt") {
        header('Content-Type: application/vnd.ms-powerpoint');
    } else if ($file_extension === "pptx") {
        header('Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation');
    } else if ($file_extension === "vsd") {
        header('Content-Type: application/vnd.visio');
    } else if ($file_extension === "ico") {
        header('Content-Type: image/vnd.microsoft.icon');
    } else if ($file_extension === "ics") {
        header('Content-Type: text/calendar');
    } else if ($file_extension === "rar") {
        header('Content-Type: application/vnd.rar');
    } else if ($file_extension === "tar") {
        header('Content-Type: application/x-tar');
    } else if ($file_extension === "7z") {
        header('Content-Type: application/x-7z-compressed');
    } else if ($file_extension === "gz") {
        header('Content-Type: application/gzip');
    }

}
  • Related