Zend Form file input set required false is not working - zend-framework

I'm having the following code to render a zend form file input
$pd_photo = new Zend_Form_Element_File('photo');
$pd_photo->setDestination(APPLICATION_PATH . '/../uploads');
$pd_photo->setRequired(false);
$pd_photo->setAllowEmpty(true);
$pd_photo->addValidator('Count', false, 1);
$pd_photo->addValidator('Size', false, 2097156672);
$pd_photo->addValidator('Extension', false, 'jpg,jpeg,png,gif,bmp');
$pd_photo->getValidator('Count')
->setMessage('You can upload only one file');
$pd_photo->getValidator('Size')
->setMessage('Your file size cannot upload file size limit of 1 MB');
$pd_photo->getValidator('Extension')
->setMessage('Invalid file extension, only valid image extensions are
(jpg, jpeg, png, gif, bmp) allowed.');
All is working fine but when I leave the file filed empty then, it does not work. The zend form validator returns an empty error string message.
What wrong am doing??

This is my first answer, this might be right because,
I also once had the similar problem, I saw that you have used both
$pd_photo->setRequired(false);
$pd_photo->setAllowEmpty(true);
In this case you should also define for 'NotEmpty' => 'false'
Try defining the above property for file element this should solve your problem, mine was resolved by this.

Related

PreloadJS - Loading an image from a URL without file format

I'm trying to use PreloadJS to load an image. I took the getting started code from http://www.createjs.com/getting-started so,
function loadImage() {
var preload = new createjs.LoadQueue();
preload.addEventListener("fileload", handleFileComplete);
preload.loadFile("assets/preloadjs-bg-center.png");
}
function handleFileComplete(event) {
document.body.appendChild(event.result);
}
Which works great, but changing the URL to a custom one doesn't play so nice:
preload.loadFile("http://localhost:3000/URLthatReturnsAnImageWithoutFileType");
I'm unable to render the image. I'm guessing the loadFile function parses the file type in the provided URL string. Is there a way to tell PreloadJS the provided asset is indeed an image and render it?
According to LoadQueue's documentation, the file type of an item is determined by it's file extension. If it doesn't have an extension, then you can pass in a type property:
preload.loadFile({src:"hello", type:createjs.AbstractLoader.IMAGE});

Images upload form laravel

My image upload doesn't work:
Controller:
if (Input::hasFile('image')) {
$bikecreate->image = Input::file('image');
$destinationPath = public_path().'/upload/';
$filename = str_random(6) . '_' . $bikecreate->users_id ;
Input::file('image')->move($destinationPath, $filename);
}
Form:
{{ Form::file('image', array('files' => true)) }}
After accepting form everything looks ok, but after the end of upload, filepath in database show .tmp/file at my server.
Without seeing the rest of your code it's hard to see exactly what's going on but my guess is that your line $bikecreate->image = Input::file('image') is where you're setting the file's path for the database. You've actually set the UploadedFile instance as the image property on $bikecreate there, which, presumably, when serialised to something to put into the database gets __toString() called on it.
__toString() called on a File instance (which itself inherits __toString from SPLFileInfo returns the path to that file. So you'd think you're get the uploaded filename, but actually because an uploaded file is actually a temporary file in PHP, you get the temporary name.
Try changing that line to the following:
$bikecreate->image = Input::file('image')->getClientOriginalName();
This retrieves the actual original name of the uploaded file, not the temporary path given to it by PHP.
It goes without saying that this is only pertinent to UploadedFiles, normal files should just be able to be __toStringed to get the path to the file, although you'll notice that it would be the full path and not the basename. To get that, use getBaseName().

Using perl mime::entity for attaching files?

below shows how one can add a file to a email being built using mime::entity
my question is instead of specifying a path to file, is there a way to add it via a varible which contains the context of the file, ??
### Attachment #2: a GIF file:
$top->attach(Path => "./docs/mime-sm.gif",
Type => "image/gif",
Encoding => "base64");
Yes, you can. You need to drop the Path parameter and instead use Data, e.g.
### Attachment #2: a GIF file:
$top->attach(Data => $my_gif_contents,
Type => "image/gif",
Encoding => "base64");
Data is a little bit buried in the MIME::Entity documentation, I must admit! I only know it from using the same parameter in MIME::Lite.

Adobe Reader cannot open .pdf file created using mPDF in Zend Framework

I'm trying to generate a .pdf file using mPDF in a Zend Framework application, from the output of the action.
Here is the code of my action:
public function testAction()
{
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
$this->view->foo = 'bar';
$this->render();
$output = $this->getResponse()->getBody();
$layout = new Zend_Layout();
$layout->content = $output;
$layout->setLayoutPath(dirname(dirname(__FILE__)) . '/views/layouts/');
$layout->setViewSuffix('tpl');
$layout->setLayout('pdf');
$html = $layout->render();
$mpdf = new mPDF('utf-8', 'A4');
$mpdf->WriteHTML($html);
$mpdf->Output('report.pdf', 'D');
}
If the content to be displayed is long (i.e. a few paragraphs), when downloading the .pdf file, Adobe Reader throws the following error: Adobe Reader could not open 'report.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
However, if I pass the same output to mpdf as a static variable, without using Zend Layout render, then the .pdf file opens without any errors. Also, Adobe Reader throws the issue if and only if the content is long (i.e. it works if there are only a couple of words).
Is there any limit I should be aware of?
there should not be space at beginning and at the end of file so check these space,
Adobe Reader is less forgiving than some othe PDF readers if the PDF file is corrupt. Open your PDF document in a text editor and check that the file starts with something like:
%PDF-1.4
%âãÃÓ
Sometimes PHP error notices are found at the top of the file.
Source : mpdf forum IanBack's answer

How to use form validation with the file uploader to make sure a file is uploaded

Can anyone suggest how using the form validation rules I can say the following:-
If no file is uploaded - then create a rule to say 'no file uploaded' using the form validator library.
I am using CodeIgniter 2.
For instance - it is simple to validate on a text input field using the following, but I cannot understand how this is done with upload (that uses the $_FILES array rather than $_POST)
eg.
$this->form_validation->set_rules('title', 'Title', 'required'); // input field named 'title' is required
CodeIgniter's File Uploading class handles its own validation - no need to use the Form Validation class.
Per the documentation,
$this->upload->display_errors()
Retrieves any error messages if the do_upload() function returned
false. The function does not echo automatically, it returns the data
so you can assign it however you need.
In your example above, if the file input is left empty, $this->upload->display_errors() will return the following:
You did not select a file to upload.
So, set your preferences (see the "Setting Preferences" section of the documentation for exactly what is available):
// The following are just examples from the documentation...
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// ... other preferences as necessary
If any of the above fail during the attempted upload, $this->upload->display_errors() will retrieve the appropriate error message. Simply pass it to your view to display the error(s).
Hope that helps.