我在做一个网页,用户上传文件。我想如果发言为创建美元的误差的变量,如果该文件的类型的任何其他jpg,gif,并pdf。

这是我的代号:

$file_type = $_FILES['foreign_character_upload']['type']; //returns the mimetype

if(/*$file_type is anything other than jpg, gif, or pdf*/) {
  $error_message = 'Only jpg, gif, and pdf files are allowed.';
  $error = 'yes';
}

我是有困难的结构如果发言。我怎么会这么说?

有帮助吗?

解决方案

把允许的类型在一个阵列和使用 in_array()

$file_type = $_FILES['foreign_character_upload']['type']; //returns the mimetype

$allowed = array("image/jpeg", "image/gif", "application/pdf");
if(!in_array($file_type, $allowed)) {
  $error_message = 'Only jpg, gif, and pdf files are allowed.';
  $error = 'yes';
}

其他提示

修改

我刚刚意识到要允许PDF的文件。在这种情况下,检查了 PHP的FileInfo类和功能的。但就安全性的话,你还是不应该依赖$_FILES[]['type']:)

我要离开这里休息的情况下,它可以帮助别人谁发现了这个问题。


为了检查图像的mime类型,$_FILES[]['type']可能是不安全的。该数据由浏览器发送,并可能很容易被欺骗。

您应该使用getimagesize()功能,如果你只是想使图像的上传(尽管它可能误导名称)。此功能将不只是给你的大小,但所有的数据你可能需要对图像。

我用下面的脚本中的图像处理类:

private function load_image_data($image_file) {

    // Firstly, to disambiguate a loading error with a nonexistant file error,
    // check to see if the file actually exists.
    if( ! file_exists($image_file) ) {
        throw new Nonexistent_Image_Exception("The file '{$image_file}' does not exist");
    }

    // We're going to check the return value of getimagesize, so we don't
    // need any pesky warnings or notices popping up, since we're going to
    // stop execution of this function if something goes wrong.
    $image_data = @getimagesize($image_file);

    if( $image_data === false ) {
        throw new Load_Image_Exception("Could not get image data from '{$image_file}'");
    }

    $this->size = new Dimensions($image_data[0], $image_data[1]);
    $this->mime = $image_data['mime'];

}

注意,getimagesize()返回包含一个“MIME”索引的关联数组。这里的数据是可靠的。

在另一个函数I检查图像的mime类型,并将其与适当的GD函数转换为PNG:

private function load_image($image_file) {

    // Suppress warning messages because we're going to throw an
    // exception if it didn't work instead.
    switch( $this->mime ) {
    case 'image/jpeg':
    case 'image/pjpeg':
        $this->image = @imagecreatefromjpeg($image_file);
        break;
    case 'image/gif':
        $this->image = @imagecreatefromgif($image_file);
        break;
    case 'image/png':
        $this->image = @imagecreatefrompng($image_file);
        break;
    default:
        throw new Invalid_Image_Exception("The image was of an invalid type");
    }

    if( $this->image === false ) {
        throw new Load_Image_Exception("Loading of image '{$image_file}' failed");
    }

}

您可能不会需要做的这一切,但你可以看到类型出现什么MIME为您所指定的文件类型。请注意,一个JPEG可以有两个不同的MIME类型。

希望这有助于。

也参看 Zend框架's Zend_File_Transfer_Adapter_HttpZend_Form_Element_File.你可以加入多个不同的验证程序等最低的分辨率的图像,MIME类型,最低的文件的尺寸,允许文件的扩展,等等。

使用这个简单的代码...

<?
$path = $_FILES['file']['name']; // file means your input type file name
$ext = pathinfo($path, PATHINFO_EXTENSION);

if ($ext=="jpg" OR $ext=="jpeg" OR $ext=="gif" OR $ext=="png") {
    // your code here like...
    echo "Upload successful";
}else{
    // your invalid code here like...
    echo "Invalid image format. Only upload JPG or JPEG or GIF or PNG";
}
?>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top