Hi everybody!
I'm trying to learn to use ZF.
I decided to make a very simple image gallery application using MVC architecture.
I took the idea from the phpRiot tutorial on storing images on mysql (you can check it out
here).
I want to upload an image file, then read its content and store the binaries in a mysql table.
I've created a trivial Model, an ImageController with a few actions like add, delete, list on it.
models/Image.php
PHP Code:
class Image extends Zend_Db_Table
{
protected $_name = "image";
}
views/scripts/image/add.phtml:
PHP Code:
<?php echo $this->render('imageHeader.phtml');?>
<h3>Seleccione una Imagen para Subir</h3>
<form enctype="multipart/form-data" action="/image/add" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<label>Imagen: </label><input name="foto[]" type="file" />
<label>Nombre: </label><input name="name" type="text" />
<label>Descripcion: </label><input name="description" type="text" />
<input type="submit" value="Submit" />
</form>
<?php echo $this->render('imageFooter.phtml');?>
controllers/ImageController.php
PHP Code:
class ImageController extends Zend_Controller_Action
{
function init()
{
$this->view->baseUrl = $this->_request->getBaseUrl();
Zend_Loader::loadClass("Image");
date_default_timezone_set('America/Buenos_Aires');
}
function indexAction()
{
$this->view->title = "Imágenes";
}
function addAction()
{
$this->view->title = "Agregar Imagen";
if ($this->_request->isPost()) {
if (is_uploaded_file($_FILES['foto']['tmp_name'])) {
Zend_Loader::loadClass('Zend_Filter_RealPath');
Zend_Loader::loadClass('Zend_Filter_StripTags');
Zend_Loader::loadClass('Zend_Date');
$fecha = new Zend_Date();
$filtroPath = new Zend_Filter_RealPath();
$filtroTags = new Zend_Filter_StripTags();
$binarios = $filtroPath->filter(file_get_contents($_FILES['foto']['tmp_name']));
$infoImagen = getimagesize($_FILES['foto']['tmp_name']);
$ahora = $fecha->get();
$nombre = $filtroTags->filter(trim($this->_request->getPost('nombre')));
$descripcion = $filtroTags->filter(trim($this->_request->getPost('descripcion')));
$data = array(
'mime_type' => $infoImagen['mime'],
'binary' => $binarios,
'size' => $infoImagen[3],
'created_on' => $ahora,
'name' => $nombre,
'description' => $descripcion
);
$imagen = new Image();
$imagen->insert($data);
if(!$imagen) {
$this->view->mensaje = 'Error al insertar datos';
} else {
$this->view->mensaje = 'Foto Exitosamente agregada en el registro '.$imagen;
}
} else {
$this->view->mensaje = 'Error al subir la foto';
}
}
}
function deleteAction()
{
}
function viewAction()
{
}
}
and my public/index.php with bottstrap
PHP Code:
ini_set('include_path',ini_get('include_path').'.:/home/novuste/zf/library');
//vemos todos los errores
error_reporting(E_ALL|E_STRICT);
//incluimos un par más de cosas en el include path
set_include_path('.' . PATH_SEPARATOR . '../application/models/' . PATH_SEPARATOR . get_include_path());
//incluimos una clase loader... ¿por el solo hecho de incluira ya instanciamos la clase?
include "Zend/Loader.php";
//Cargamos la clase de configuración a través de ini y la clase de manejo del registro del framework
Zend_Loader::loadClass('Zend_Config_Ini');
Zend_Loader::loadClass('Zend_Registry');
//creamos un nuevo objeto de confoguracion y cargamos el .ini, la seccion general
$config = new Zend_Config_Ini('../application/config.ini', 'general', false);
$registry = Zend_Registry::getInstance();
$registry->set('config', $config);
//cargamos la clase de manejo de bases de datos y tablas
Zend_Loader::loadClass('Zend_Db');
Zend_Loader::loadClass('Zend_Db_Table');
//creamos un nuevo objeto base de datos, y asigna a la tabla ese objeto de configuración
$db = Zend_Db::factory($config->db->adapter, $config->db->config->toArray());
Zend_Db_Table::setDefaultAdapter($db);
//Cargamos la clase Controller para el front end
Zend_Loader::loadClass('Zend_Controller_Front');
//cargamos el objeto, le permitos lanzar excepciones, configuramos el directorio de controladores, y despachamos
$frontController = Zend_Controller_Front::getInstance();
$frontController->throwExceptions(true);
$frontController->setControllerDirectory('../application/controllers');
$frontController->dispatch();
When i try to upload any image i just receive something like Notice: Array to string conversion in application/controllers/ImageController.php on line 23, and the image isn't uploaded, can anybody give me a tip?
What am i doing wrong?
How can i access in the controller, to the file sended via multipart post form in view?
Thank you!