What is the purpose of the '.' in a CakePHP file upload form element? - cakephp

long time reader, with a small question on my hands.
I have been working on adding multiple file upload support to a CakePHP 2.x project, I've implemented as such in a view here;
<label>Supporting Document(s):</label>
<?php
echo $this->Form->file('files.', ['type' => 'file', 'multiple' => ($allowMultipleSetting ? 'multiple' : false)]);
?>
But I'm a little confused, what is the purpose of the '.' in 'files.' here? I can see what it can do, it gives me an array of file objects in my controller when I do a $this->data['files'], which is desired, and without it, I just have the one file object, even with multiple set to true from option parameters passed to this view.
Does anyone know what 'magic' or Cake 'thing' happens here so that, when a dot/full stop is added to the end of the key name, Cake shows all the images selected?
Thanks!

In Cake typically the first parameter of the FormHelper you are using is for the column name it is using in the database. So it could be Model.column_name for an example, or just coulmn_name as the parameter.
As long as you make sure the column name matches up there you are good. Cake will take care of the rest and output the <input type="file" /> with the correct name attributes.
The "." you are referencing is because that is setup for multiple file uploads, and if it is giving you problems you can remove it and specify the columns in the options. Such as: "'name' => 'data[Model][fieldname][]'
The output Cake needs to create is:
<input type="file" name="filename[Model][col][]" /> so that's why there is no column specified.
Check out this blog post from 2012 about multiple file uploads.
Here is a reference from their documentation specifically about the file input in the form helper as well.

Related

Cakephp Upload Plugin - Saving Custom Path in DIR Field

I am using the https://github.com/josegonzalez/cakephp-upload
'pathMethod' => 'primaryKey'.
And the below path
'path' => '{ROOT}webroot{DS}files{DS}upload{DS}{model}{DS}{field}{DS}',
So, I am stuck at a point where in I want to save the DIR with a more elaborate information than just the primaryKey ID in DIR field
eg DIR field to be saved as "upload/organization/logo/1", currently its just saves "1"
I have gone though the documentation several times, but unable to understand about how to solve the above situation. Please help through with this.
Why want this feature, when everything is happening auto-magically, using only dir field in the view helps a lot, to link the file.
Thanks.
With the current code base, I believe the only solution you have at your disposal is to use the pathMethod of 'flat', use the 'handleUploadedFileCallback' property and manage the handling of directory creation and file movement (from TMP to 'path') on your own...and then also save the 'dir' property of your model (if you're saving that) within that callback (or afterSave) as well. Using 'pathMethod' => 'flat' removes the automatic saving of the file's location.
According to GitHub tickets, it seems that Jose might have plans to implement this feature in the future, but it's a "WIP" (Work in Progress).

Render individual page fields once only in Drupal 7 page.tpl.php

So I gather I can render a specific element of the $page['content'] array like so...
<?php print render(field_view_field('node', $node, 'field_image')); ?>
Where out of the box this will render the element as expected with standard defaults. Ok, so how can I make sure that hand plucked element no longer renders in the <?php print render($page['content']) ?> call later?
Why do I want something dumb like this? Because every page WILL have a header image with a few css tricks for overlays, design and such. But not every page will have attachments, links, and so on... you know, things that are additional fields in the page. So I can't manually print out each field since I don't know how many or what else there is. All I know for sure is the field_image I'm printing above is wrapped in a ton of markup for styling and must be done this way. Same for a few other fields.
Basically I'm looking for a way to unset the field immediately after use.
Does anyone know how to achieve this? I'd rather not make a view or a custom block that displays for specific pages. I eventually have to hand this over to a client who will not be able to wrap their heads around a single page being administered over many places in the CMS.
You can in fact control the display of individual fields for content types in Drupal without having to resort to the function you've used.
Since you know in advance which field(s) you want to suppress, you can turn off its display in the content type settings.
In Drupal 7, see:
Admin >> Structure >> Content types >> your content type >> Manage display
Under "Format" select < Hidden > for the field that you want to omit.
This will prevent the field contents from being displayed within the usual node contents, but field_view_field will naturally still work.
You can also fine-tune your field formats based on different view modes, e.g. choose to display the field within teasers but not in full content.
Looks like this will work:
<?php
print render(field_view_field('node', $node, 'field_image'));
MYTHEME_remove_item($page['content'], 'field_image');
?>
and in template.php file I made this function:
function MYTHEME_remove_item(&$content, $field)
{
foreach($content['system_main']['nodes'] AS $key => $val){
unset($content['system_main']['nodes'][$key][$field]);
}
}
This is ridiculous, in my opinion. I would think a system as robust as drupal would have a solution for something like this. If anyone knows the proper way to do this I will gladly mark them as correct. In the meantime, for others facing similar situations, this worked for me.

CakePHP - Security - Image and Link Helpers

I just read this quite interesting post about security for CakePHP: Cakephp Security
It says that whenever a helper is used, CakePHP basically takes care security risks unless I turn of escape. I believe I only turn off escape when I want my links to be images, so nesting an image helper line inside a link helper line. For example:
echo $this->Html->link($this->Html->image('logo.png'), "/" , array('id'=>'logo', 'escape' => false));
Is that bad practise? Does that leave me vulnerable? Should I be doing it some other way?
Additionally, is it correct that whenever I output database data on dynamic pages, it needs to be enclosed in htmlspecialchars($myvariable)? I don't understand why I need to do that if I know that my database is clean from "bad stuff" and all of my forms for input into my database uses FormHelper.
In the example code shown you have all static values, no content coming from user so there's no risk.
Similarly for your content coming from database if for eg. all content is managed by site admin and no content from users is saved to database its reasonably safe to echo the content without escaping.

How to add a cake php file in another cake php file

I am using cake php now. I have two file example1.ctp and example2.ctp. I want to include example2.ctp in example1.ctp just like how we add php page using "include". I am new to this please suggest me how to do it.
According to me , You have to create Elements..
CakePHP can help you repeat parts of your website that need to be reused. These reusable parts are called Elements.
<?php echo $this->element('ur elements ctp file name'); ?>

How to do form-based file uploads in CakePHP?

I have been looking into this for a while and can't figure it out. Basically I have an add page for my model which you can add a map from a URL or from a file upload. I have got all the fields and validation in but how and where do I manage the uploaded file?? There must be some easy way to do this. Thanks!
Firstly your form needs to be set up to allow file uploads.
<?php echo $form->create(Model, array('type' => 'file')); ?>
This will allow any file inputs to actually upload the file to your server with $form->file(field) or $form->input(field, array('type' => 'file')).
Once the file has been uploaded you should handle everything else from within the Model:
function beforeSave($created) {
extract($this->data[Model][field]);
if ($size && !$error) {
move_uploaded_file($tmp_name, destination);
$this->data[Model][field] = destination;
}
return true;
}
These are only the basics, so be sure to have a play around to find the solution that best fits your needs.
You can use Zend Components to handle the file upload. There is a good example here on my website:
CakePHP file upload using Zend Components
NOTE: MeioUploadBehavior has been deprecated. Instead jrbasso suggests the Upload Plugin.
In addition to the fine answers already given I want to hint about MeioUploadBehavior, currently maintained by jrbasso at github, which has been a great help for me in my own CakePHP project.
You simply add the behavior to your model using the $actsAs field and at the same time specifying any custom preferences. Then create necessary fields (described by supplied docs in detail) in your database, or configure the model to not use any database table. Finally setup the form in your add page, also described in the supplied documentation. The behavior will then take care of the rest for you.

Resources