How to set path to file in Laravel? - laravel-5

I have located file in root directory of Laravel.
From controller I try to get this file like as:
$shipments = json_decode(file_get_contents("Statistics.json"), true);
But I get error.
What correct path to specify for getting file?

https://laravel.com/docs/5.0/helpers#paths
I guess you need base_path
file_get_contents(base_path().'/Statistics.json');

Related

Laravel - How to declare a file path constent in .env file?

In one of my Laravel based application, I want to include a JSON key file that is currently located in public/key/store.json
Now I want to write a constant in Laravel .env file so that I can access that file anytime I want. So, I write the following line of code:
KEY_FILE='/public/key/store.json'
But it show file path does not exist.
Can anyone tell me what's wrong in my declaration?
you should push your path in your app/config.php not in your .env

Laravel 5.5 Storage::url() doesn't return the desired url with S3

I'm using S3 for storage in my laravel 5.5 project
Everything is good and the images is stored in my bucket under my desired folder
using :
use Illuminate\Support\Facades\Storage;
$path = Storage::put($directory, $file, $permission);
$fullPath = config('filesystems.disks.s3.driver').
'.'.
config('filesystems.disks.s3.region').
'.amazonaws.com/'.
config('filesystems.disks.s3.bucket').
'/'.
$path;
the full path then will be like :
https://s3.us-east-2.amazonaws.com/iva-files/brands/logos/qweisiEC7H2SOV9ozjuOOBRxw399cTm2imnbJEXj.jpeg
I searched for better way to get my full url so I found:
Storage::url($path);
But it's returning path like :
https://ivasystem.signin.aws.amazon.com/console/brands/logos
What's wrong with my sinario ?
Try:
Storage::disk('s3')->url($filename)
As mentioned in the Official Laravel Documents
The path to the file will be returned by the putFile method so you can store the path, including the generated file name, in your database.
So you should always store file path & file name in database columns so that later on you can concatenate path & name to get fill url to the file.
In my project I am already doing this.

How to read a .docx file via PHP Word sample code in Codeigniter

Am trying to read a .docx file which is inside my project folder via PHP Word library. I have included autoloader like this :
include_once 'vendor/autoload.php'; in my controller.
CODE FOR FUNCTION IN CONTROLLER:
function test_phpword()
{
$a=base_url();
$path=$a."".'123.docx';
$source =$path;
echo date('H:i:s'), " Reading contents from `{$source}`";
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);
echo write($phpWord, basename(__FILE__, '.php'), $writers);
}
BUT GETTING ERROR LIKE BELOW:
06:18:42 Reading contents from http://localhost/myproject/123.docx
Fatal error: Uncaught Exception: Cannot find archive file. in /opt/lampp/htdocs/myproject/vendor/phpoffice/common/src/Common/XMLReader.php:51
Try this
Change
$path=$a."".'123.docx';
to
$path='123.docx';
And put 123.docx beside your php script file. make sure the two files are in the same place. Run your php script again.
If this helps and works fine you can check the file path and make proper change to your php program.
While loading the path, you have to give relative path to the doc file as shown below,
$random_name = 123.docx;
$contents = \PhpOffice\PhpWord\IOFactory::load('../uploads/'.$random_name);
I don't know whats your $base_url, but it will not work if it is absolute path like http://path/to/doc/file.
I have worked on it and tested. Hope it helps.

How to delete a file using delete_files function of File Helper

I would like to delete a file in codeigniter.
I want to use delete_files function, but it returns false !
$this->load->helper("file");
return delete_files(base_url().'application/cache/'.'blade-'.$cache_path);
Why it is returning false instead of deleting files.
Just use :
return unlink(APPPATH.'cache/'.'blade-'.$cache_path);
use
$this->load->helper('file');
return delete_files(FCPATH.'application/cache/'.'blade-'.$cache_path);
Codeigniter delete_files function will delete files inside a directory and it returns true or false.
Returns:TRUE on success, FALSE in case of an error
You getting False means you have error.
Possible reasons for errors
Folder does not exists.
Folder path is not valid
Does not have proper permission
The files must be writable or owned by the system in order to be
deleted.
For your case base_url().'application/cache/'.'blade-'.$cache_path is not a valid folder path.you added base_url before your folder name which will make the folder name like an URL. But It should be folder path.
Use FCPATH instead of base_url
you need to change the path to the folder delete like
$this->load->helper('file');
return delete_files(APPPATH.'cache/'.'blade-'.$cache_path);
you donĀ“t need to writte the url, coz the APPPATH places the pointer on the folder application, then you only specify in what subfolder you want to delete the file

Reading an external yml file in symfony1.4 with doctrine

I created one yml file
$loader = sfYaml::load('/pms/config/org.yml');
print_r($loader);
I have values in the yml file
but Its printing only the file name
/pms/config/org.yml
What could be the error.
That happens when the file does not exist. You are pointing to the root dir of you server.
Try something like this to point to the root dir of your project:
$loader = sfYaml::load( sfConfig::get('sf_root_dir') . '/config/org.yml' );
print_r($loader);

Resources