简体   繁体   English

PHP:从以下文件检测文件扩展名:filename.jpg

[英]PHP: Detecting file extension from: filename.jpg

Yes as the title says: 是的,正如标题所述:

How can i detect an file extension? 如何检测文件扩展名? $_POST['fname']; $ _POST ['fname']; is where i have the filename stored eg asddsa.jpg 是我存储文件名的地方,例如asddsa.jpg

So how can i check if .jpg then ... if .png then ... ? 那么我怎样才能检查.jpg然后...如果.png然后...呢?

    $src = "images/status/photo/".$_POST['fname'];
    $parts=pathinfo($src);
if($parts['extension'] == "jpg"){
    $img_r = imagecreatefromjpeg($src);
}elseif($parts['extension'] == "png"){
    $img_r = imagecreatefrompng($src);
}elseif($parts['extension'] == "gif"){
    $img_r = imagecreatefromgif($src);
}

tried this too without any success: 也尝试了这个但没有成功:

    $ext= pathinfo($src, PATHINFO_EXTENSION);
if($ext == "jpg"){
    $img_r = imagecreatefromjpeg($src);
}elseif($ext == "png"){
    $img_r = imagecreatefrompng($src);
}elseif($ext == "gif"){
    $img_r = imagecreatefromgif($src);
}

The proper way to do this would be to use pathinfo on the file. 正确的方法是在文件上使用pathinfo For example... 例如...

$parts=pathinfo('somefile.jpg');
echo $parts['extension']; //Returns "jpg"

Now, you're simply looking at a string in a variable, so the way to handle this would be something like this ... 现在,您只是在查看变量中的字符串,因此处理此问题的方式将类似于以下内容 ...

$parts=explode(".", $_POST['fname']);
echo $parts[count($parts)-1];

Finally, you should not be detecting file type by extension. 最后,您不应按扩展名检测文件类型。 This is bad. 这不好。 Instead, you should be using the content-type (commonly referred to as MIME type). 相反,您应该使用内容类型(通常称为MIME类型)。 You can find this in $_FILES['userfile']['type'] for uploaded files. 您可以在$_FILES['userfile']['type']找到上传的文件。 ( documentation ) 文件

Once you have your type, you can do a simple switch statement . 输入类型后,即可执行简单的switch语句

Also, beware of folks uploading content that isn't what it says it is. 另外,要提防人们上传的内容不是它说的那样。 Someone can send files to you with a specific content type, but it may be something such as executable code instead! 有人可以使用特定的内容类型向您发送文件,但是可能是诸如可执行代码之类的东西! The only way to be sure is to check the files with your own code server-side. 确保的唯一方法是在您自己的代码服务器端检查文件。 Never trust user input. 永远不要相信用户输入。

You can do something like this: 您可以执行以下操作:

$aPieces = explode( '.', $_POST[ 'fname' ] ); // A file name can have more than 1 .something

switch( strtolower( $aPieces[ count( $aPieces) - 1 ] ) ) // We take the LAST piece in our array
{
   case 'jpg':

   break;
   case 'png':

   break;
}

The solution really depends on what you're trying to do, however the simple approach would be something like: 该解决方案实际上取决于您要尝试执行的操作,但是简单的方法如下所示:

if ( strtolower(substr($_POST['fname'],-3)) == 'jpg' )

You can also switch() on the string instead of using if/else depending on how many comparisons you will be making. 您也可以在字符串上切换()而不是使用if / else,这取决于要进行的比较次数。

Edit: if you are going to be specifically dealing with images, you're best off using getimagesize 编辑:如果要专门处理图像,最好使用getimagesize

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

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