File & Folder Cake Php - cakephp

i have another problem with CakePhp concerning the File & Folder utility.
I am getting this error:
Fatal Error Error: Call to undefined method
RegistriesController::clearStatCache() File:
/var/www/crm0.convieneonline.it/lib/Cake/Utility/File.php Line: 408
what i did it's simple:
I inserted App::uses('Folder', 'Utility'); and App::uses('File', 'Utility'); in the Controller
And then i developed the following code:
$src = 'http://www.website.it/be/preventivatore/img/brands/'. $registry['Registry']['id'] .'.png';
if ($check = File::exists($src)) {
$check = true;
} else {
$check = false;
}
Can you please help me with the error? Thank you

Related

Slim3 telling me that callable route can't be found

I'm new to slim. I ran into an error telling me the callable controller does not exist. However my other controller (HomeController) does work. I'm sure I am doing something stupid but I can't figure out what.
Error Message
Type: RuntimeException
Message: Callable JoeyD473\RPG_Tools\controllers\traveller\UniverseCreationControllercreateUniverse does not exist
File: C:\Users\Joey\Web\JRD_Traveller_Tools\vendor\slim\slim\Slim\CallableResolver.php
Line: 90
Project Structure
\public
index.php
.htaccess
\src
\app
\controllers
BaseController.php
HomeController.php
\traveller
UniverseCreationController.php
\model
\views
config.php
container.php
routes.php
composer.json
"autoload": {
"psr-4": {
"JoeyD473\\RPG_Tools\\models\\": "src/app/model",
"JoeyD473\\RPG_Tools\\controllers\\": "src/app/controllers"
}
},
container.php
$container = $app->getContainer();
$container['view'] = new \Slim\Views\PhpRenderer(APP.'views/');
$container['db'] = function($c){
$settings = $c->get('settings')['db'];
$db = new \Aura\Sql\ExtendedPdo($settings['vendor'].':host='.$settings['host'].';dbname='.$settings['db_name'],$settings['username'],$settings['password']);
return $db;
};
$container['HomeController'] = function($container){
return new \JoeyD473\RPG_Tools\controllers\HomeController;
};
$container['UniverseCreationController'] = function($container){
return new \JoeyD473\RPG_Tools\controllers\traveller\UniverseCreationController;
};
routes.php
$app->get('/',JoeyD473\RPG_Tools\controllers\HomeController::class.':home');
$app->get('/api',JoeyD473\RPG_Tools\controllers\HomeController::class.':sw_api');
$app->get('/temp',JoeyD473\RPG_Tools\controllers\HomeController::class.':temp');
$app->get('/universe/create_new_universe',JoeyD473\RPG_Tools\controllers\traveller\UniverseCreationController::class.'createUniverse');
UniverseCreationController.php
namespace JoeyD473\RPG_Tools\controllers\traveller;
use JoeyD473\RPG_Tools\controllers\BaseController;
use JoeyD473\RPG_Tools\models\traveller\universe\Universes;
class UniverseCreationController extends BaseController
{
public function createUniverse($request,$response)
{
return $this->container->view->render($response,'travellers/universe/create_universe.phtml');
}
public function generateUniverse($request,$response)
{
$universe = new Universes($this->db);
return 'generateSector';
}
}
You've missed a colon (:) in your last route
\traveller\UniverseCreationController::class.'createUniverse');
^^^
You could also use the array syntax for this.
[YourClass::class, 'yourMethod']

Unable to download a file in laravel 5?

trying to download a csv file, during downloading the page is opened in new tab and showing the error as FileNotFoundException in File.php line 37: The file "72" does not exist. Ccan anyone please help me?
Laravel controller:
public function fileExport(Request $request, Response $response)
{
$employee = Employee::all()->toArray();
$fileName = 'Employee.csv';
$headers = array(header('Content-Type: application/csv', 'Content-Disposition: attachment; filename="'.$fileName.'"'));
$f = fopen('php://output', 'w');
foreach ($employee as $line)
{
$data = fputcsv($f, $line);
}
return response()->download($data,$headers);
}
Laravel router:
Route::get('fileExport', 'EmployeeController#fileExport');
Angularjs controller:
$scope.exportFile = function(){
$window.open(rootUrl+'fileExport');
}
<button class="btn btn-default btn-xs btn-detail" ng-click="exportFile()">Export</button>
Exception shows that file is not found and might not be generated.
First of all check file exists and paths are correct?
And if file does not exists then make it simple as below and create the file correctly on desired path and then you can move on:
$f = fopen($fileName, 'w');

CakePHP Custom Helper not working

MY first time creating a custom helper. I'm getting an error in my Controller Code that I"m calling a a method on a non existed object (helper). Yet i believe my helper "BM" is being loaded successfully because i'm not getting any errors on loading helpers.
Error: Call to a member function mcpGetActiveMerchantID() on a non-object
File: C:\wamp\www\bm\app\Controller\MCPController.php
Line: 412
I have placed BMHelper.php into my View\Helper\ directory.
<?php
class BMHelper extends AppHelper{
public function mcpGetActiveMerchant(){
return $this->Session->read('Auth.ActiveMerchant');
}
public function mcpGetActiveMerchantID() {
$activemerchant = $this->Session->read('Auth.ActiveMerchant');
foreach($activemerchant as $key => $value) {
$merchant_id = $key;
}
return $merchant_id;
}
}
?>
Then in my Controller I have this:
<?php
class MCPController extends AppController {
public $helpers = array('Html', 'Form', 'Session','BM','Js' => array('Jquery'));
public function walkinadd() {
$test = $this->BM->mcpGetActiveMerchantID(); //Line 412
}
}
?>
HEre is the error again (same as the error I pasted at the top)
Error: Call to a member function mcpGetActiveMerchantID() on a non-object
File: C:\wamp\www\bm\app\Controller\MCPController.php
Line: 412
Anyone know what is wrong?
Helpers are to be used in Views not Controllers, though you could do:
public function walkinadd() {
$view = new View($this);
$bm = $view->loadHelper('BM');
$test = $bm->mcpGetActiveMerchantID();
}

