推特引导程序 - PHP 上传文件,扩展错误


twitter bootstrap - PHP Upload File, Extension Error

我已经搜索并尝试了所有内容,我的脚本运行良好并且我正确上传了文件,但现在我不知道为什么它仍然抛出文件扩展名的错误确实存在于我的数组中。

这是我的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 表单代码 (Bootstrap CSS & Js):

<form role="form" class="form-horizontal" enctype="multipart/form-data">
<div class="form-group has-feedback">
              <label class="control-label col-sm-3" for="fileselect">Images :</label>
              <div class="col-sm-9 upload">
                <!-- Change the wording using a title tag -->
                <input type="file" title="Parcourir..." name="file" multiple="multiple" id="file"/>
                <span class="messages" id="file-name"></span>
              </div>
            </div>
            <div class="form-group pull-right">
              <div class="col-sm-9">
                  <button type="submit" class="btn btn-success" id="submit" name="submit" value="addPro">Ajouter ce bien</button>
              </div>
</div>
</form>

谢谢你们!

表单标签需要上传文件所需的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代码是一样的,希望我在这里帮助了某人,祝你好运