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

10
  • 1
    Standard advice - set full path for $dir Commented Oct 5, 2014 at 14:12
  • @u_mulder alright. I have done it. Commented Oct 5, 2014 at 14:20
  • Full path means path with $_SERVER['DOCUMENT_ROOT'] or from the root of file system if you put your images not in web-site folder. Commented Oct 5, 2014 at 14:22
  • Turn on error_reporting(-1) to see exactly what doesn't work Commented Oct 5, 2014 at 14:30
  • 1
    Ah I missed that you don't have the attribute enctype="multipart/form-data" in your form tag. Add it and try Commented Oct 5, 2014 at 18:05

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.