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 :
- 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" />
sitemap.xml
is always getting broken but all rest is working fine. even i tried addingcharset=utf-8
in header but stillsitemap.xml
load broken- What should i use for XML (
text/xml
orapplication/xml
) and javascript (text/javascript
orapplication/javascript
orapplication/x-javascript
) - What should i use in
if
statement?strrchr
orstrpos
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');
}
}