Joomla Component error - Fatal error

I have installed a ticket module on my Joomla website, I get the following error when a ticket is submitted
Fatal error: Call to undefined method JRegistry::getValue() in /home/xboxfifa/public_html/HD/components/com_jtaghelpdesk/controller.php on line 300
function mailme($formData){
$version = new JVersion();
$body = "A new inquiry has been recieved: <br /> Name : ". $formData['inqName']. "<br /> Email ID : ". $formData['inqEmail']. "<br/> Message : ".$formData['inqMessage'] ;
$mailer =& JFactory::getMailer();
$config =& JFactory::getConfig();
if($version->RELEASE==3.0)
{
$recipient = array(
$config->get('mailfrom'),
$config->get('fromname'));
}else
{
$recipient = array(
$config->getValue( 'config.mailfrom' ),
$config->getValue( 'config.fromname' ));
}
$mailer->addRecipient($recipient);
$subject=$formData['inqSubject'];
$mailer->setSubject($subject);
$mailer->isHTML(true);
$mailer->Encoding = 'base64';
$mailer->setBody($body);
$send =& $mailer->Send();
if ( $send !==true )
{ $msg = 'Reply not Sent!';
} else
{
$msg = 'Reply Sent Successfully!';
}
}
If anyone could help me I would appreciate it.
JRegistry::getvalue() was removed in Joomla 3.x so make sure you use JRegistry::get() instead.
As for your version compare, you should use this:
if (version_compare(JVERSION, '3.0.0', 'ge')){
// code here
}
Editing this answer because I realized the issue is something else. The problem here is the $version check you're doing looks for the release to be EXACTLY == 3.0. When really you want that get() method to be used for 3.0 and up. You should change your if-statement to:
if($version->RELEASE >= 3.0)
{ ... }
Because you're using 3.2.1 this will use the proper method gets for that version.

upload a file in cakePHP

Hi I want to upload a file using cake php, i got the file and i am using
move_uploaded_file() to move to a specific location but it is not moving my simple logic is
shown below
if (move_uploaded_file($this->data['Add']['upload']['tmp_name'], APP . 'views' . DS .
'static' . DS.'uploads'.DS.'Rajaram'.DS )) {
LogUtil::$logger->debug('KMP File upload Url :
'.var_export($this->data, true));
}
Thanks in Advance.
File uploading is something CakePHP doesn’t do out of the box, which is one of the only thing that annoys me about the framework.
I tackled this by adding file handling to a model using callback methods. I upload the actual file with beforeSave(), and delete the file from the file system with beforeDelete(). A sample model looks like this:
<?php
App::uses('File', 'Utility');
class Image extends AppModel {
public $name = 'Image';
public function beforeSave($options = array()) {
$fieldName = 'filename';
$field = $this->data[$this->alias][$fieldName];
if (!is_array($field)) {
$this->validationErrors[$fieldName][] = 'No file detected';
return false;
}
switch ($field['error']) {
case UPLOAD_ERR_OK:
$newFilename = time() . '.jpg';
$uploadDir = WWW_ROOT . 'files/';
$source = $field['tmp_name'];
$destination = $uploadDir . $newFilename;
if (move_uploaded_file($source, $destination)) {
$this->data[$this->alias][$fieldName] = $newFilename;
return true;
}
else {
$this->validationErrors[$fieldName][] = 'No file detected';
return false;
}
break;
default:
$this->validationErrors[$fieldName][] = 'No file detected';
return false;
break;
}
}
public function beforeDelete($cascade = true) {
$image = $this->findById($this->id);
$file = new File(WWW_ROOT . 'files/' . $image['Image']['filename']);
return $file->delete();
}
}
Obviously this isn’t a perfect implementation, so feel free to take from it, learn from it, adapt it.
This was written off-the-cuff for a project recently where there’s only one model that has images attached, but on a larger project I’d more than likely wrap it up into a nice model behavior.
In your model you can use the afterSave callback method to handle your file upload:-
public function afterSave($created) {
if (isset($_FILES['data']['name'][$this->alias]['filename'])) {
$filename = $_FILES['data']['name'][$this->alias]['filename'];
$fileInfo = pathinfo($filename);
$fileExt = isset($fileInfo['extension']) ? $fileInfo['extension'] : '';
$filename = $fileInfo['filename'];
$newFilename = "$filename.$fileExt";
$dir = WWW_ROOT . 'files' . DS . 'uploads';
$target = $dir . DS . $newFilename;
move_uploaded_file($source, $target);
}
}
You can use $newFilename to change the filename to something appropriate if need be (I tend to check if a file with the same name already exists and rename the new one to avoid overwriting it.

Resources