We need to have the user upload an image as a part of sign up process.
Had tried accessing $_FILES['filename'] in the controller, which turns out to be undefined under slim.
Had seen about Slim's way of file uploading in a couple of articles, which are reported to be working, but I hit the wall.
The twig part works fine with Bootstrap File Input library
For the server part using File Upload library for Slim
Controller code (modifications to AccountController) looks like this
...
$storage = new \Upload\Storage\FileSystem('c:/xampp/htdocs/userfrosting/public/images/');
$file = new \Upload\File('imagefile', $storage);
$new_filename = 'test.jpg';
$file->setName($new_filename);
$file->addValidations(array(
// Ensure file is of type "image/jpg"
new \Upload\Validation\Mimetype('image/jpg'),
// Ensure file is no larger than 500K (use "B", "K", M", or "G")
new \Upload\Validation\Size('500K')
));
// Access data about the file that has been uploaded
$uploadfiledata = array(
'name' => $file->getNameWithExtension(),
'extension' => $file->getExtension(),
'mime' => $file->getMimetype(),
'size' => $file->getSize(),
'md5' => $file->getMd5(),
'dimensions' => $file->getDimensions()
);
error_log('$uploadfiledata' . print_r($uploadfiledata, true));
// Try to upload file
try {
// Success!
$file->upload();
} catch (\Exception $e) {
// Fail!
$errors = $file->getErrors();
}
...
This returns the following error,
Type: InvalidArgumentException
Message: Cannot find uploaded file identified by key: imagefile
File: C:\xampp\htdocs\userfrosting\userfrosting\vendor\codeguy\upload\src\Upload\File.php
Line: 139
The relevant twig chunk is
<input id="imagefile" type="file" name="imagefile" class="file" data-show-upload="false">
Has anyone been able to get file upload working as a part of any Userfrosting code?
Appreciate any help / pointers.
Thanks!
My guess is that you're using ufFormSubmit to submit your registration form, and it is not grabbing the file input. So, you will probably need to add some extra code on the client side to explicitly submit the file input along with the rest of the form. See this example using Dropzone and UF: https://gist.github.com/frostbitten/c1dce70023321158a2fd#file-upload-twig
By the way, you can use your browser to see what data is actually being sent in your POST request. For example, in Firefox you can use the Network Monitor.
Related
I was about to upload images to cloudinary - a Image CDN look cloudinary.com, It supports all languages and frameworks including Cakephp 3, but for cakephp 3 the steps we're not included in their site. Can anyone possibly say me steps for uploading images in ease?
As per their site, I'm providing procedures for uploading.
Base Documentation:
http://cloudinary.com/documentation/php_integration#getting_started_guide
Step 1:
Installation
{
"require": {
"cloudinary/cloudinary_php": "dev-master"
}
}
Add the above to your composer.json located in your project folder.
You can use composer to update it and fetch dependencies. Run the following in composer after navigating to your project folder.
php composer.phar update
Step 2:
Installation in Cake PHP.
Open your AppController and add the following in the initialize function
It apparently looks like the following:
public function initialize() {
parent::initialize();
$this->loadComponent('Flash');
\Cloudinary::config(array(
"cloud_name" => "sample",
"api_key" => "874837483274837",
"api_secret" => "a676b67565c6767a6767d6767f676fe1"
));
}
In the above you can find the cloudinary configuration, replace with your own credentials.
To fetch credentials, follow the link below and sign in,
https://cloudinary.com/users/login
Step 3:
Image Upload Procedure
<?php
echo $this->Form->create('upload_form', ['enctype' => 'multipart/form-data']);
echo $this->Form->input('upload', ['type' => 'file']);
echo $this->Form->button('Change Image', ['class' => 'btn btn-primary']);
echo $this->Form->end();
?>
Use the above code in your view file. (you can modify as you need)
In your controller, you can use in the following way,
if (!empty($this->request->data['upload']['name'])) {
$file = $this->request->data['upload']; //put the data into a var for easy use
$cloudOptions = array("width" => 1200, "height" => 630, "crop" => "crop");
$cloudinaryAPIReq = \Cloudinary\Uploader::upload($file["tmp_name"], $cloudOptions);
$imageFileName = $cloudinaryAPIReq['url'];
}
You can save the $imagefilename in your database, in here the complete url is saved and repopulated.
The CakeEmail help page states that the data option has been added as of 2.4, so you no long have to have a physical file to add an attachment to an email.
I've got the following code:
$Email->from(array($this->Session->read('Auth.User.email') => $this->Session->read('Auth.User.name')))
->to($this->request->data['email-to'])
->subject($this->request->data['email-subject'])
->attachments(array('attachement1.pdf', array('data' => $pdf)))
->send($this->request->data['email-message']);
But whenever I run that I get an Internal Error saying File Not Found: "".
I had a look at the source code (which I'm beginning to learn is often more useful than reading the documentation!): https://github.com/cakephp/cakephp/blob/master/lib/Cake/Network/Email/CakeEmail.php
Changing my code to:
$Email = new CakeEmail('default');
$Email->from(array($this->Session->read('Auth.User.email') => $this->Session->read('Auth.User.name')))
->to($this->request->data['email-to'])
->subject($this->request->data['email-subject'])
->attachments(array('attachement1.pdf' => array('data' => $pdf, 'mimetype' => 'application/pdf')))
->send($this->request->data['email-message']);
Notice on the attachments line, the array is assigned to the filename variable, rather than passed in as a parameter!
For completeness, if anyone else is reading this and is wondering how I am generating my PDF with CakePDF:
// Create PDF for attachment
$CakePdf = new CakePdf();
$CakePdf->template('claim', 'default');
//get the pdf string returned
$pdf = $CakePdf->output();
In my controller init method I have this
public function init()
{
//initialise the context switch action helper
$this->_helper->getHelper('contextSwitch')
->addContext('csv', array('suffix' => 'csv',
'headers' => array(
'Context-Type' => 'application/csv',
'Context-Disposition' => 'inline; filename="fooo.csv"',
'Pragma' => 'no-cache',
'Expires' => '0',
)))
->addActionContext('stockreport', 'csv')
->initContext();
}
In my stockreportAction I have disabled the layout and view render as follows.
public function stockreportAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
echo 'foobar';
}
I get a blank page when I call this file, I would like to generate this so in the example 'foobar' appears as a downloadable csv file. Could anyone advise
Zend_Magic is not implemented yet.
It is your responsibility to generate csv or any other output format.
in short: context switcher modify view script suffix, so view script for your context is stockreport.csv.phtml (if csv context is active, stockreport.phtml otherwise).
But in your example context switcher used in a wrong way. You don't switch context here.
You don't use viewscripts, you don't check if context active in your action.
Why your output is empty is a different question - probably at some point you clear content in Response object or there somewhere is a fatal error & error output disabled.
Update:
Also headers are Content-Type and Content-Disposition
SORRY FOR THE LONG POST. I JUST WANT TO BE CLEAR!! :cheese:
I am using the stated template library in my ci setup and everything works great except for when i use Mr. Haughin's facebook_connect!
facebook_connect works beautifully if I call anything in a separate view file in my controller like so:
function facebookconnect()
{
$this->load->library('facebook_connect');
$data = array(
'user' => $this->facebook_connect->user,
'user_id' => $this->facebook_connect->user_id
);
$this->template->write_view('content','fbtest',$data);
$this->template->render();
}
this works no problem calling it from it's own page and everyone is happy. But when I try to put my facebook interactivity into a div on my homepage with this code:
function index()
{
$this->load->library('facebook_connect');
$data = array(
'user' => $this->facebook_connect->user,
'user_id' => $this->facebook_connect->user_id
);
$this->template->write_view('content','indexpage',$data);
$this->template->render();
}
My homepage loads fine but I have this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: user_id
Filename: views/indexpage.php
Line Number: 32
This i think is strange as it works with code above. My variables are passed so I don't get it.
What I want to achieve is that on the homepage a list of facebook friends should appear when page loads. Is there anything i should know about this library? I went through the userguide looking for to see if i could write a separate view to the div that should carry the facebook connect results. Here is my view code just incase it will help:
php if ( !$user_id ):
php else:
" />
Hi php$user['first_name']!
(Logout)
php endif;
Here's some comments!
[removed]
FB.init("$this->config->item('facebook_api_key')", "/xd_receiver.htm");
[removed]
The problem seems to be in this line:
user_id' => $this->facebook_connect->user_id
because facebook's PHP client doesn't have any user_id property. The user id is available from:
user_id' => $this->facebook_connect->user
So try removing the line:
'user_id' => $this->facebook_connect->user_id
I can't seem to upload an image using Zend_Service_Nirvanix. Is it even possible?
I have a feeling that my problem has something to do with not being able to figure out how to set the UploadHost on the Transfer Service.
Any help is greatly appreciated! My deadline is July 16th!
Here is my code:
$nirvanix = new Zend_Service_Nirvanix(array('appKey' => $key,
'username' => $user,
'password' => pass));
$NSImfs = $nirvanix->getService('IMFS');
$options = array('sizeBytes' => filesize($source));
$storageNode = $NSImfs->getStorageNode($options);
$NSTransfer = $nirvanix->getService('Transfer');
$options = array('uploadToken' => $storageNode->getStorageNode->UploadToken,
'path' => $original,
'fileData' => file_get_contents($source));
$result = $NSTransfer->uploadFile($options);
Here is the error I keep getting:
Zend_Service_Nirvanix_Exception: XML
could not be parsed from response:
Server Error in '/' Application. The
resource cannot be found. Description:
HTTP 404. The resource you are looking
for (or one of its dependencies) could
have been removed, had its name
changed, or is temporarily
unavailable. Please review the
following URL and make sure that it is
spelled correctly.
Requested URL:
/ws/Transfer/UploadFile.ashx
in
/Applications/MAMP/bin/php5/lib/php/Zend/Service/Nirvanix/Response.php
on line 119
You're getting a 404?
Have you checked for an updated version of that library?
Try going into the libray and changing UploadFile.ashx to UploadFile.aspx. I don't think ashx is not a standard extension.
Maybe that will fix it.
There's a commercial upload tool from Aurigma that has support for file and image upload to Nirvanix. Here's the link (see Uploading to Nirvanix section there) to the help topic to check.
To do a local upload (rather than a web upload via the browser) you just have to call the putContents method passing the files data.
Example:
$nirvanix = new Zend_Service_Nirvanix(array('appKey' => $key,
'username' => $user,
'password' => pass));
$NSImfs = $nirvanix->getService('IMFS');
$response = $NSImfs->putContents($destination_file_and_path,
file_get_contents($source_file));
if($response->ResponseCode != 0)
{
echo 'Fail!';
}
You would only call GetStorageNode if you want to generate a token to pass a browser the upload token.