Здравейте, първо ще обясня какво се опитвам да постигна, след това ще постна кода който имам до момента. Искам да направя multiple image upload форма, само с едно поле за качване на снимките, по възможност без джаваскрипт. Идеята ми е да има един бутон освен субмита, който като се натисне да пълни някакъв масив примерно и като се субмитне формата да се качат снимките в базата с едно id. В смисъл на едно и също id да отговарят няколко снимки. Ето пример какво очаквам да имам в базата.
| id | image | title | category | text |
-----------------------------------------------------------
| 1 | asd_1.jpg asd_2.jpg | text | text | text |
В момента се качва само по една снимка. Ето и кода който имам.
Това е модела
Това е контролера
Това е вю-то
| id | image | title | category | text |
-----------------------------------------------------------
| 1 | asd_1.jpg asd_2.jpg | text | text | text |
В момента се качва само по една снимка. Ето и кода който имам.
Това е модела
Код:
class UploadModel
{
private $_config;
private $_database;
public function __construct(Config $config, Database $database)
{
$this->_config = $config;
$this->_database = $database;
}
public function run($title, $category, $text, $add_more)
{
$img_name = count($_FILES['image']['name']);
for ($i = 0; $i < $img_name; $i++)
{
$imageFolderPath = realpath('..\uploads') . DIRECTORY_SEPARATOR;
$fileTmpLoc = $_FILES['image']['tmp_name'][$i];
$fileName = $_FILES['image']['name'][$i];
$clasicMimeType = $_FILES['image']['type'][$i];
$exp = explode('.', $fileName);
$end = strtolower(end($exp));
$newFileName = bin2hex(openssl_random_pseudo_bytes(16)) . '.' . $end;
if ($fileTmpLoc == null)
{
throw new \Exception('empty', 405);
}
else
{
if ($clasicMimeType !== 'image/jpg' && $clasicMimeType !== 'image/jpeg')
{
throw new \Exception('mimeType', 405);
}
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->file($fileTmpLoc);
if ($mimeType !== 'image/jpg' && $mimeType !== 'image/jpeg')
{
throw new \Exception('mimeType', 405);
}
if ($end !== 'jpg' && $end !== 'jpeg')
{
throw new \Exception('mimeType', 405);
}
if ($add_more)
{
throw new \Exception('add_more', 405);
}
if (empty($title))
{
throw new \Exception('empty', 405);
}
if (empty($category))
{
throw new \Exception('empty', 405);
}
if (empty($text))
{
throw new \Exception('empty', 405);
}
move_uploaded_file($fileTmpLoc, $imageFolderPath . $newFileName);
$text = nl2br($text);
$this->_database->prepare('INSERT INTO images (image,title,category,text) VALUES(?,?,?,?)', array($newFileName, $title, $category, $text))->execute();
throw new \Exception('ok', 200);
}
}
}
Това е контролера
Код:
class Upload
{
private $_view;
private $_uploadModel;
private $_normalize;
public function __construct(View $view, UploadModel $uploadModel, Normalize $normalize)
{
$this->_view = $view;
$this->_uploadModel = $uploadModel;
$this->_normalize = $normalize;
}
public function run()
{
$submit = $this->_normalize->post('submit', 'trim');
$title = $this->_normalize->post('title', 'trim');
$category = $this->_normalize->post('category', 'trim');
$text = $this->_normalize->post('text', 'trim');
$add_more = $this->_normalize->post('add_more', 'trim');
try
{
$this->_uploadModel->run($title, $category, $text, $add_more);
}
catch (\Throwable $exc)
{
if (isset($add_more))
{
if ($exc->getMessage() == 'add_more')
{
$this->_view->_message = 'message_1';
$this->layout();
}
}
if (isset($submit))
{
if ($exc->getMessage() == 'ok')
{
$this->_view->_message = 'message_2';
$this->layout();
}
elseif ($exc->getMessage() == 'empty')
{
$this->_view->_message = 'message_3';
$this->layout();
}
elseif ($exc->getMessage() == 'mimeType')
{
$this->_view->_message = 'message_4';
$this->layout();
}
}
else
{
$this->_view->_message = 'message_0';
$this->layout();
}
}
}
private function layout()
{
$this->_view->display('header');
$this->_view->display('body_upload');
$this->_view->display('footer');
}
}
Това е вю-то
Код:
<body>
<div class="container">
<form method="post" enctype="multipart/form-data">
<input type="file" name="image[]" multiple="true"/>
<input type="submit" name="add_more" value="Add More Images"/>
<p>Заглавие</p>
<input type="text" name="title"/>
<p>Категория</p>
<select name="category">
<option disabled selected value> ---</option>
<option value="асд">асд</option>
</select>
<p>Текст</p>
<textarea name="text" rows="5" cols="30" wrap="hard">
</textarea>
<br><br>
<input type="submit" name="submit"/>
</form>
<div class="messages">
<p><?php echo $this->_message; ?></p>
</div>
</div>
<script>
$(document).ready(function () {
$(".messages").animate({opacity: 0}, 0);
$(".messages").animate({opacity: 1}, 500);
});
</script>
</body>