-3

現時点では、.jpg ファイルのアップロードを許可しています。

jpeg、gif、png を追加するにはどうすればよいですか?

私の現在のコードは次のとおりです...

$filename = basename($_FILES['photo']['name']);
            $ext = substr($filename, strrpos($filename, '.') + 1);

            //Check if the file is JPEG image and it's size is less than 5Mb
$allowedExts = array('jpg', 'jpeg', 'gif', 'png');
            if ( (in_array($ext, $allowedExts)) && ($_FILES["photo"]["type"] == "image/jpeg") && ($_FILES["photo"]["size"] <= 5242880) ){
                //Determine the path to which we want to save this file
                $newname = str_replace( ' ', '_', trim( strip_tags( $_POST['name'] ) ) ) . _ . $formKey->generateKey() . '_' . time() . '.jpg';
4

2 に答える 2

1

許可された拡張子の配列を作成し、それに対してチェックします。http://php.net/manual/en/function.in-array.phpをご覧ください。

あなたは次のようなもので終わります:

$allowedExts = array('jpg', 'jpeg', 'gif'); // Add more here
if (in_array($ext, $allowedExts))
{
    // Do something here
}

また、ファイルの拡張子を確認するには、pathinfo を使用することをお勧めします: http://php.net/manual/en/function.pathinfo.php .jpg で終わるファイルをアップロードして騙す人もいますが、 .php ファイルです。

[編集]ミニコメントが嫌いなので更新:

   $allowedExtensions = array("image/jpeg", "image/png", "image/gif", "image/pjpeg", "image/jpg");
   $pathInfo = pathinfo($_FILES["photo"]["type"]);
   if (in_array($pathInfo['extension'], $allowedExtensions))
   {
       // Do stuff
   }

このように簡単にできます。これは、pathinfo の 2 番目のパラメーターを使用して単純化することもできますが、後で配列の他の要素が必要になると想定しています。

于 2012-10-28T11:44:37.457 に答える
0

以下を使用できます。

if(file_exists($path_image) && in_array(exif_imagetype($path_image),array(1, 2, 3, 6))){ ... } 

http://php.net/manual/en/function.exif-imagetype.php

于 2014-12-01T13:34:10.770 に答える