0

I'm trying to upload multiple files and store them into a directory for a specific hotel room.

PHP will check if there is already a directory and if not it will make it. However the part of moving the files to that directory does not work.

My create a room function:

public function create($fields = array(), $file) {

    if(!$this->_db->insert('rooms', $fields)) {
        throw new Exception('There was a small problem creating the room');
    }

    else{
        $roomNR = $fields['roomnr'];
        $dir = $_SERVER['DOCUMENT_ROOT'] . "/images/rooms/$roomNR";

        if ( !file_exists($dir) ) {
            mkdir ($dir, 0744);
        }
        move_uploaded_file($file["file"]["tmp_name"], "$dir/" . $file["file"]["name"]);
    }
}

This is how I pass everything to my function:

try{
            $newRoom->create(array(
                'roomnr' => Input::get('roomnr'),
                'catagory' => Input::get('Catagory'),
                ),$_FILES);

            Session::flash('success', 'The room has been added');
            // Redirect::to('admin-rooms.php');

        } catch(Exception $e){
            die($e->getMessage());
        }

and here is my form that I use:

<form role="form" action="" method="post">
<div class="form-group">
    <label for="roomnumber">Room Number:</label>
    <input type="text" name="roomnr" id="roomnumber">
</div>
<div>
    <label for="Catagory">Catagory:</label>
    <select name="Catagory" id="Catagory">
    <?php 

    $catagorys = DB::Getinstance()->query('SELECT * FROM catagory');

    if ($catagorys->count()) {
        foreach($catagorys->results() as $catagory){?>
        <option value="<?php echo $catagory->name; ?>"><?php echo $catagory->name; ?></option>

        <?php }
    }

     ?>
        </select><br>
</div>
    <div>
        <label for="file">Picture: </label> <input type="file" name="file" id="file" accept="image/*" multiple><br>
    </div>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
    <input type="submit" value="Insert">
</form>

Sorry if the code is not that clean!

For the people that want to take a look: Github

Debreker
  • 206
  • 4
  • 12

1 Answers1

0

Seems the problem was that I forgot one part of my form. it whas the enctype="multipart/form-data".

Debreker
  • 206
  • 4
  • 12