How to get public directory? - laravel

I am a beginner here so pardon me for this question am using return File::put($path , $data); to create a file in public folder on Laravel. I used this piece of code from controller I need to know the value of $path how should it be.

Use public_path()
For reference:
// Path to the project's root folder
echo base_path();
// Path to the 'app' folder
echo app_path();
// Path to the 'public' folder
echo public_path();
// Path to the 'storage' folder
echo storage_path();
// Path to the 'storage/app' folder
echo storage_path('app');

You can use base_path() to get the base of your application - and then just add your public folder to that:
$path = base_path().'/public';
return File::put($path , $data)
Note: Be very careful about allowing people to upload files into your root of public_html. If they upload their own index.php file, they will take over your site.

I know this is a little late, but if someone else comes across this looking, you can now use public_path(); in Laravel 4, it has been added to the helper.php file in the support folder see here.

The best way to retrieve your public folder path from your Laravel config is the function:
$myPublicFolder = public_path();
$savePath = $mypublicPath."enter_path_to_save";
$path = $savePath."filename.ext";
return File::put($path , $data);
There is no need to have all the variables, but this is just for a demonstrative purpose.
Hope this helps,
GRnGC

I think what you are looking for is asset(''); You can use asset('storage'), after creating your symbolic link.

asset('public')
OR
url('public')

Related

Laravel File not found at path (again)

So, I've already dug through the questions here about this, but still can't seem to figure it out.
Do you know what I am doing wrong here?
$imageName = $thishire->color_image;
$path = public_path('/img/colorImages/' . $imageName);
$file = Storage::get(public_path('/img/colorImages/'. $imageName));
$file->move($path, public_path('/img/uploads'));
The 3rd line is throwing an error:
"File not found at path: Applications/MAMP/htdocs/GT Laravel/public/img/colorImages/l18Ow1uVBqhReGqW7lWjanp2vT5bOAKt8pylnmmb.jpg"
But that is the correct path, so I'm super confused.
You shouldn't do Storage::get(public_path('/img/colorImages/'. $imageName)).
Storage::get() is relative to the storage disk root.
If you use the default storage configuration, then your storage root path is storage_path('app'), said otherwise /path/to/your/project/storage/app.
This is what's happening:
Storage::get(public_path('/img/colorImages/'. $imageName));
// looking into /path/to/your/project/storage/app/path/to/your/project/public/img/colorImages
// which is obviously wrong
What you should do instead:
Store your public files in the storage/app/public directory
Use the php artisan storage:link command to create a symlink between public and storage/app/public. If you are not familiar with this concept, it'll create a small file into your public directory which is a shortcut to the storage/app/public directory. More about this here: https://laravel.com/docs/8.x/filesystem#the-public-disk
For instance, with a file named helloworld.jpg and located in storage/app/public/users/8/helloworld.jpg, you can get it by doing:
Storage::disk('public')->get('users/8/helloworld.jpg');
// or Storage::get('public/users/8/helloworld.jpg');
I know it can be confusing at first. This is a quick tip to "know where you are and what your are looking at". Use Storage::path():
dd(Storage::path(public_path('/img/colorImages/'. $imageName)));
// you'll see the problem
don't forget public meaning access storage but by created symlink
I think you should be store image in your path before getting it by using
Storage::put(public_path('/img/colorImages/'), $imageName);
and you can check documentation from here
I solved it.
$imageName = $thishire->color_image;
$old = public_path('/img/colorImages/'.$imageName);
$new = public_path('/img/uploads/'.$imageName);
rename($old, $new);

Is it ok to change public storage folder name from 'public/storage' to 'public/public'?

