我检查上传的文件是否具有文件扩展名。我的示例方法有效,但现在我需要知道使用 pathinfo
方法是否正确。是否还有其他更好且更快的方法?
$filename = $_FILES['video_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if ($ext !== 'gif' || $ext !== 'png' || $ext !== 'jpg') {
echo 'error';
}
我检查上传的文件是否具有文件扩展名。我的示例方法有效,但现在我需要知道使用 pathinfo
方法是否正确。是否还有其他更好且更快的方法?
$filename = $_FILES['video_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if ($ext !== 'gif' || $ext !== 'png' || $ext !== 'jpg') {
echo 'error';
}
使用if( $ext !== 'gif'
)可能不够高效。如果你允许20种不同的扩展名呢?
尝试:
$allowed = array('gif', 'png', 'jpg');
$filename = $_FILES['video_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (!in_array($ext, $allowed)) {
echo 'error';
}
检查文件扩展名并不被视为最佳实践。完成此任务的首选方法是检查文件的MIME类型。
来自PHP:
<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE); // Return MIME type
foreach (glob("*") as $filename) {
echo finfo_file($finfo, $filename) . "\n";
}
finfo_close($finfo);
?>
text/html
image/gif
application/vnd.ms-excel
虽然MIME类型也可以被欺骗(编辑文件的前几个字节并修改魔数),但这比编辑文件名要困难。因此,您永远无法百分之百确定该文件类型实际上是什么,处理用户上传/发送的文件时应格外小心。
就我个人而言,我更喜欢使用preg_match()函数:
if(preg_match("/\.(gif|png|jpg)$/", $filename))
$exts = array('gif', 'png', 'jpg');
if(in_array(end(explode('.', $filename)), $exts)
in_array()
函数可以很方便地验证许多扩展并解决性能问题。另一种验证图像文件的方法是使用@imagecreatefrom*()
函数,如果该函数失败,则表示该图像无效。function testimage($path)
{
if(!preg_match("/\.(png|jpg|gif)$/",$path,$ext)) return 0;
$ret = null;
switch($ext)
{
case 'png': $ret = @imagecreatefrompng($path); break;
case 'jpeg': $ret = @imagecreatefromjpeg($path); break;
// ...
default: $ret = 0;
}
return $ret;
}
$valid = testimage('foo.png');
foo.png
是一个带有.png
扩展名的PHP脚本文件,上述函数会失败。它可以避免像shell更新和LFI这样的攻击。pathinfo很酷,但您的代码可以改进:
$filename = $_FILES['video_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$allowed = array('jpg','png','gif');
if( ! in_array( $ext, $allowed ) ) {echo 'error';}
当然,仅检查文件名的扩展名不能保证文件类型是有效的图像。您可以考虑使用getimagesize
函数来验证上传的图像文件。
$img_up = $ _FILES ['video_file'] ['type']; $img_up_type = explode("/", $img_up); $img_up_type_firstpart = $img_up_type [0];如果($img_up_type_firstpart == "image") { // image是图像文件类型,如果需要检查视频文件类型,则可以处理视频。
/* 执行您的逻辑代码 */
}function get_mime($file) {
if (function_exists("finfo_file")) {
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
$mime = finfo_file($finfo, $file);
finfo_close($finfo);
return $mime;
} else if (function_exists("mime_content_type")) {
return mime_content_type($file);
} else if (!stristr(ini_get("disable_functions"), "shell_exec")) {
// https://dev59.com/ZHVC5IYBdhLWcg3w9GLM#134930
$file = escapeshellarg($file);
$mime = shell_exec("file -bi " . $file);
return $mime;
} else {
return false;
}
}
//pass the file name as
echo(get_mime($_FILES['file_name']['tmp_name']));
$acceptedFormats = array('gif', 'png', 'jpg');
if(!in_array(pathinfo($filename, PATHINFO_EXTENSION), $acceptedFormats))) {
echo 'error';
}
//<?php
//checks file extension for images only
$allowed = array('gif','png' ,'jpg');
$file = $_FILES['file']['name'];
$ext = pathinfo($file, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) )
{
//?>
<script>
alert('file extension not allowed');
window.location.href='some_link.php?file_type_not_allowed_error';
</script>
//<?php
exit(0);
}
//?>
<?php
if(isset($_POST['save'])) {
//Validate image
if (empty($_POST["image"])) {
$imageError = "";
} else {
$image = test_input($_POST["image"]);
$allowed = array('jpeg','jpg');
$ext = pathinfo($image, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
$imageError = "jpeg only";
}
}
}
// Validate data
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
<?
你的 HTML 代码将会长成这个样子:
<html>
<head>
</head>
<body>
<!-- Validate -->
<?php include_once ('validate.php'); ?>
<!-- Form -->
<form method="post" action="">
<!-- Image -->
<p>Image <?php echo $imageError; ?></p>
<input type="file" name="image" value="<?php echo $image; ?>" />
<p><input type="submit" name="save" value="SAVE" /></p>
</form>
</body>
</html>
function check_image_extension($image){
$images_extentions = array("jpg","JPG","jpeg","JPEG","png","PNG");
$image_parts = explode(".",$image);
$image_end_part = end($image_parts);
if(in_array($image_end_part,$images_extentions ) == true){
return time() . "." . $image_end_part;
}else{
return false;
}
}
&&
而不是||
... 只是想分享我的观点。 - itsolsgetimagesize
PHP 函数来检测文件是否真的是一张图片。如果未检测到图片,则返回 false。 - Hike Nalbandyan