PHP上传文件,扩展名错误

PHP上传文件,扩展名错误,php,twitter-bootstrap,file-upload,Php,Twitter Bootstrap,File Upload,我到处搜索并尝试了一切,我的脚本运行良好,我正确上传了文件,但现在我不知道为什么它仍然抛出我的数组中确实存在的文件扩展名错误 以下是我的PhpUpload脚本: <?php # code... if(!@include("bootstrap.php")) throw new Exception("Failed to include 'bootstrap'"); else{ $sqldb=new SqlDB; $mysqli=$sqldb->DBconnect('lo

我到处搜索并尝试了一切,我的脚本运行良好,我正确上传了文件,但现在我不知道为什么它仍然抛出我的数组中确实存在的文件扩展名错误

以下是我的PhpUpload脚本:

<?php
# code...
if(!@include("bootstrap.php")) throw new Exception("Failed to include 'bootstrap'");
else{ 
    $sqldb=new SqlDB;
    $mysqli=$sqldb->DBconnect('localhost','root','root','dbex');
    if (isset($_GET['ref']) && isset($_GET['rooms']) && isset($_GET['showers']) && isset($_GET['parkings']) && isset($_GET['infos']) && isset($_GET['city']) && isset($_GET['surface']) && isset($_GET['price']) && isset($_GET['sup']) && isset($_GET['desc'])) {
        # code...
        if (!empty($_GET['ref']) && !empty($_GET['rooms']) && !empty($_GET['showers']) && !empty($_GET['parkings']) && !empty($_GET['infos']) && !empty($_GET['city']) && !empty($_GET['surface']) && !empty($_GET['price']) && !empty($_GET['sup']) && !empty($_GET['desc'])) {
            # code...
            $ref = $_GET['ref'];
            $rooms = $_GET['rooms'];
            $showers = $_GET['showers'];
            $parkings = $_GET['parkings'];
            $infos = $_GET['infos'];
            $city = $_GET['city'];
            $surface = $_GET['surface'];
            $price = $_GET['price'];
            $sup = $_GET['sup'];
            $desc = $_GET['desc'];

            //File size, path and extensions allowed
            $allowed_filetypes = array('.jpg','.jpeg','.png','.gif');
            $max_filesize = 10485760;
            $path="css/";

            //File name 
            $i=10;

            //File settings
            $filename = $_FILES['file']['name'];
            $uploadfile=$path.basename($filename);
            $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

            //Conditions
            if(!in_array($ext,$allowed_filetypes))
              die('The file you attempted to upload is not allowed.');
            if(filesize($_FILES['fileselect']['tmp_name']) > $max_filesize)
              die('The file you attempted to upload is too large.');
            if (file_exists($upload_path))
                die('File already exist.');
            if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
                # code...
                die('Line 41');
            }
            else{
                die('error uploading the file');
            }

        }
    }
}

?>

它说,当我尝试上载数组中存在的扩展名为“.gif”和“.jpg”的图像时,不允许使用该文件

HTML表单代码(引导CSS和Js):


图像:
阿杰斯宾

谢谢你们

表单标签需要上传文件所需的
enctype=“multipart/form data”

编辑 为什么不将允许的文件类型数组更改为:
array('image/jpg','image/jpeg','image/png','image/gif')

然后说:

$file_type = $_FILES['file']['type'];

if(!in_array($file_type, $allowed_filetypes)) {
 die('The file you attempted to upload is not allowed.');
}

这应该行得通(至少对我是这样)

问题解决了,我所做的是删除了包含我的
bootstrap.php的条件
然后我的脚本在
POST
方法下运行良好

