A
A
Anton2015-12-02 09:09:19
Kohana
Anton, 2015-12-02 09:09:19

Kohana, slider, uploading photos through the admin panel, how to upload a photo?

Hello, I put a slider on the site, I decided to upload photos through the admin panel, then display the photo in the slider.
Can't upload photos.
What I've done.
Created a table: Table
name: slyders
9a60e797b3524c2a878ac9b8ae730d6b.jpg
Then added the field slyder_id - int99 to the images table
Created a model:
Named slyder.php

<?php defined('SYSPATH') or die('No direct script access.');

class Model_Slyder extends ORM {
  
protected $_table_name = 'slyders';

protected $_primary_key = 'id';

protected $_db_group = 'default';
  
protected $_has_many = array(
'images' => array(
'model' => 'image',
'foreign_key' => 'slyder_id',
),
);
    
}

Created a template for loading:
/application/views/admin/slyder/slyder_index.php
<h2>Слайдер, загрузка фоток</h2>

<form action="/admin/slyder" method="post" class="form-horizontal">

<div class="form-actions">
Фотография не должна быть меньше <strong>1200x270px.</strong><br/>
Размеры фотографий в слайдере <strong>1200x270px</strong>. </div>

<div class="form-actions">
<input name="images[]" type="file" multiple id="multi" title="Загрузка фотографий" class="btn btn-success"/>
</div>

<br/>

<? if (!empty($data->images)): ?>
<a name="img"></a>

<? foreach ($data->images as $i => $image): ?>
<div class="fotoadmi1n">
<div class="fotoadminnn1">
<?= html::anchor('media/uploads/slyder/' . $image->name, html::image('media/uploads/slyder/' . $image->name), array('target' => '_blank')) ?>
<br><?= html::anchor('admin/slyder/delimg/' . $image->id, 'Удалить') ?>
</div>
</div>
<? if ($i % 2): ?>

<? endif ?>
<? endforeach ?>

<? else: ?>
<!--<div class="empty">Нет изображений</div>-->
<? endif ?>

<div style="clear:both"></div>
    
<div class="form-actions" style="display:inline-block">
<div style="margin-top: 0px; margin-bottom: -4px">
<button type="submit" name="submit" id="submit" class="btn btn-primary">СОХРАНИТЬ</button>
</div>
</div>
</form>

Next, I created controllers, etc.:
/application/classes/controller/admin
slyder.php
<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Slyder extends Controller_Admin {

public function before() {
parent::before();
    
$this->template->page_title = 'Настройки слайдера, загрузка фоток';
    
// для загрузки фоток
$this->template->scripts[] = 'media/js/jquery-1.7.2.min.js';
$this->template->scripts[] = 'media/js/jquery.MultiFile.pack.js';
$this->template->scripts[] = 'media/js/upload.js';
}

public function action_index() {
    
$slyders = ORM::factory('slyder')->where('id', '=', '1')->find();

$data = $slyders->as_array();
$data['images'] = $slyders->images->find_all()->as_array();

if (isset($_POST['submit']))
{
$data = Arr::extract($_POST, array('contemail', 'conttell', 'contskype'));
$slyders->values($data);
$slyders->save();	

if (!empty($_FILES['images']['name'][0])) {
foreach ($_FILES['images']['tmp_name'] as $i => $image) {
$filename = $this->_upload_img($image, strtolower(substr(strrchr($_FILES['images']['name'][$i], '.'), 1)));
if ($filename) {
$im_db = ORM::factory('image');
$im_db->slyder_id = $slyders->pk();
$im_db->name = $filename;
$im_db->save();
}
}
}
}
               
$content = View::factory('admin/slyder/slyder_index')
->bind('data', $slyders);
        
$this->template->page_title = 'Настройки слайдера, загрузка фоток';
$this->template->content = $content;
}



public function _upload_img($file, $ext = NULL, $directory = NULL)
{

if ($directory == NULL) {
$directory = 'media/uploads/slyder';
$smalldirectory = 'media/uploads/slyder/small/';
}
if ($ext == NULL) {
$ext = 'jpg';
}
$symbols = '0123456789abcdefghijklmnopqrstuvwxyz';
$filename = '';
for ($i = 0; $i < 10; $i++) {
$filename .= rand(1, strlen($symbols));
}
$im = Image::factory($file);
if ($im->width < 350 OR $im->height < 350) {
return false;
} else {
$im->resize(1000, 800);
$im->save("$directory/$filename.$ext");
$im->resize(350, 197, Image::INVERSE);
$im->crop(350, 197);
$im->save("$smalldirectory/small_$filename.$ext");
return "$filename.$ext";
}
}

}

I also made a folder for uploading photos:
/media/uploads/slyder/
set the rights to 777
I go into the section itself in the admin panel, select a photo and try to upload it, but it doesn’t upload anything.
I look at the table there is empty.
It was the problem with uploading a photo, I tried to do it using other methods, but it doesn’t upload and doesn’t write errors.
Tell me what I did wrong, maybe I put something wrong in some places.
Maybe something needs to be added to the photo upload code so that saving to id 1 is done.
The goal is to upload photos, and see in this section those that have been uploaded, after selecting the photos, I click on the save button and the download should take place and the page is reloaded. but there is no photo anywhere, not in the table, not in the download folder.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kravchenko, 2015-12-02
@mydearfriend

everything seems simple
<form enctype="multipart/form-data">...</form>

G
Grigory Vasilkov, 2015-12-08
@gzhegow

How are you, "purely for yourself" or is there a resource to pay, while I will do and explain to you on Skype live?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question