简体   繁体   中英

form post-data isn't passed

I was getting an error of undifined index 'shop' when i submit the form bellow, and when i gave submit button a name and tested if it exists (isset) I had a negative response weridly, here's my code:

<?php  
session_start();
$data['titre'] = 'Ajouter des médias';
$this->load->view('templates/header',$data);
$this->load->view('templates/navbar');
if(isset($_SESSION['username'])){
    global $con;
    $userid = getUserId($_SESSION['username']);
    if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['form_posted'])){
        if(isset($_POST['shopid'])) $shop = $_POST['shopid'];
        if($_POST['shopid'] == 0){
            echo '<div class="alert alert-danger"> Aucune boutique n\'a été choisie!</div>';
        }
        else
        {
            if($_FILES['video']['name']!= NULL || $_FILES['images']['name'][0] != NULL){
                if(isset($_FILES['images']) && $_FILES['images']['name'][0] != NULL){

                                $avatarAllowedExtensions = array("jpeg", "jpg", "png", "gif");
                                $name_array = $_FILES['images']['name'];
                                $tmp_name_array = $_FILES['images']['tmp_name'];
                                $type_array = $_FILES['images']['type'];
                                $size_array = $_FILES['images']['size'];
                                $error_array = $_FILES['images']['error'];

                                for($i = 0; $i < count($tmp_name_array) ; $i++){
                                        $exploded_img = explode('.', $name_array[$i]);

                                        $img_Extension = strtolower(end($exploded_img));
                                    if(in_array($img_Extension, $avatarAllowedExtensions)){
                                        $img_name = rand(1000000,10000000). '.' . $img_Extension;
                                        move_uploaded_file($tmp_name_array[$i], "uploads/shops/" . $img_name);
                                        $stmt = $con->prepare("INSERT INTO shop.shop_images (ID_boutique, pic) VALUES (?,?)");
                                        $stmt->execute(array($shop, $img_name));
                                        $insertid = $con->lastInsertId();

                                        $stmt = $con->prepare("INSERT INTO publications ( type, tableid, ID_boutique) VALUES (?,?,?)");
                                        $stmt->execute(array(2, $insertid, $shop));
                                        if($_FILES['video']['name']== NULL){
                                            echo '<div class="alert alert-success">Chargement réussi.</div>';
                                            header('refresh:1.5;url='.base_url("boutiques/store/".$shop."#media"));
                                        }
                                    }
                                }

                            }
                    if(isset($_FILES['video']) && $_FILES['video']['name']!= NULL){
                           $maxsize = 5242880*3; 

                           $name = $_FILES['video']['name'];
                           $target_dir = "uploads/videos/";
                           $videoFileType = strtolower(pathinfo($name,PATHINFO_EXTENSION));
                           $videoName = rand(1000000,10000000). '.' . $videoFileType;
                           $target_file = $target_dir . $videoName;

                           // Select file type


                           // Valid file extensions
                           $extensions_arr = array("mp4","avi","3gp","mov","mpeg");

                           // Check extension
                           if( in_array($videoFileType,$extensions_arr) ){

                              // Check file size
                              if(($_FILES['video']['size'] >= $maxsize) || ($_FILES["video"]["size"] == 0)) {
                                echo '<div class="alert alert-danger">Vidéo très large, 15MB maximum!</div>';
                              }else{
                                if(move_uploaded_file($_FILES['video']['tmp_name'],$target_file)){
                                  // Insert record
                                    global $con;    
                                  $stmt = $con->prepare("INSERT INTO shop_videos(ID_boutique,video) VALUES('".$shop."','".$videoName."')");
                                        $stmt->execute();
                                        $insertid = $con->lastInsertId();

                                        $stmt = $con->prepare("INSERT INTO publications ( type, tableid, ID_boutique) VALUES (?,?,?)");
                                        $stmt->execute(array(4, $insertid, $shop));
                                  echo '<div class="alert alert-success">Chargement réussi.</div>';
                                  header('refresh:1.5;url='.base_url("boutiques/store/".$shop."#media"));
                                }
                              }

                           }else{
                              echo '<div class="alert alert-danger">Extension invalide!</div>';
                           }

                         }
            } 
            else{
                echo '<div class="alert alert-danger">Aucun fichier selectionné!</div>';
            }
        }
    }
    else echo 'POST VARIABLE HASNT PASSED!';
?> 
    <div class="offset-md-2 col-md-8">
    <div class="container block">
        <div class="card bg-light mb-3">
            <div class="panel-header">Nouvelles photos</div>
            <div class="card-body">
                <form enctype="multipart/form-data" action="<?php echo current_url(); ?>" method="POST" class="form-horizontal">
                                <div class="form-group row">
                                    <label class="col-sm-3 control-label" for="file">Ajouter des photos:</label>
                                <input name="images[]" id="shop_pics" type="file" multiple>
                                </div>

                                <div class="gallery"></div>
                                <div class="form-group row">
                                    <label class="col-sm-3 control-label" for="file">Ajouter une vidéo:</label>
                                    <input name="video" id="video-upload" type="file">

                                </div>
                                <p class="offset-md-2" style="font-size: 12px; font-style: italic;">NB: La vidéo ne doit pas dépasser 15mb de volume.</p>
                                <div class="video-preview"></div>


                            <div class="form-group row">
                                <label class="col-sm-2 control-label">Assigner à une boutique:</label>
                                <div class="col-sm-8 col-md-6">
                                    <?php
                                    $stmt = $con->prepare("SELECT * from shop.boutiques where userID = ?");
                                    $stmt->execute(array($userid));
                                    $boutiques = $stmt->fetchALL();
                                    if(!empty($boutiques)){
                                        echo '<select name="shopid" class="shopselect" required="required">';   
                                        echo '<option>Choisir une boutique</option>';                   
                                        foreach (myShops() as $boutique) {
                                        echo '
                                        <option value='.$boutique['ID_boutique'].'>'.$boutique['nom'].'</option>';

                            }           echo '</select>';
                                    }


                                ?> 
                                </div>
                            </div>


                            <div class="form-group">
                                <div class="text-center">
                                    <input type="submit" name="form_posted" value="Ajouter" class="btn btn-primary">
                                    <input type="reset" value="Annuler" class="btn btn-danger">
                                </div>
                            </div>

                        </form> 
                    </div>
                </div>
            </div>
            </div>
<?php
}
else{
    echo '<p class="alert alert-primary"><a href="login.php">Connectez-vous ou créez un compte</a> rapidement pour pouvoir déposer des articles</p>';
}
$this->load->view('templates/footer');

?>

PS: it happens only when I load a video, works well when I upload photos, any help would be welcome

About halfway down your code, you have:

// Valid file extensions
$extensions_arr = array("mp4","avi","3gp","mov","mpeg");

// Check extension
if (in_array($videoFileType, $extensions_arr)) {
    if (($_FILES['video']['size'] >= $maxsize) || ($_FILES["video"]["size"] == 0)) {
        // do stuff

So your code is checking if the file you've uploaded has any of the extensions in $extensions_arr (.mp4, .avi, .3gp, .mov, .mpeg).

Once you've uploaded the file, it then checks the size and makes sure it's not either 0 or greater than the max file size. As an aside, to make this a little bit more readable, why not just use

if (in_array($videoFileType, $extensions_arr)) {
    if (($_FILES['video']['size'] <= $maxsize) || ($_FILES["video"]["size"] > 0)) {
        // do stuff

and check that the file size is less than max size but greater than 0?

The problem here is that you haven't declared $shop anywhere in the code, maybe you meant $target_file ?

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