if (isset($_POST['ref']) && isset($_POST['rooms']) && isset($_POST['showers']) && isset($_POST['parkings']) && isset($_POST['location']) && isset($_POST['typepr']) && isset($_POST['tran']) && isset($_POST['priceper']) && isset($_POST['city']) && isset($_POST['surface']) && isset($_POST['price']) && isset($_POST['sup']) && isset($_POST['desc'])) {
        # code...
        if (!empty($_POST['ref']) && !empty($_POST['rooms']) && !empty($_POST['showers']) && !empty($_POST['parkings']) && !empty($_POST['location']) && !empty($_POST['typepr']) && !empty($_POST['tran']) && !empty($_POST['priceper']) && !empty($_POST['city']) && !empty($_POST['surface']) && !empty($_POST['price']) && !empty($_POST['sup']) && !empty($_POST['desc'])) {
            # code...
            $ref = $_POST['ref'];
            $rooms = $_POST['rooms'];
            $showers = $_POST['showers'];
            $parkings = $_POST['parkings'];
            $location = $_POST['location'];
            $typepr = $_POST['typepr'];
            $tran = $_POST['tran'];
            $priceper = $_POST['priceper'];
            $city = $_POST['city'];
            $surface = $_POST['surface'];
            $price = $_POST['price'];
            $sup = $_POST['sup'];
            $desc = $_POST['desc'];

            //File size, path and extensions allowed
            $allowed_filetypes = array('.jpg', '.jpeg', '.png', '.gif');
            $file_type = $_FILES['fileselect']['type'];
            $max_filesize = 10485760;
            $path="css/";

            //File name 
            $i=10;

            //File settings
            $filename = $_FILES['fileselect']['name'];
            $uploadfile=$path.basename($filename);
            $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

            //Conditions
            if(!in_array($ext,$allowed_filetypes))
              die('The file you attempted to upload is not allowed.');
            if(filesize($_FILES['fileselect']['tmp_name']) > $max_filesize)
              die('The file you attempted to upload is too large.');
            if (file_exists($uploadfile))
                die('File already exist.');
            if (move_uploaded_file($_FILES['fileselect']['tmp_name'], $uploadfile)) {
                # code...
                die('Line 41');
            }
            else{
                die('error uploading the file');
            }

        }
    }

我的html代码是一样的,希望我在这里帮助了一些人goodluck

尝试将
enctype=“multipart/form data”
添加到您的
中。我已经尝试过它不起作用。这就是为什么我将它作为评论发布。当涉及到这些问题时,我知道它不是100%确定它会起作用,而且可能是其他因素导致了这个问题。我想我知道为什么。您可能有命名约定冲突。您的意思是我的html输入文件标记中的类型、名称和id之间存在冲突?我已经尝试了此方法,谢谢兄弟,但仍然无效:)
if (isset($_POST['ref']) && isset($_POST['rooms']) && isset($_POST['showers']) && isset($_POST['parkings']) && isset($_POST['location']) && isset($_POST['typepr']) && isset($_POST['tran']) && isset($_POST['priceper']) && isset($_POST['city']) && isset($_POST['surface']) && isset($_POST['price']) && isset($_POST['sup']) && isset($_POST['desc'])) {
        # code...
        if (!empty($_POST['ref']) && !empty($_POST['rooms']) && !empty($_POST['showers']) && !empty($_POST['parkings']) && !empty($_POST['location']) && !empty($_POST['typepr']) && !empty($_POST['tran']) && !empty($_POST['priceper']) && !empty($_POST['city']) && !empty($_POST['surface']) && !empty($_POST['price']) && !empty($_POST['sup']) && !empty($_POST['desc'])) {
            # code...
            $ref = $_POST['ref'];
            $rooms = $_POST['rooms'];
            $showers = $_POST['showers'];
            $parkings = $_POST['parkings'];
            $location = $_POST['location'];
            $typepr = $_POST['typepr'];
            $tran = $_POST['tran'];
            $priceper = $_POST['priceper'];
            $city = $_POST['city'];
            $surface = $_POST['surface'];
            $price = $_POST['price'];
            $sup = $_POST['sup'];
            $desc = $_POST['desc'];

            //File size, path and extensions allowed
            $allowed_filetypes = array('.jpg', '.jpeg', '.png', '.gif');
            $file_type = $_FILES['fileselect']['type'];
            $max_filesize = 10485760;
            $path="css/";

            //File name 
            $i=10;

            //File settings
            $filename = $_FILES['fileselect']['name'];
            $uploadfile=$path.basename($filename);
            $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

            //Conditions
            if(!in_array($ext,$allowed_filetypes))
              die('The file you attempted to upload is not allowed.');
            if(filesize($_FILES['fileselect']['tmp_name']) > $max_filesize)
              die('The file you attempted to upload is too large.');
            if (file_exists($uploadfile))
                die('File already exist.');
            if (move_uploaded_file($_FILES['fileselect']['tmp_name'], $uploadfile)) {
                # code...
                die('Line 41');
            }
            else{
                die('error uploading the file');
            }

        }
    }