简体   繁体   中英

PHP Upload File, Extension Error

I have searched around and tried everything, my script was working good and i was uploading files correctly, but now i dunno why it still throw the error of file extension that does exist in my array.

Here is my PhpUpload script :

<?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');
            }

        }
    }
}

?>

It says that the file is not allowed while i have tried to upload an image with '.gif' & '.jpg' extension that exists in my array.

HTML form code (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>

Thank you guys !

The form tag needs enctype="multipart/form-data" thats is needed to upload files

Edit Why not change the allowed file type array to: array('image/jpg', 'image/jpeg', 'image/png', 'image/gif');

and say:

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

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

this should work (atleast it does for me)

Issue solved, what i did is that i removed the condition where i include my bootstrap.php and then my script worked fine with POST method.

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');
            }

        }
    }

my html code is the same, hope i helped someone here goodluck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM