I have a Cache Configuration in my bootstrap.php file as
Cache::config('long', array(
'engine' => 'File',
'duration' => '+1 week',
'probability'=> 100,
'mask' => 0666,
'path' => CACHE . 'long' . DS,
));
and i am trying to clear cache when a setting is edited. Below is my admin_edit function
public function admin_edit($id = null) {
if (!$this->Setting->exists($id)) {
throw new NotFoundException(__('Invalid setting'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Setting->save($this->request->data)) {
$this->Session->setFlash(__('The setting has been saved'));
$this->redirect(array('action'=> 'index'));
Cache::clear(false,'long');
Cache::gc();
}else {
$this->Session->setFlash(__('The setting could not be saved. Please, try again.'));
}
}else {
$options = array('conditions' => array('Setting.' . $this->Setting->primaryKey=> $id));
$this->request->data = $this->Setting->find('first', $options);
}
}
However, Cache::clear(false,'long') does not work and it does not clear the Cache. Not sure what is going wrong. Stuck for a few days now!
Please use below function in any controller and run that function where you want it will be clear all cache.
/**
* function to clear all cache data
* by default accessible only for admin
*
* #access Public
* #return void
*/
public function clear_cache() {
Cache::clear();
clearCache();
$files = array();
$files = array_merge($files, glob(CACHE . '*')); // remove cached css
$files = array_merge($files, glob(CACHE . 'css' . DS . '*')); // remove cached css
$files = array_merge($files, glob(CACHE . 'js' . DS . '*')); // remove cached js
$files = array_merge($files, glob(CACHE . 'models' . DS . '*')); // remove cached models
$files = array_merge($files, glob(CACHE . 'persistent' . DS . '*')); // remove cached persistent
foreach ($files as $f) {
if (is_file($f)) {
unlink($f);
}
}
if(function_exists('apc_clear_cache')):
apc_clear_cache();
apc_clear_cache('user');
endif;
$this->set(compact('files'));
$this->layout = 'ajax';
}
Once let me know if not working for you :)
Thanks
Related
I have a fileupload function that is supposed to save files in different locations depending on which form was submitted.
First of all, I have in the same view the two ActiveForms to submit a file with (different variable in the field):
<?php $form = ActiveForm::begin(['action' => \yii\helpers\Url::to(['upload', 'id' => $model->id, 'mod' => 1]), 'options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'file1')->fileInput(['style' => ''])->label(false) ?>
<?= Html::submitButton('Upload', ['class' => 'btn-success']) ?>
<?php ActiveForm::end(); ?>
and
<?php $form = ActiveForm::begin(['action' => \yii\helpers\Url::to(['upload', 'id' => $model->id, 'mod' => 0]), 'options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'file2')->fileInput(['style' => ''])->label(false) ?>
<?= Html::submitButton('Upload', ['class' => 'btn-success']) ?>
<?php ActiveForm::end() ?>
To my understanding, this would then put those files in the variable in the corresponding model:
* #var file1
* #var file2
*/
class X extends \yii\db\ActiveRecord
{
/**
* #var UploadedFile file1 attribute
*/
public $file1;
/**
* #var UploadedFile file2 attribute
*/
public $file2;
with the 'action' pointing to upload, it should call the xController action:
public function actionUpload($id, $mod) {
$model = new X();
if($mod == 1){
$model->file1 = UploadedFile::getInstance($model, 'file1');
if($model->file1){
$path = 'uploads/docs1/'. $id .'/';
if(!is_dir($path)){
FileHelper::createDirectory($path);
$model->pwFile->saveAs($path . $model->file1->baseName . '.' . $model->file1->extension);
}else{
$files = \yii\helpers\FileHelper::findFiles($path);
if(empty($files) || is_null($files)){
$model->file1->saveAs($path . $model->file1->baseName . '.' . $model->file1->extension);
}else{
foreach($files as $file){
unlink($file);
}
$model->file1->saveAs($path . $model->file1->baseName . '.' . $model->file1->extension);
}
}
}
}else{
$model->file2 = UploadedFile::getInstance($model, 'file2');
if($model->file2){
$path = 'uploads/docs2/'. $id .'/';
if(!is_dir($path)){
FileHelper::createDirectory($path);
}
$model->file->saveAs($path . $model->file2->baseName . '.' . $model->file2->extension);
}
}
return $this->render('view', ['model' => $this->findModel($id)]);
}
This doesn't seem to work. The upload for file1 works alright, the file gets uploaded to the server and that's it. If I use the second form however, it loads the correct url but nothing gets saved on the server. What am I doing wrong?
You have a massive amount of duplicated code, which makes it harder to debug. I hacked away at it and I think the below is optimal. I've also changed a few things, which should be pretty easy for you to spot.
public function actionUpload($id, $mod) {
$model = new X();
if($mod == 1) {
$file = UploadedFile::getInstance($model, 'file1');
$path = 'uploads/docs1/'. $id .'/';
} else {
$file = UploadedFile::getInstance($model, 'file2');
$path = 'uploads/docs2/'. $id .'/';
}
if ($file->name != '') {
if(is_dir($path)){
$files = \yii\helpers\FileHelper::findFiles($path);
foreach($files as $file){
unlink($file);
}
}
FileHelper::createDirectory($path);
$file->saveAs($path . $file->name);
}
return $this->render('view', ['model' => $this->findModel($id)]);
}
I think this will work now, if not, give me a shout and I'll have another look.
I need to UPDATE record with out changing or deleting filename(i mean file) , OR reinsert file again so how i can do this?
here is my actionCreate, How i can write update?
public function actionCreate() {
$model = new Page;
if (isset($_POST['Page'])) {
$model->attributes = $_POST['Page'];
$model->filename = CUploadedFile::getInstance($model, 'filename');
if ($model->save()) {
if ($model->filename !== null) {
$dest = Yii::getPathOfAlias('application.uploads');
$model->filename->saveAs($dest . '/' . $model->filename->name);
$model->save();
}
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render('create', array(
'model' => $model,
));
}
So Please can anyone find a solution
If you don't need to change or delete filename in edit/update then ignore filename (and upload part) assuming the file is alrready uploaded and you don't wish to change/delete it.
public function actionUpdate($id) {
$model = $this->loadModel($id);
$file = $model->filename;
if (isset($_POST['Page'])) {
$model->attributes = $_POST['Page'];
$model->filename = $file;
if ($model->save()) {
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render('create', array(
'model' => $model,
));
}
Try this, it's working for me -
create a upload folder outside protected directory and also give read/write permission.
actionCreate function -
public function actionCreate() {
$model = new Page();
if(isset($_POST['Page']) && !empty($_POST['Page'])){
$model->attributes = $_POST['Page'];
$rand = rand(1, 999);
$myfile = CUploadedFile::getInstance($model, 'filename');
$fileName = "{$rand}-{$myfile}"; //Generate unique file name
if(!empty( $fileName)){
$model->filename = $fileName;
$target_dir = realpath( Yii::getPathOfAlias('application') . '/../upload');
$target_file = $target_dir . '/' . $fileName;
if(!$myfile->saveAs($target_file)){
$model->addError('filename', 'File saving error');
}
}
if(!$model->hasErrors() && $model->validate() && $model->save(false)) {
$this->redirect(array('view', 'id'=>$model->id, 'msg'=>'success'));
}
}
}
actionUpdate function -
public function actionUpdate() {
$model = Page::model()->findbyPK($id);
$model->scenario = 'update';
if(isset($_POST['Page']) && !empty($_POST['Page'])){
$model->attributes = $_POST['Page'];
if(empty($_POST['Page']['filename'])) { $file_name = $model->filename; } //store existing file name into a variable if your uploading a file on update
$rand = rand(1, 999);
$myfile = CUploadedFile::getInstance($model, 'filename');
$fileName = "{$rand}-{$myfile}"; //Generate unique file name
if($model->validate()) {
if(!empty($fileName)) {
$model->filename = $fileName;
$target_dir = realpath( Yii::getPathOfAlias( 'application' ) . '/../upload');
$target_file = $target_dir . '/' . $fileName;
if(!$myfile->saveAs($target_file)){
$model->addError('filename', 'File saving error');
}
}
else {
$model->filename = $file_name; //allow existing file name
}
if( !$model->hasErrors() && $model->save(false) ) {
$this->redirect(array('view', 'id'=>$model->id, 'msg'=>'update'));
}
}
}
In model rules array define this -
array('filename', 'file', 'types'=>'jpg, jpeg, bmp, gif, png', 'allowEmpty'=>true, 'on'=>'insert, update')
I would like to get object by id from database inside Controller then use its name to create directory with that name.
public function album($id = null) {
$dirname = $this->Dog->query('SELECT * from dogs WHERE id='$id'');
$dir = new Folder(WWW_ROOT . 'albums' . DS . 'files' . DS . $dirname->name , true, 0755);
$this->layout = 'edit';
if (!$id) {
throw new NotFoundException(__('Zły post'));
}
$Dog = $this->Dog->findById($id);
if (!$Dog) {
throw new NotFoundException(__('Zły post'));
}
if ($this->request->is('post') || $this->request->is('put')) {
$this->Dog->id = $id;
if ($this->Dog->save($this->request->data)) {
$this->Session->setFlash(__('Edycja zakończona sukcesem.'));
$this->redirect(array('action' => 'index_psy'));
} else {
$this->Session->setFlash(__('Spróbuj ponownie.'));
}
}
}
I have tried a few other solutions but none of them seems to work.
Thanks for help
public function album($id) {
//if id doesn't exist Exit
if (!$this->Dog->exists($id)) {
throw new NotFoundException(__('Invalid album'));
}
$dog = $this->Dog->findById($id);
//create a folder with directory name
$path = 'files' . DS . $dog['Dog']['name'];
$folder = new Folder($path , true, 0755);
$this->layout = 'edit';
}
}
look at here just to get an idea
<?php
/** Check if environment is development and display errors **/
function setReporting() {
if (DEVELOPMENT_ENVIRONMENT == true) {
error_reporting(E_ALL);
ini_set('display_errors','On');
} else {
error_reporting(E_ALL);
ini_set('display_errors','Off');
ini_set('log_errors', 'On');
ini_set('error_log', ROOT.DS.'tmp'.DS.'logs'.DS.'error.log');
}
}
/** Check for Magic Quotes and remove them **/
function stripSlashesDeep($value) {
$value = is_array($value) ? array_map('stripSlashesDeep', $value) : stripslashes($value);
return $value;
}
function removeMagicQuotes() {
if ( get_magic_quotes_gpc() ) {
$_GET = stripSlashesDeep($_GET );
$_POST = stripSlashesDeep($_POST );
$_COOKIE = stripSlashesDeep($_COOKIE);
}
}
/** Check register globals and remove them **/
/*function unregisterGlobals() {
if (ini_get('register_globals')) {
$array = array('_SESSION', '_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
foreach ($array as $value) {
foreach ($GLOBALS[$value] as $key => $var) {
if ($var === $GLOBALS[$key]) {
unset($GLOBALS[$key]);
}
}
}
}
}*/
/** Routing **/
function routeURL($url) {
global $routing;
foreach ( $routing as $pattern => $result ) {
if ( preg_match( $pattern, $url ) ) {
return preg_replace( $pattern, $result, $url );
}
}
return ($url);
}
/** Main Call Function **/
function callHook() {
global $url;
global $default;
global $sent;
$queryString = array();
if (!isset($url)) {
$controller = $default['controller'];
$action = $default['action'];
} else {
$url = routeURL($url);
$urlArray = array();
$urlArray = explode("/",$url);
$controller = $urlArray[0];
array_shift($urlArray);
if (isset($urlArray[0])) {
$action = $urlArray[0];
array_shift($urlArray);
} else {
$action = 'view'; // Default Action
}
$queryString = $urlArray;
if(isset($queryString[0]))
$sent=$queryString[0];
//echo $sent;
}
$controllerName = $controller;
$controller = ucwords($controller);
$model = rtrim($controller, 's');
$controller .= 'Controller';
//echo($model);
//echo($controllerName);
//echo($action);
//echo phpinfo();
**$dispatch = new $controller($model,$controllerName,$action);**
if ((int)method_exists($controller, $action)) {
//call_user_func_array(array($dispatch,"beforeAction"),$queryString);
call_user_func_array(array($dispatch,$action),$queryString);
//call_user_func_array(array($dispatch,"afterAction"),$queryString);
} else {
/* Error Generation Code Here */
}
}
/** Autoload any classes that are required **/
function __autoload($className) {
if (file_exists(ROOT . DS . 'library' . DS . strtolower($className) . '.class.php')) {
include_once(ROOT . DS . 'library' . DS . strtolower($className) . '.class.php');
} else if (file_exists(ROOT . DS . 'application' . DS . 'controller' . DS . strtolower($className) . '.php')) {
include_once(ROOT . DS . 'application' . DS . 'controller' . DS . strtolower($className) . '.php');
} else if (file_exists(ROOT . DS . 'application' . DS . 'model' . DS . strtolower($className) . '.php')) {
include_once(ROOT . DS . 'application' . DS . 'model' . DS . strtolower($className) . '.php');
} else {
/* Error Generation Code Here */
}
}
setReporting();
removeMagicQuotes();
//unregisterGlobals();
callHook();
*****//
When I uploaded this file on server it is showing the error
Fatal error: Cannot instantiate non-existent class:updatescontroller on line 97
pointing to the line
$dispatch =new $controller($model,$controllerName,$action);
Please help me determine what is going wrong.
Also the same server is also not allowing me to run the unregisterGlobals() function and showing “too many errors for undefined index”.
The complete project is running very well on my localhost server.
From the error message I guess you are missing an included file that declares the updatescontroller class. Maybe you need to upload the entire project?
It also seems that your local PHP configuration doesn't match the remote configuration. Try matching your local php.ini with the remote server to make local testing more realistic.
If at all possible try to match the PHP version on your local server with the remote server. There can be subtle differences between versions.
<?php
class UploadsController extends AppController {
var $name = 'Uploads';
var $components = array('Auth');
var $uses = array('Upload');
function beforeFilter() {
$this->Auth->allow('*');
}
function upload($event) {
App::import('Vendor', 'UploadedFiles', array('file' => 'UploadedFiles.php'));
$user = $this->Auth->user('id');
$this->set('user', $user);
if(!$this->Auth->user()) { $this->Session->setFlash(__('Please login.', true)); }
echo $user;
echo $event;
$vardir = date('d-m-Y');
$dir = 'img/gallery/'.$vardir.'/';
$thmbdir = 'img/gallery/'.$vardir.'/thumbnails/';
if(!is_dir($dir)) {
mkdir($dir, 0777);
mkdir($thmbdir, 0777);
}
$galleryPath = $dir;
$absGalleryPath = realpath($galleryPath) . '/';
$absThumbnailsPath = realpath($galleryPath . 'thumbnails/') . '/';
//Iterate through uploaded data and save the original file, thumbnail, and description.
while(($file = UploadedFiles::fetchNext()) !== null) {
$fileName = $file->getSourceFile()->getSafeFileName($absGalleryPath);
$file->getSourceFile()->save($absGalleryPath . '/' . $fileName);
$thumbFileName = $file->getThumbnail(1)->getSafeFileName($absThumbnailsPath);
$file->getThumbnail(1)->save($absThumbnailsPath . '/' . $thumbFileName);
$this->Upload->create();
$this->Upload->set(array(
'name' => $absGalleryPath . $fileName,
'width' => $file->getSourceFile()->getWidth(),
'height' => $file->getSourceFile()->getHeight(),
'description' => $file->getDescription(),
'event_id' => $event,
'user_id' => $user
));
$this->Upload->save();
}
}
}
Check the last part of the code where I try to save to the database. It doesn't save because $event and $users become empty. But when I echo them (line 17 and 18), they do appear on the page with correct values. It seems that they are being 'erased' in the while loop...
If I put some dummy data for 'event_id' => '123' and 'user_id' => '123', the scripts saves successfully. But if I use the variables, it doesn't save. What happens with my variables? Any idea?
Thanks
I'm guessing there's an error in your model. Try creating the array first, and printing it, and see if it creates the array correctly. Then make sure your model matches the array, ie. that the field types match the field types you input. '123' isn't the same as 123, since '123' is a string, and 123 is an integer.
i can think of a couple things i'd try though it's not to say you haven't already.
echo $user and $event the line just before and just after the while loop, to verify the problematic line.
set the variables to another temp variable:
$t_user = $user;
$t_event = $event;
then try using the $t_ variables in the loop.