Laravel recommends here to create symlink from public/storage to storage/app/public. I'm looking for some inputs on whether renaming public/storage to public/public would cause any issues in other configurations or packages.
I am doing this because all my filenames in db are relative to storage/app folder. I store it that way since I can then uniformly retrieve any file using storage_path() helper. For example:
Protected file: storage/app/internal/secret.doc => filename in db = internal/secret.doc
Public file: storage/app/public/common.doc => filename in db = public/common.doc
Then to retrieve them:
// get filename 'internal/secret.doc' or 'public/common.doc'
$filename = DB::table('files')->find(1)->value('name'); // find file where id=1 and get only the name field
$file = storage_path('app/'.$filename);
This way I don't have to wonder whether I need to add public/ folder while retrieving a specific file. Also when I use asset() helper to generate urls to public files, again I can simply say:
$url = asset($filename)`;
Whereas, if I continue to use public/storage as the symlinked folder then I would have to do:
$temp_filename = str_replace('public/', '', $filename);
$url = asset('storage/'.$temp_filename);
Let me know if you have a different take or spot some issues with the renaming.
Once public/storage is sym linked to storage/app/public, you don't want your filepath in asset() to reference public. Public folder is already the root of your webserver, and asset() assumes public directory base. So your asset would be in asset("storage/$filename"). That actually pulls from storage/app/public/$filename.

How do I include file in codeIgniter application, which is outside application folder

I want to include this file
api/app/PHPexcel/phpexcel.php
Into this library
application/library/excel.php
In codeIgniter. Base url in my config file is empty.
pls help.
On your file try include or require
File-name: application > library > Excel.php
<?php
// dirname will let you access out side application folder.
$file = dirname(FCPATH . '/api/app/PHPexcel/phpexcel.php');
// $file = FCPATH . 'api/app/PHPexcel/phpexcel.php';
require_once $file;
class Excel {
public function __construct() {
}
}
Folder Layout
application
api
api > app
system
index.php
Best not to leave your base_url blank will run into some issues later.
Use absolute path. Something like:
'/var/www/html/api/app/PHPexcel/phpexcel.php'
or
'C:\wamp\www\api\app\PHPexcel\phpexcel.php'
Note that you have some predefined constants in CodeIgniter itself:
APPPATH => '/path/to/application/'
FCPATH => '/path/to/directory/where/index/php/file/is/
etc...
Those constants are defined in index.php file.
Commonly used practice is to put (e.g.) PHPexcel directory into
APPPATH . 'libraries'
location so structure could be
-application
--libraries
---PHPexcel
----phpexcel.php
---Excel.php # don't forget have it with ucfirst rule if CI3
or you can put directory in third_party and include it as
APPPATH . 'third_party/PHPexcel/phpexcel.php';
But it is up to you how do you like to arrange files/directories structure of your app.
In application/config/autoload.php file there is variable named $autoload['libraries'] = array();
you should define your library in this array and your library will be loaded automatically.

Laravel uploading a file to the right directory

I upload the files alright. Only it turns out that, in my case, the starting directory is Public, and that results that the files are accesible by everybody when they should be in the directories that are protected by previous authentication, that is, under App.
So, when I do this:
$file = Input::file('fichero');
$destinationPath = 'uploads/folder';
$file->move($destinationPath);
// Create model
$model = new Model;
$model->filename = $file->getClientOriginalName();
$model->destinationPath = $destinationPath;
The file is saved in here: localh.../myweb/public/
and that is outside the App parent folder where all my application files are.
I can imagine that the fact that is going to that directory is because it is taking the default in the configuration file, which is localhost/laravel/public/index.php/
so, writing something like
$file->move(base_path().'/uploadfolder/subdirectory/', $newname);
will start from what is fixed as base_path, but the base_path needs to be something like localhost/mywebname/public.
So, how do you write the path so that it goes to directories under App and not under Public?
I hope I am not asking a dumb question, but I have been trying for days and I have read the 3 related queries in here.
thank you
You may use:
app_path() // Path to the 'app' folder
app_path('controllers/admin') // Path to the 'app/controllers/admin' folder
This will return you local file system path, you should put your uploaded images in public/... folder.

How to find Joomla root directory

how to I find out the Joomla root directory to use in files? I tried the following with no luck:
$path = JPATH_COMPONENT;
//Gives component root
$path = JURI::root();
//Gives absolute path, can't be used for some reason (http://www.site.com)
$path = $_SERVER['DOCUMENT_ROOT'];
//Gives public_html folder, but if someone installs joomla in a directory and not in root, it wrecks the whole thing
Is there a way to find the joomla root of the site only? Thanks.
may be this can help you
http://docs.joomla.org/Constants
Let's say you want to include an image from the images folder, you can simply use this:
$image = JUri::root() . 'images/image_name.png';
JUri::root() and both JUri::site() always define the root directory

Resources