I uploaded xml file in my cakephp in img/files directory
app->webroot->files->Myfile.xml
and I want to call it from my controller like this
$file = "/files/Myfile.xml";
I am not sure is this the correct path from the controller ot not
of course controller is in app->controllers->my_controller
thanks
Use one of the CakePHP Core Configuration Constants.
WWW_ROOT for example:
$file = WWW_ROOT . "files/MyFiles.xml";
Related
I am just trying to check if an image exists or not, I can do it by using PHP. For example:-
$file = WWW_ROOT .'uploads' . DS . 'employee' . DS .'_'.check.jpg;
$file_exists = file_exists($file);
It's working fine for me. But I have tried also tried using elementExists like this:-
if($this->elementExists("../".$employees->front_image))
{
echo $this->Html->image("../".$employees->front_image); // image output fine without condition.
}
// Here $employees->front_image = uploads/employee/employeename.jpg
This check is not working. How can I do this in CakePHP?
CakePHP is written in PHP, so if you already have a simple solution like file_exists() use that. So you can write something like this:-
if (file_exists(WWW_ROOT . $employees->front_image)):
echo $this->Html->image('../' . $employees->front_image);
endif;
elementExists() is intended for checking that a View element exists, not if files exist in the webroot, so should not be used like you are trying. It does do a file_exists() check, but this scans all available View element paths only.
I think this works in Cake 3 (you should do this in afterFind method IMO):
// Create a new file with 0644 permissions
$file = new File('/path/to/file.php', true, 0644);
if ($file->exists()) {
//do something
}
$file->close();
Your way is checking whether view element exists or not.
This worked for me:
Q: How to check if image exists on remote url or not?
Explanation:
$rfile will take url
$check will open the file with read rights
If file exists then it will print "File exists" else "Doesn't exists".
code:
<?php
// Remote file url
$rFile = 'https://www.example.com/files/test.pdf';
// Open the file
$check = #fopen($rFile, 'r');
// Check if the file exists
if(!$check){
echo 'File does not exist';
}else{
echo 'File exists';
}
?>
I'm working with CakePHP 3(beta 2) version recently launched. I need to integrate Facebook Login using PHP SDKs and I'm not clear with importing vendor files in this version.
In CakePHP 2x, I had used
App::import('Vendor', 'Facebook', array('file' => 'Facebook' . DS . 'src'. DS. 'facebook.php'));
So I need to reproduce the same in CakePHP 3x(I'm not using composer).
Any reference for this?
Well you will have to load it yourself if composer is not an option. You can always use the very basic require method and create a new instance of the vendor class yourself. Reference: http://book.cakephp.org/3.0/en/core-libraries/app.html#loading-vendor-files
Use:
//The following line should do the same like App::import() in the older version of cakePHP
require_once(ROOT . 'vendor' . DS . 'Facebook' . DS . 'src' . DS . 'facebook.php');
$facebookApi = new facebook();
In cakephp3 , to add a new vendor library you can follow below steps :
Place library folder under your_project/vendor/
include library file using require_once(ROOT . DS . 'vendor' . DS . "my_library_folder" . DS . "my_library_base_class.php")
,this includes the library code file in our code.
Include class name in top of Controller like :
namespace App\Controller;
use MyLibraryBaseClass;
,
this imports the library code file in our namespace to be used.
create object of loaded class as
$my_obj= new MyLibraryBaseClass();
the answer provided by Ayman B. does not look like doing the job as expected in the question after i tried it myself , for the following reasons :
the vendor folder in cakephp3 is not located in src folder under APP namespace , it is moved to the ROOT folder , by doing you will not be able to load your Facebook class as expected , try it yourself and you will see the result ...
By loading a vendor file this is does not automatically load the class name itself , if your vendor lib does not follows the follwing rule as PSR-0 rule :
\VENDOR\PACKAGE\TEST.CLASS.PHP and inside the test.class.php there is not a class definition that must be called or imported in your script with a defined namespace keyword in the begining of this script as follows : namespace
then the code above will not work
To correct the answer you have to do some several steps as follows :
1 - Define in bootstrap.php a new cakephp constant like so :
define('VENDOR',ROOT . DS . 'vendor' .DS); as VENDOR constante is removed in cakephp 3.x you can define it yourself
2 - After that , you have to specify the vendor name , the package name and the class name in a vendor constante like :
define('_',; and then you can do $facebookApi = new \\();
this is will work for you as expected in the question
If you have issues try to get back to me , i will show you an example of use as described here ...
I also had the same problem with CakePHP 3.0.
Do the installation as instructed using Composer.
Then You have to properly load the plugin in your controller with the use statement. Like this:
use Ghunti\HighchartsPHP\Highchart;
If you're using the plugin in most of the pages, instead of loading in each Controller, you can add the same line in your bootstrap.php file, right below the other use statements.
That will solve the problem of using the plugin.
As of CakePhp 3.x, the recommended code standard is to use require_once without the brackets"()".
require_once(ROOT.'Folder'.DIRECTORY_SEPARATOR.'requiredfile.ph');
becomes
require_once ROOT.'Folder'.DIRECTORY_SEPARATOR.'requiredfile.ph';
https://book.cakephp.org/3.0/en/contributing/cakephp-coding-conventions.html
Hope that helps someone in the future.
I have a library in my project.When I want to use this with this code:
require('../Plugin/Utils/DateTimeUtil.php');
it says no such file exists. my cakephp 1s 2.3 what should I do?
The Routing in cakephp is different from pure php.I had something like this.At first you should find the path Plugin folder with this code
$pluginPath = App::path('Plugin');
Then It returns an array which contains the plugin folder's path in 0 index.So you should the returned value like blow:
require($pluginPath[0] . 'Utils' . DS . 'DateTimeUtil.php');
You can use slash instead of DS. DS is DIRECTORY_SEPRATOR.
i want to give file path out of the webroot, how to give it?
$uploads_dir = WWW_ROOT .'/uploads';
$tmp_name = $this->data["Image"]["filedata"]["tmp_name"];
$name = $this->data["Image"]["filedata"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
You need another constant. Cake has a few globally defined constants you can use, see the documentation here.
In you case you want to save images in a directory outside the webroot directory (not sure if that's wise, though), so you should use APP instead of WWW_ROOT as the former holds the path to the directory you're application is in.
The cakephp documentation is a bit poor according the File api and I would like to use it to copy one file to another directory and if the directory does not exist to create it.
Thanks!
You can do something like this:
$file = new File('your_file.ext');
if ($file->exists()) {
$dir = new Folder('folder_inside_webroot', true);
$file->copy($dir->path . DS . $file->name);
}
API about these classes:
Folder;
File;
I hope it helps.