Laravel uploading a file to the right directory - laravel

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.

Related

Issue getting a file with Laravel

Hi I do not know what is happening but I want to get a file from the storage folder and it says that it does not existe but it exists I mean I go to the folder and it is there..
I retrieve the file like this:
$path = storage_path('app/public/' . $settlement->support);
$settlement_file = Storage::disk('local')->get($path);
And I get this error:
message: "File not found at path:
home/jysparki/newjis.jisparking.com/storage/app/public/06152617_8-2025.pdf"
and If I go to the folder it exists so I wonder what am I doing wrong? Thanks.
If you look at config/filesystems.php file and find disks array, you may notice that root under the local disk key is set to storage_path('app'). Well it's up to what the value may be in your case you're free to change it, but what does this mean is whenever you call Storage::disk('local') method it will try to find files relative to storage_path('app'), so there is no need to specify full path. Try this instead
$settlement_file = Storage::disk('local')->get('/public/' . $settlement->support);
or
$settlement_file = Storage::disk('public')->get($settlement->support);
as Laravel by default has public disk which pointed into /app/public storage directory

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);

How to copy more than 5k files with FTP client from linux server

i need to backup a site's FTP. The site is hosted on a linux server. The problem is that there is a folder with more than 5k files. Linux can't show me more than 4998 files, so i cant copy these files 'cause the server don't give me more than 4998.
I can't delete these files to see the others 'cause the site is actually online. I can't move these file in another directory for the same reason.
What can i do? i'm trying using a shell...but i don't know...i'm not sure using this method.
I got solution for my own answer
<?php
$rootPath = realpath('wp-content/uploads/2014/07');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('dio.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** #var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
You can do this through the command line, this guide shows you how. It seems mget (the ftp command) isn't recommended for recursive calls (subfolders and their content), so wget can also be used, se this.
I also like to zip such folders with many files into one, for easy oversight when up-and-downloading. Use
zip -r myfiles.zip myfiles
Here's a guide for that too.

Laravel image upload creating folder instead of file

When I try to upload image from the form , Laravel is creating directory out of image name. Here is my basic code:
$file = Input::file("image");
$file = $file->move(public_path(). "/img/".$file->getClientOriginalName());
//file name is IPC_diagram.png
When die and dump I got this:
'/var/www/php/frbit/l4blog/public/img/IPC_diagram.png/phpvEb9zk'
Now name of image is name of new folder and image is renamed to some random php string, and placed in that folder.
What is the problem. Maybe something related to Linux specific handling of files. Also I was looking into symfony code for this, and symfony is trying to crete new folder every time file is moved but I don't understand how it is related to my code.
Thanks for help.
Do not use dot. Use comma, like this:
$name = time() . $file->getClientOriginalName();
$file->move('uploads/posts/',$name);

How do I get the full local path of a file uploaded with $_FILES

I am uploaded a file through the file input. If I print_r($_FILES), I can get several pieces of info, but it doesn't give me the full LOCAL path of the file (the path on my computer, not the server). How do I get this?
I need this to use the FTP library in CodeIgniter. Documentation is here on how to use it to upload a file.
As you can see, it requires the full local path, although I'm not sure why.
As file upload path is a variable which changes from server to server, I would suggest you to use move_uploaded_file() function:
$target = "uploads/myfile.something";
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target)) {
// do something
}
that way it will always work no matter what the path is even if it changes. The target can be anything you wish, it's relative to the current folder where script is being executed.
I solved this.
public function upload($file, $folder) {
$this->CI->ftp->upload($_FILES['file']['tmp_name'], '/public_html/rest/of/path/' . $_FILES['file']['name'], 'ascii', 0775);
}
The file is being uploaded, so you can access it with $FILES['file']['tmp_name']. This is from within a class, so that's why I'm using $this->CI. Otherwise, I would just use $this.

Resources