I am trying to create a simple Cakephp 2.5.1 app that accepts file upload. I am using the file uploader plugin provided in http://milesj.me/code/cakephp/uploader. I followed the instructions in the website and got it installed and it seems to work fine.
The code below is added to the model.php. There is a column 'picture' in the model table.
public $actsAs = array(
'Uploader.Attachment' => array(
// Do not copy all these settings, it's merely an example
'picture' => array(
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => '',
'transportDir' => '',
'finalPath' => '',
'dbColumn' => '',
'metaColumns' => array(),
'defaultPath' => '',
'overwrite' => false,
'stopSave' => true,
'allowEmpty' => true,
'transforms' => array(),
'transformers' => array(),
'transport' => array(),
'transporters' => array(),
'curl' => array()
)
)
);
In the associated ctp file, I have the following code;
<?php
echo $this->Form->create('Upload', array('type' => 'file'));
echo $this->Form->input('picture', array('type' => 'file'));
echo $this->Form->end('Submit');
?>
I managed to upload an image file SSSS.jpg to the webserver. However, the strange thing is that the uploaded image file is a tiny file which contains the HTTP POST information in text, not the actual image data. The file contents looks something like this;
_method=POST&_method=POST&data%5BUpload%5D%5Bpicture%5D=SSSS.jpg
Can someone tell me what are the possible things that have gone wrong? Thank you.
EDIT: I just noticed in Chrome debugging that under the HTTP POST method, the file sent was of type text/html. Is this a problem? If yes, how do I change the type to image/jpg ?
Change the following line
echo $this->Form->input('picture', array('type' => 'file'));
To the following syntax
echo $this->Form->input('picture', array('type' => 'file/image'));
Sample Code:
echo $form->labelTag('File/image', 'Image');
echo $html->file('File/image');
Related
quick question that has been bugging me for days now. I just installed CKfinder and "browsing" works perfect. Except when I want to upload a image or file, it gives me the following error:
Fatal error: Uncaught exception 'ErrorException' with message
'fopen(/home/xxx/app/userfiles/images/sdfdsf.jpg): failed to open
stream: Permission denied' in
/home/xxx/app/webroot/js/packages/finder/core/connector/php/vendor/league/flysystem/src/Adapter/Local.php:142
I used the following settings:
$config['backends'][] = array(
'name' => 'default',
'adapter' => 'local',
'baseUrl' => '/app/userfiles/',
'chmodFiles' => 0777,
'chmodFolders' => 0755,
'filesystemEncoding' => 'UTF-8',
);
$config['backends'][] = array(
'name' => 'ftp',
'adapter' => 'ftp',
'host' => 'xxx',
'username' => 'xxx',
'password' => 'xxx'
);
$config['resourceTypes'] = array(
array(
'name' => 'Files',
'directory' => '/home/websites/www/shared/images/ckfinder/',
'maxSize' => 0,
'allowedExtensions' => 'pdf,doc,zip',
'backend' => 'ftp',
'lazyLoad' => true
),
array(
'name' => 'Images',
'directory' => '/home/websites/www/shared/images/ckfinder/',
'maxSize' => 0,
'allowedExtensions' => 'gif,jpeg,jpg,png',
'backend' => 'ftp',
'lazyLoad' => true
)
);
As said, browsing images that are located in /app/userfiles/ are working perfect. It even returns the name to my input field.
But at the moment I want to upload a image or file I get this error. Anyone who can tell me how to fix this issue?
P.S. The folders have CHMOD 777 so that should be fine. It seems like the error says that it is trying to upload the file, or access it from the wrong directory e.g. my settings are wrong :)
This was just a test but the local resource will be removed, image/file browsing should be from the FTP only, same as the upload directory that is :)
Looks like you tried to define absolute paths for resource types. The path used in directory option of the resource type should be relative to the root of the used backend.
Please have a look at following places in the PHP connector docs:
http://docs.cksource.com/ckfinder3-php/upgrading.html#upgrading_2to3_resource_types
http://docs.cksource.com/ckfinder3-php/howto.html#howto_resource_type_folder
You can use the absolute path to define the backend root, and then use paths relative to the defined root in resource types directory options. For example:
$config['backends'][] = array(
'name' => 'ftp',
'adapter' => 'ftp',
'host' => 'xxx',
'username' => 'xxx',
'password' => 'xxx',
'root' => '/home/websites/www/shared/images/ckfinder/'
);
$config['resourceTypes'] = array(
array(
'name' => 'Images',
//'directory' => '', You can also omit this option - the resource type in this case will be attached to the backend root
'maxSize' => 0,
'allowedExtensions' => 'gif,jpeg,jpg,png',
'backend' => 'ftp',
'lazyLoad' => true
)
);
I have created some custom page on frontend for particular type of node modyfications. Here's my page callback:
function vintranet_talk_edit_entry_page_callback($sNid) {
module_load_include('inc', 'node', 'node.pages');
$oNode = node_load($sNid);
return drupal_get_form('page_node_form', $oNode);
}
My node has one field with file attachments.
Config:
'vintranet_talk_attachments' => array(
'field_name' => 'vintranet_talk_attachments',
'label' => t('Attachments'),
'type' => 'file',
'cardinality' => -1,
),
Instance:
'vintranet_talk_attachments' => array(
'field_name' => 'vintranet_talk_attachments',
'label' => t('Attachments'),
'entity_type' => 'node',
'bundle' => 'intranet_talk_page',
'widget' => array(
'type' => 'file_mfw',
),
'settings' => array(
'max_filesize' => 10,
'file_directory' => 'intranet/talk',
'file_extensions' => 'jpg, png, gif, pdf, zip, doc, rtf, xdoc, rar',
'description_field' => 1,
),
'display' => array(
'default' => array(
'type' => 'file_table',
),
),
),
My first problem is, when I want to upload JPG file after module installation, system sends me this message:
So ok... I'm going to check that particular field settings in Structure and I see this:
Why the hell it is saved like this?!
Okaaaaay.... so I'm changing this form field value on jpg, png, gif, pdf, zip, doc, rtf, xdoc, rar, saving and trying to upload the image one more time...
....clickin "Upload" button.... aaaaandd....
....yup.... that's my 2nd problem :/. Have no idea why it's not working. On the backend, in other hand, the "Upload" button works perfectly. Am I missing something?
(working on Drupal 7.31 version)
Menu node add path:
array(
'mynode/add/path' => array(
'title' => 'Title - new entry',
'page callback' => 'vintranet_talk_add_entry_page_callback',
'file' => 'vintranet_talk.pages.inc',
'access arguments' => array('vintranet_talk_add_entry'),
),
);
Answer for my problems:
function hook_menu_alter(&$aItems) {
$sNodePath = drupal_get_path('module', 'node');
$aItems['file/ajax']['file path'] = $sNodePath;
$aItems['file/ajax']['file'] = 'node.pages.inc';
$aItems['system/ajax']['file path'] = $sNodePath;
$aItems['system/ajax']['file'] = 'node.pages.inc';
}
I have a problem with uploading Images in a Zend Framework 2 Application and i think i have a configuration error somewhere in my code.
This is my current Code. I created the Form and Filter like the ZfcUser Form.
Form extends ProvidesEventsForm(ZfcBase)
$file = new Element\File('image');
$file->setLabelAttributes(array('class' => 'control-label col-sm-4'));
$file->setLabel('image');
$this->add($file);
Filter extends ProvidesEventsInputFilter(ZfcBase)
$this->add(
array(
'type' => 'Zend\InputFilter\FileInput',
'name' => 'image',
'required' => true,
'validators' => array(
array(
'name' => 'File\UploadFile',
),
),
'filters' => array(
array(
'name' => 'File\RenameUpload',
'options' => array(
'target' => './public/img/uploads',
'randomize' => true,
),
),
),
)
);
In the validation process the method FileInput::isValid() is called - but the Value is always null and i have no clue why it is.
The HTML-Form is set to multipart/form-data and the Server configuration is also no problem.
The file i used for testing is only 80KB.
Anyone an idea, what is wrong?
Finally i found a solution for the Problem.
Before the Post information added to the form, they have to merged with the file information:
$files = $this->getRequest()->getFiles()->toArray();
$post = array_merge_recursive(
$this->getRequest()->getPost()->toArray(),
$files
);
$form->setData($post);
Is it possible to do a attribute what will be file upload (not image)?
I tryed like this:
<?php
$installer = $this;
$installer->startSetup();
$attribute = array(
'type' => 'file',
'label'=> 'Catalog Pdf',
'input' => 'file',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => "",
'group' => "General Information"
);
$installer->addAttribute('catalog_category', 'catalog_pdf', $attribute);
$installer->endSetup();
It's showing file upload in category, but probably something is wrong because after installation subcategories are not showing (ajax problem), and in frontend categories view I get "There has been an error processing your request"
File upload attribute type is not present. So, the sql which run on setup is creating the issue
Try something like:
'type' => 'varchar',
'input' => 'image'
This makes input type as file upload but process it in default image processing
'backend_model' => 'xxx'
Here you can specify where this would be processed
I've integrated the latest version of KCFinder into CKEditor - whenever I click the 'Browse Server' button on the CKEditor toolbar it opens the KCFinder dialog box and downloads the 'browse.php' file rather than display the contents of a directory.
It is almost as if for some reason the PHP engine stops working temporarily - i've not modified any of the htaccess files.
I'm currently using the standard config.php within the kcfinder directory - would anyone know what may have caused this and how to fix it?
// kcfinder/config.php
$_CONFIG = array(
'disabled' => true,
'denyZipDownload' => false,
'denyUpdateCheck' => false,
'denyExtensionRename' => false,
'theme' => "oxygen",
'uploadURL' => "",
'uploadDir' => "",
'dirPerms' => 0755,
'filePerms' => 0644,
'access' => array(
'files' => array(
'upload' => true,
'delete' => true,
'copy' => true,
'move' => true,
'rename' => true
),
'dirs' => array(
'create' => true,
'delete' => true,
'rename' => true
)
),
'deniedExts' => "exe com msi bat php phps phtml php3 php4 cgi pl",
'types' => array(
// CKEditor & FCKEditor types
'files' => "",
'flash' => "swf",
'images' => "*img",
// TinyMCE types
'file' => "",
'media' => "swf flv avi mpg mpeg qt mov wmv asf rm",
'image' => "*img",
),
'filenameChangeChars' => array(/*
' ' => "_",
':' => "."
*/),
'dirnameChangeChars' => array(/*
' ' => "_",
':' => "."
*/),
'mime_magic' => "",
'maxImageWidth' => 0,
'maxImageHeight' => 0,
'thumbWidth' => 100,
'thumbHeight' => 100,
'thumbsDir' => ".thumbs",
'jpegQuality' => 90,
'cookieDomain' => "",
'cookiePath' => "",
'cookiePrefix' => 'KCFINDER_',
// THE FOLLOWING SETTINGS CANNOT BE OVERRIDED WITH SESSION CONFIGURATION
'_check4htaccess' => true,
//'_tinyMCEPath' => "/tiny_mce",
'_sessionVar' => &$_SESSION['KCFINDER'],
//'_sessionLifetime' => 30,
//'_sessionDir' => "/full/directory/path",
//'_sessionDomain' => ".mysite.com",
//'_sessionPath' => "/my/path",
);
?>
I got the same problem and I found that the .htaccess file in the kcfinder dir was precisely shut the php engine off. I removed the .htaccess file and then it worked. I do not understand why this shutting off was necessary. Security??
Well, I still get a problem to point at the right directory, but now I see something !!
One comment here about the CONFIG array here above. The attribute disabled have to be put to false, and it is often suggested to be done through a SESSION variable (put $_SESSION['KCFINDER'] = array();
$_SESSION['KCFINDER']['disabled'] = false; after the session_start() ), and not directly in the config file.