PHP上传文件,扩展错误

时间:2014-04-08 14:16:45

标签: 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('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');
            }

        }
    }
}

?>

它说我尝试使用&#39; .gif&#39;上传图片时不允许使用该文件。 &安培; &#39; .JPG&#39;我的数组中存在的扩展名。

HTML表单代码(Bootstrap CSS&amp; 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>

谢谢你们!

2 个答案:

答案 0 :(得分:2)

表单标记需要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.');
}

这应该有用(至少它对我有用)

答案 1 :(得分:0)

问题解决了,我所做的是我删除了包含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代码是一样的,希望我帮助这里的人好运