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.
-
are you using Laravel 3 or Laravel 4?– LaurenceFeb 12, 2013 at 16:26
8 Answers
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');
-
7Just to add that
public_path()
is actually pretty useless- it just assumes all your public files are in a directory called "public"- even if they aren't. For example, I always have to change the default "public" directory to "public_html" (in line with my server). Laravel doesn't recognise this. So don't rely on this to make any intelligent determination of what your public directory actually is. I'm just using$_SERVER['DOCUMENT_ROOT'];
unless anyone can suggest why I shouldn't?– InigoOct 21, 2017 at 17:40 -
12@inigo it is not useless, you are just not using it as it was intended. If you want to change the directory of public, you need to tell Laravel about it: stackoverflow.com/questions/31758901/…– JustinMay 2, 2018 at 21:23
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.
-
1is base_path defined in laravel .. i used it i got this error Call to undefined function base_path() Feb 12, 2013 at 16:48
-
Yes - it is defined in helpers.php. I tested in on my L4 app - it works. Make sure you are running the latest L4 build - it might not have been in the earlier versions.– LaurenceFeb 12, 2013 at 16:51
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.
Use asset()
function;
for example,
you have image.png in the assets folder in the public dir
ie: public/assets/image.png
You can access it by:
$image = asset('assets/image.png');
To get a public directory in laravel controller:
you can use
$custom_url = "sdefrghj";
$filename="assets/".$custom_url.'-question-list.csv';
hope you get it :-)