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.
Asked
Active
Viewed 2.7e+01k times
127

Rubens Mariuzzo
- 28,358
- 27
- 121
- 148

Yasser Moussa
- 2,209
- 6
- 26
- 39
-
are you using Laravel 3 or Laravel 4? – Laurence Feb 12 '13 at 16:26
7 Answers
315
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? – Inigo Oct 21 '17 at 17:40
-
11@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: https://stackoverflow.com/questions/31758901/laravel-5-change-public-path?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Justin May 02 '18 at 21:23
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.

Laurence
- 58,936
- 21
- 171
- 212
-
1is base_path defined in laravel .. i used it i got this error Call to undefined function base_path() – Yasser Moussa Feb 12 '13 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. – Laurence Feb 12 '13 at 16:51
9
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

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

Olusola Omosola
- 847
- 9
- 9
0
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 :-)

Mohd_PH
- 1,590
- 12
- 19