简体   繁体   English

查找特定文件名的扩展名

[英]Find extension of a specific filename

I am creating a image hosting service and I want to shorten the name so that it has no extension and the files are in the 'root' directory of my server. 我正在创建图像托管服务,我想缩短名称,使其不具有扩展名,并且文件位于服务器的“ root”目录中。 I've gotten to the point where I can load any PNG file and it will show the image, but say, the file is swf or jpg, it will still try to open the PNG. 我已经到了可以加载任何PNG文件的地步,它将显示图像,但是说,该文件是swf或jpg,它仍然会尝试打开PNG。 Now, here's what I've got currently. 现在,这是我目前所拥有的。 If there is an extension on the filename, and the filename ends in gif, jpg, png or swf, it will load the file from the /img/ directory. 如果文件名带有扩展名,并且文件名以gif,jpg,png或swf结尾,则它将从/ img /目录加载文件。 If not, it will open media.php and parse the file form there .htaccess 如果没有,它将打开media.php并在那里解析文件形式。

RewriteEngine on
    RewriteCond %{REQUEST_URI} !/img/.*
    RewriteRule ^(.*\.(gif|jpg|png|swf))$ img/$1 [QSA]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ media.php?i=$1 [NC,L]

media.php media.php

<?php
//Media.PHP
if(!isset($_REQUEST['i'])){
die("No media selected  ");
}
$filename = $_REQUEST['i'];
if(file_exists('img/'.$filename .'.png')){
$header = "image/png";
$ext = ".png";
}
elseif(file_exists('img/'.$filename .'.jpg')){
$header = "image/jpg";
$ext = ".jpg";
}
elseif(file_exists('img/'.$filename.'.jpeg')){
$header = "image/jpg";
$ext = ".jpeg";
}
elseif(file_exists('img/'.$filename .'.gif')){
$header == "image/gif";
$ext = ".gif";
}
elseif(file_exists('img/'.$filename .'.swf')){
$header == "application/x-shockwave-flash";
$ext = ".swf";
}
else die("404 File not found");
header("Content-Type: $header");
@readfile('img/'.$filename."$ext");

Now, this works great with images(png,gif,jpg,jpeg) but I cannot get it to work correctly with .swf files. 现在,这可以很好地与images(png,gif,jpg,jpeg)一起使用,但是我无法使其与.swf文件一起正常使用。 It just downloads them. 它只是下载它们。 So, in this case, I can open the file from mydomain.com/MYSWF.swf but not mydomain.com/MYSWF, because it downloads it with the shorter link. 因此,在这种情况下,我可以从mydomain.com/MYSWF.swf中打开文件,但不能从mydomain.com/MYSWF中打开文件,因为它会使用较短的链接下载文件。

在设置$header ?的情况下,如果有条件,则gif和swf似乎有双等号( == )。

Try like this header when it is a swf file: 当它是swf文件时,请尝试使用以下标头:

$fileName="filename.swf";
header("Content-Type: application/x-shockwave-flash",true);
header("Accept-Ranges: bytes",true);
header("Connection: keep-alive",true);
header("Content-Disposition: inline; filename=$fileName");
readfile($fileName); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM