正在上传Image PHP文件类型?

时间:2015-07-07 03:05:43

标签: php image

我正在尝试上传图片,但整个代码似乎搞砸了,我发送了一个.jpg,它说的是“invalide图像类型”,当我发送.JAR成功发送它为什么? 这是我的代码我试图上传3MB图像,只有.jpg和.png图像文件。

<?php 
            if(isset($_POST["changepic"])) {
                    $new_file_name = "profileN.jpg";

                    //if no errors...
                    if(!$_FILES["photo"]["error"]) {

                      if((!$_FILES["photo"]["type"] == "image/jpeg") || (!$_FILES["photo"]["type"] == "image/png" )) {
                        echo $alert->danger("The image file is invalide.");

                        //Max size 3MB
                      } elseif($_FILES["photo"]["size"] > (3000000)) { 
                            echo $alert->danger("The image size is to large, 3MB maximum.");
                      } else {
                            //Move it to user's Folder.
                            move_uploaded_file($_FILES["photo"]["tmp_name"], "../assets/images/u/{$user->username}/{$new_file_name}");
                            $file->compressImg("../assets/images/u/{$user->username}/profileN.jpg", "../assets/images/u/{$user->username}/profile.jpg", 75);
                            echo $alert->success("Your profile picture has been changed !");
                        }
                } else {
                    echo $alert->danger("There was an error uploading the image, please try again.");
                }
        }
        ?>

2 个答案:

答案 0 :(得分:0)

你应该使用&&运算符,因为你只需要jpg和png文件类型。

if((!$_FILES["photo"]["type"] == "image/jpeg") && (!$_FILES["photo"]["type"] == "image/png" )) {

答案 1 :(得分:0)

试试这个方法

$valid_formats = array("jpeg","png");
$name = $_FILES['EXCEL_FILE']['name'];
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
    // Code if format is right
}
else
{
    echo 'Invalid format';
}