I have a question on uploading may images in Yii. Where should I put this code?
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'topic-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
Folks from the Yii forums also said to use this:
$this->widget('CMultiFileUpload', array(
'name' => 'images',
'accept' => 'jpeg|jpg|gif|png',
'duplicate' => 'Duplicate file!',
'denied' => 'Invalid file type',
));
Where should I put that also? What my problem is using those codes I can't select many image, I'm only allowed to select one image. What should I use to select more than one image?
One more thing, it has been recommended to me to use this:
if(!is_dir(Yii::getPathOfAlias('webroot').'/images/ADD YOUR PATH HERE!/'. $model->name)) {
mkdir(Yii::getPathOfAlias('webroot').'/images/ADD YOUR PATH HERE!/'. $model->name))
chmod(Yii::getPathOfAlias('webroot').'/images/ADD YOUR PATH HERE!/'. $model->name)), 0755);
}
Should I put this on the controller?
You can try EAjaxUpload. I use it and it's very good tool for multi uploading and also have a good documentation. Here is the link: Yii EAjaxUpload extension
Related
I'm trying to create a XML import module that will convert given file to CSV format and then use that CSV to import categories and products.
I have a working configuration page made with getContent() it basically calls a method that generates this form via $helper->generateForm(). $helper is a HelperForm() object.
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'type' => 'file',
'label' => $this->l('XML file'),
'name' => 'XMLIMPORT_XML_FILE',
'desc' => $this->l('Select file you wish to import.'),
'required' => true
),
array(
'col' => 3,
'type' => 'text',
'prefix' => '<i class="icon icon-envelope"></i>',
'desc' => $this->l('Enter a valid email address'),
'name' => 'XMLIMPORT_LINES',
'label' => $this->l('Records per file'),
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
I need to get this data to my XML converter. How do I upload a file (around 10-20MB) to Prestashop to be then able to do other stuff with it? How to save it permanently on the server?
I tried doing this:
return array(
'XMLIMPORT_XML_FILE' => Configuration::get('XMLIMPORT_XML_FILE', null),
'XMLIMPORT_LINES' => Configuration::get('XMLIMPORT_LINES', 1000)
);
And after that this:
$form_values = $this->getConfigFormValues(); // returned array from above
foreach (array_keys($form_values) as $key)
Configuration::updateValue($key, Tools::getValue($key));
And later using my own class for XML conversion like this, hoping that it will give me file handle.
$xml_converter = new XMLToCSVConverter(Configuration::get('XMLIMPORT_XML_FILE'), 'output', 'example_products.php');
Apparently it didn't as nothing happens. The class itself is working fine outside of Prestashop module. The constructor is __construct($xml_file, $csv_filename, $template_file).
I need to pass the file I upload to that constructor. I've been struggling for days now.
#edit: I can see the contents of the file inside the HTTP call when submit is clicked. But how do I pass that file to my class?
As far as I remember 'type' => 'file', doesn't actually save any values in the database. This type is only meant to output a file field in your form.
After submitting, you should then do you custom processing with $_FILES['XMLIMPORT_XML_FILE'] : move to upload/ or whereever you want.
'XMLIMPORT_XML_FILE' => Configuration::get('XMLIMPORT_XML_FILE', null), won't return you anything. After uploading you my wish to save the uploaded file path here, but it won't show up next time in the form unless you build the output yourself.
Module configratuon is meant to save text config values. Handling files is trickier and you have to do them yourself each time.
EDIT:
To intercept the saving process, look up the submit button name and make an if statement:
public function getContent() {
if(Tools::isSubmit('submitButtonName')) {
error_log(print_r($_FILES, 1));
}
}
There's probably a function postProcess which does the same (it looks like you copied the methods from a default module).
prestashop handles the image upload with ImageManager class, this class contains more methods which are useful for handling image upload, resize etc.. so its better refer the default homeslider module for the image upload using a module. This module is handling the image upload process in postProcess method with the help of ImageManager class, this class methods will do the all the processes related to upload.
How are you supposed to prevent the CMultiFileUpload widget from appending the file names of selected files to the page?
Here is my code:
<?php
$this->widget('CMultiFileUpload', array(
'name' => 'images',
'accept' => 'jpeg|jpg|gif|png',
'denied' => 'Invalid file type',
'htmlOptions' => array('multiple'=>'multiple'),
));?>
Here is a picture of what I am talking about:
I need to remove what the arrows point to. It's also strange why it says "No files selected" when that isn't the case. If I submit the form, the file does indeed get sent to the server.
Edit:
It does what I want if javascript is turned off though. It also fixes the "no files selected" error. Is there a way to disable the javascript for just the widget?
if you want to hide the names of the files you have uploaded then you can use the options in your CMultiFIleUpload
eg:-
<?php
$this->widget('CMultiFileUpload', array(
'name' => 'images',
'accept' => 'jpeg|jpg|gif|png',
'denied' => 'Invalid file type',
'htmlOptions' => array('multiple'=>'multiple'),
'options'=>array(
'onFileAppend'=>'
function(e,v,m)
{
// try using e.preventDefault();
(".MultiFile-label").css("display","none");
}
'
)
));?>
Note:- I haven't tested it but hope it helps.
In my web application I need to upload images from my system and use it in Yiistrap carausel. I copied the image into my images directory and gave the address .But its not recognizing , its not showing the image although I gave the correct adress of my image
My code for using the image in Yiistrap widgets.
<?php echo TbHtml::carousel(array(
array('image' => '/var/www/store3/images/download.jpg/830x477', 'label' => 'First Thumbnail label', 'caption' => 'image1'),
array('image' => '/var/www/store3/images/download1.jpg/830x477', 'label' => 'Second Thumbnail label', 'caption' => 'image2'),
)); ?>
I also tried all combinations by removing the full path naming ,adding the relative path , but nothing seems to work .
The error I am getting is..
Failed to load resource: the server responded with a status of 404 (Not Found)
Any body kindly help me with this .I am stuck up
Try this. It worked at my end. Using Yii's baseurl is the best way to reference directories.
<?php echo TbHtml::carousel(array(
array('image' => Yii::app()->request->baseUrl.'/images/download.jpg', 'label' => 'First Thumbnail label', 'caption' => 'image1'),
array('image' => Yii::app()->request->baseUrl.'/images/download1.jpg', 'label' => 'Second Thumbnail label', 'caption' => 'image2'),
)); ?>
I'm using cakephp-upload plugin of jose gonzalez to upload images to our app. By default, they're being saved in a directory like this: webroot/files/user/photo/{user_id} which is fine, except for when we want to display such images using $this->Html->image() which searches for images in the webroot/img directory.
I have already tried to display the images with
echo $this->Html->image('../files/user/photo/' .
$user['User']['photo_dir'] . '/' .
$user['User']['photo']);
which works but I was wondering if there's some way to tell this plugin to save into the img directory? The documentation doesn't mention any of that.
And also, is there any way to tell the $this->Form->input('User.photo', array('type' => 'file')); to accept only image files?
as you can see in this file, path is set like:
public $defaults = array(
'rootDir' => null,
'pathMethod' => 'primaryKey',
'path' => '{ROOT}webroot{DS}files{DS}{model}{DS}{field}{DS}',
...
you could change it to make:
'path' => '{ROOT}webroot{DS}img{DS}'
and for your second question, you could use accept attribute, like:
$this->Form->input('User.photo',
array(
'type' => 'file',
'options' => array('accept' => 'image/*')
)
);
I got a insert form. I would like to insert the below code into the form.
<?php
echo $html->image("slide_03.jpg", array(
"alt" => "Event Banner",
'class' => '',
));
?>
In my view layer I want the the image to be display. However, the image did not display and got this code in my view layer instead
image("slide_03.jpg", array( "alt" => "Event Banner", 'class' => '', )); ?>
In CakePHP 2.x you have to use $this->Html->image(). $html->image() was used in previous CakePHP versions and no longer works in CakePHP 2.x.
In general (not just for images) you can set option after or before if you want additional html elements near of your input element.
As you see from sample below you can combine input options with $this->Html->image().
<?php
echo $this->Form->input('field', array(
'before' => $this->Html->image(),
'after' => $this->Html->image(),
));
More details about options on documentation.