I can't seem to be able to save image files uploaded using the FAPI.
The images are uploaded as managed files, I need to save them permanently but they are always saved in the database with a status of 1.
Any idea what I'm doing wrong? Thanks,
//fapi
$form['fieldset_3']['pic_3'] = array(
'#type' => 'managed_file',
'#title' => t('Image'),
'#required' => TRUE,
'#default_value' => variable_get('pic_3'),
'#description' => t('Allowed extensions: gif png jpg jpeg.'),
'#upload_validators' => array(
'file_validate_extensions' => array('gif png jpg jpeg'),
'file_validate_size' => array('MAX_FILE_SIZE' * 1200 * 300),
),
'#theme' => 'image_multifield_multitype',
'#upload_location' => 'public://carousel',
);
//in submit function
$file = file_load($form_state['fieldset_1']['pic_1']);
$file = file_save_upload($file->uri,$validators, 'public://carousel/' . $file->filename, FILE_EXISTS_REPLACE);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
My bad, the problem was the submit handler was not being called...
Load image by the fid and set status to permanent
$file = file_load($form_state['values']['image_fid']);
$file->status = FILE_STATUS_PERMANENT;
$file_saved =file_save($file);
variable_set('image_fid', $file_saved->fid);
Related
I have written a module with form api which can upload image with dynamic file name. I want to upload that file with auto compression. I cannot understand how will I achieve this. How can I implement this in submit callback.
Below my code:
function MYMODULE_registration_form() {
$form['name'] = array(
'#title'=> t('Name'),
'#type' => 'textfield',
'#required' => TRUE,
'#size' => 40,
'#attributes' => array('placeholder' => t('Enter your name'),),
);
$form['personal_info']['upload_pic'] = array(
'#title' => t('Upload Photo'),
'#type' => 'file',
'#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif, bmp'),
);
$form['submit'] = array(
'#value' => t('Submit'),
'#type' => 'submit',
);
$form['#attributes']['enctype'] = 'multipart/form-data';
return $form;
}
function MYMODULE_registration_form_submit($form, $form_state) {
$name = $form_state['values']['name'];
$count = 1;
$original_username = $name;
while (user_load_by_name($name)) {
$name = $original_username . $count++;
}
$validators = array(
'file_validate_extensions' => array('png gif jpg jpeg bmp')
);
$uploadCheck = file_save_upload('upload_pic', $validators);
if($uploadCheck) {
$parts = pathinfo($uploadCheck->filename);
$imagename = str_replace(' ', '_',$original_username.'_'. $count . '.'. $parts['extension']);
$target = "public://attorneys/".$imagename;
file_move($uploadCheck, $target, FILE_EXISTS_REPLACE);
$picture_fid = $uploadCheck->fid;
}
}
After a lot of R&D I have come to this conclusion and it is working fine:
Implement gulp package and install image optimisation plugin.
Take ref from this url : https://www.tutorialspoint.com/gulp/gulp_optimizing_images.htm
I'm using meioupload and everything works well till I try to make thumbnails. Then I can see only kind of phpThumb error message which says
"C:/wamp/cakephp/vendors/phpTmub/img/uploads/product/filename/image.png" does not exist
I'm new with cakePHP so I never faced this problem before. Does anyone know what should I do?
here is my model code:
var $actsAs = array(
'MeioUpload' => array(
'filename' => array(
'create_directory' => true,
'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/png'),
'allowed_ext' => array('.jpg', '.jpeg', '.png'),
'zoomCrop' => true,
'thumbsizes' => array(
'normal' => array('width' => 400, 'height' => 300),
'small' => array('width' => 80, 'height' => 80,'maxDimension' => '', 'thumbnailQuality' => 100, 'zoomCrop' => true),
),
'default' => 'default.jpg'
)
));
UPDATE:
I found special phpThumb for cakePHP 2.0 so the Path changed into this:
"C:/wamp/cakephp/img/uploads/product/filename/image.png"
and if I open default image in my browser th path is like:
localhost:8080/cakephp/img/uploads/product/filename/image.png
Thanks
I solved the problem, maybe not very elegant, but it works!
For the first time, as I said, I downloaded special version of phpThumb for cakePHP.
Here is link: https://github.com/simkimsia/phpThumb-for-cakephp-2.0
After there was my problem with the path because my images was in folder: C:\wamp\www\cakephp\app\webroot\img\uploads\product\filename\image.png
So I had to find this part of code(begins on line 1078):
if ($this->useCake) {
if ($this->config_document_root != null) {
$AbsoluteFilename = $this->config_document_root.DIRECTORY_SEPARATOR.$filename;
} else {
$AbsoluteFilename = WWW_ROOT.DIRECTORY_SEPARATOR.$filename;
}
}
And edit a the path to fit into my folder:
$AbsoluteFilename = $this->config_document_root.DIRECTORY_SEPARATOR.'cakephp'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'webroot'.DIRECTORY_SEPARATOR.$filename;
$AbsoluteFilename = WWW_ROOT.DIRECTORY_SEPARATOR.'cakephp'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'webroot'.DIRECTORY_SEPARATOR.$filename;
and now its working perfectly...
after try many things.. and finally read the answer of Jan Omacka, i only have to edit phpthumb.class.php, line 223 in the constructor function phpThumb()
First
// public: constructor
function phpThumb() {
.....
//comment line
//$this->config_document_root = (!empty($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : $this->config_document_root);
.....
}
//and put the constant CakePHP Var WWW_ROOT (Full path to the webroot.)
$this->config_document_root = WWW_ROOT;
//and if this don't work.. put your real path in linux, or windows
//$this->config_document_root = '/srv/www/htdocs/sic/app/webroot/';
In my case inside webroot, have a folder with estructure 'Usuarios/ID_Prestamo/IDUsuario/', all works great uploading the picture but thumbs not.. so after download the last version of phpThumb, i saw that show an error of File does not exist (URL misformat, for original JPG File), after that download the special versiĆ³n for CakePhp and change the lines as mentioned.. and all works... I use OpenSuse Linux, Apache2, with Cake 2.4.2
Notes: I verified that if not have zoomCrop it not works, this is my structure
'dir' => 'galeria/', //main folder webroot/galeria/
'createDirectory' => true,
'maxSize'=>'5 Mb',
'allowedExt' => array('.jpg','.jpeg','.png'),
'zoomCrop' => true,
'thumbsizes' => array(
'small' => array('width'=>165, 'height'=>115),
'medium' => array('width'=>800, 'height'=>600)
),
I am trying to use CakePdf to generate a pdf file within CakePHP. I have a function in the CourseGradesController called viewReport. I want viewReport to allow you to select a student from a select field, and then upon submitting the form, it will generate a PDF with the appropriate data. If I put the data into a table and do not try to make a PDF, the page will display correctly, so I don't think that is the problem. In bootstrap.php, I have
CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
Configure::write('CakePdf', array(
'engine' => 'CakePdf.WkHtmlToPdf',
'options' => array(
'print-media-type' => false,
'outline' => true,
'dpi' => 96
),
'margin' => array(
'bottom' => 15,
'left' => 50,
'right' => 30,
'top' => 45
),
'binary' => '/var/www/cakephp/app/Plugin/CakePdf/Vendor/WkHtmlToPdf',
'orientation' => 'landscape',
'download' => false
));
I have the WkHtmlToPdf folder moved into /var/www/cakephp/app/Plugin/CakePdf/Vendor/WkHtmlToPdf, so the lib and include folders are in that directory.
In the viewReport function of CourseGradesController, I have
function viewReport($id = null)
{
$this->CourseGrade->id = $id;
if (!$this->CourseGrade->exists())
{
throw new NotFoundException(__('Invalid invoice'));
}
$this->pdfConfig = array(
'orientation' => 'portrait',
'filename' => 'Invoice_' . $id
);
$this->set('invoice', $this->CourseGrade->read(null, $id));
...
}
If I navigate to /courseGrades/viewReport, I get the error "Error: The requested address '/cakephp/courseGrades/viewReport' was not found on this server."
If I naviate to /courseGrades/viewReport/1.pdf, then I just see a completely blank screen.
I would strongly recommend the use of this plugin for this kind of work.
https://github.com/ceeram/CakePdf
I am currently trying to use Miles J plugin located here http://milesj.me/code/cakephp/uploader Although I have made great progress learning CakePhp, I am currently having a problem using the plugin and I would appreciate any help provided.
I have followed all the necessary steps to use the plugin. It has been downloaded put on Plugin folder, I bootstrapped with CakePlugin::loadAll().
So far so good.
Next I have proceed to set up the table as indicated by the plugin developer.
Ok, now back to my own code. I have the following set up:
images_controller.php , image.php and their views.
My goal now is to use the plugin inside those files as such:
App::import('Vendor', 'Uploader.Uploader');
Class ImagesController extends AppController {
var $components = array('Auth');
var $helpers = array('Design');
var $uses = array('Image', 'Uploader.Upload');
function manage(){
//here I show a simple upload form that uses the action saveimage
}
function saveimage(){
$this->Uploader = new Uploader();
if(!empty($this->data)){
$this->Upload->save($this->data);
}
}
}
Now, my model is set as follows
Class Image extends AppModel {
public $useTable = 'uploads';
public $actsAs = array(
'Uploader.FileValidation' => array(
'file' => array(
'extension' => array(
'value' => array('gif', 'jpg', 'jpeg'),
'error' => 'Only gif, jpg and jpeg images are allowed!'
),
'minWidth' => 500,
'minHeight' => 500,
'required' => true
),
'import' => array(
'required' => false
)
),
'Uploader.Attachment' => array(
'file' => array(
'name' => 'uploaderFilename',
'uploadDir' => '/files/uploads/',
'dbColumn' => 'path',
'maxNameLength' => 30,
'overwrite' => true,
'stopSave' => false,
'transforms' => array(
// Save additional images in the databases after transforming
array(
'method' => 'resize',
'width' => 100,
'height' => 100,
'dbColumn' => 'path_alt'
)
),
'metaColumns' => array(
'size' => 'filesize', // The size value will be saved to the filesize column
'type' => 'type' // And the same for the mimetype
)
),
'import' => array(
'uploadDir' => '/files/uploads/',
'name' => 'uploaderFilename',
'dbColumn' => 'path',
'overwrite' => true,
'stopSave' => false,
'transforms' => array(
array(
'method' => 'scale',
'percent' => .5,
'dbColumn' => 'path' // Overwrite the original image
)
)
)
)
);
}
}
On my model for testing purposes I have not changed anything but just copied and pasted the very same array as shown in the the Test/Model folder inside the plugin, which is meant to show the functionality of the plugin.
The following confusion, errors or lack of understanding is taking place:
My file is not being uploaded to the webroot/files/uploads folder
My data is being inserted in the database, but not in a complete manner by leaving empty as shown:
id | caption | path | path_alt | created |
4 | | | |2012:02:..|
Above, I expect the path to be saved, but it doesn't.
I have to admit my confusion comes mainly from my inexperience using plugins, so I am aware I might be doing something wrong regarding my models or my configuration.
Any prompt help would be appreciated greatly as I have tried to work this out on my own without any success.
A few things:
1 - In your controller you do not need to import the Uploader class. You also don't need to use the Uploader.Upload model (it's merely an example test case). All you need to do in the controller is call $this->Image->save() which will upload the file and save the path as a row into the database (if you defined the Attachment in Image).
2 - In your view, create the file input. Pay attention to the input name.
echo $this->Form->input('FILE_INPUT_NAME', array('type' => 'file'));
3 - In your Image model, setup the AttachmentBehavior and its options for that specific input field:
'Uploader.Attachment' => array(
'FILE_INPUT_NAME' => array(
'uploadDir' => '/files/uploads/',
'dbColumn' => 'path'
),
'ANOTHER_INPUT' => array()
);
Be sure that the column "path" exists in your images table. And thats it.
For more information on what each option does, check out the following: http://milesj.me/code/cakephp/uploader#uploading-files-through-the-model
On my files model:
var $actsAs = array(
'Uploader.FileValidation' => array(
'file' => array(
'extension' => array('gif', 'jpg', 'png', 'jpeg'),
'filesize' => 5242880,
'required' => true
)
),
'Uploader.Attachment' => array(
'file' => array(
'uploadDir' => 'upload/', // Where to upload to, relative to app webroot
'dbColumn' => 'path', // The database column name to save the path to
'maxNameLength' => 30, // Max file name length
'overwrite' => true, // Overwrite file with same name if it exists
'name' => '', // The name to give the file (should be done right before a save)
'transforms' => array() // What transformations to do on images: scale, resize, etc
)
)
);
And on the controller:
$this->File->Behaviors->Attachment->update('File', 'file', array('name' => 'testing')));
if ($this->File->save($this->data)) {
The file is uploaded fine, and the record is saved on the database. But I wanted to rename the file to avoid people finding the archives by mistake.
Thanks!
I have run into this same problem. You can accomplish the same task by doing this before you save.
$this->data['File']['file']['name'] = 'myNewName';