多个文件上传并存储在一个目录中 [英] Multiple file upload and store them in a directory
问题描述
我正在尝试上传多个文件并将它们存储到特定酒店房间的目录中.
PHP 将检查是否已经存在一个目录,如果没有则创建它.但是,将文件移动到该目录的部分不起作用.
我的创建房间功能:
public function create($fields = array(), $file) {if(!$this->_db->insert('rooms', $fields)) {throw new Exception('创建房间时出现了一个小问题');}别的{$roomNR = $fields['roomnr'];$dir = $_SERVER['DOCUMENT_ROOT'] ."/images/rooms/$roomNR";如果 ( !file_exists($dir) ) {mkdir ($dir, 0744);}move_uploaded_file($file["file"]["tmp_name"], "$dir/" . $file["file"]["name"]);}}
这就是我将所有内容传递给我的函数的方式:
尝试{$newRoom->创建(数组('roomnr' =>输入::get('roomnr'),'类别' =>Input::get('Category'),),$_FILES);Session::flash('success', '房间已添加');//Redirect::to('admin-rooms.php');} catch(异常$e){死($e->getMessage());}
这是我使用的表格:
<div><label for="Catagory">类别:</label><select name="类别" id="类别"><?php$catagorys = DB::Getinstance()->query('SELECT * FROM catagory');如果 ($catagorys->count()) {foreach($catagorys->results() as $catagory){?><option value="<?php echo $catagory->name; ?>"><?php echo $catagory->name;?></option><?php }}?></选择><br>
<div><label for="file">图片:</label><input type="file" name="file" id="file" accept="image/*" multiple><br>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>"><输入类型=提交"值=插入"></表单>
对不起,如果代码不那么干净!
想看的人:Github
问题似乎是我忘记了表单的一部分.它有 enctype="multipart/form-data"
.
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
Seems the problem was that I forgot one part of my form. it whas the enctype="multipart/form-data"
.
这篇关于多个文件上传并存储在一个目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!