Laravel file upload name is incorrect - laravel

If i do the following with file example.jpg the storage file is getting a random name. How can i give it the example.jpg name?
Storage::disk('local')->put('test', $request->file('file') )
This works, but how can i specify a disk?
$file = request()->file('files');
$file->storeAs('test',$request->file('files')->getClientOriginalName());

From the docs,
If you would not like a file name to be automatically assigned to your stored file, you may use the storeAs method, which receives the path, the file name, and the (optional) disk as its arguments:
$file->storeAs('test', $request->file('files')->getClientOriginalName(), 'local');

Related

Search the File Pattern from File Name

I have a file Patter_File.txt which stores lines like below -
ABC|ABC_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].dat|8|,|70|NAME
ABC|ABC_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].dat|9|,|70|PLACE
XYZ|XYZ_[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].dat|23|,|70|SSN
XYZ|XYZ_[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].dat|33|,|70|DOB
MNO|MNO_SUMMIT.dat|40|,|70|ADDRESS
MNO|MNO_SUMMIT.dat|5|,|70|COUNTRY
So this PATTERN_FILE.txt stores some information of the actual file but file name is stored in the pattern(if file name has date in the name) except the actual name.
My requirement is a command in which I should pass the actual file name like "ABC_20200408.dat" and it should return all the related lines from this file. Can someone please help.
below command is working fine but in this case I have to pass each pattern one by one to check which one is working.
echo "ABC_20200408.dat"|grep ABC_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].dat

Set Filename Storage::put

I cant set a filename with Storage::put in Laravel 5.8
I tried the following:
Storage::put(private/foo/bar , $file, 'private');
This is creating the folder structure with a random generated filename inside. (Like: private/foo/bar/uidjasbknfsdoiruewjnfsdai.pdf)
Storage::put(private/foo/bar/file.pdf , $file, 'private');
This is creating this: private/foo/bar/file.pdf/uidjasbknfsdoiruewjnfsdai.pdf
I expect my own given filename on this private file. The file should not be public.
According to Laravel docs:
If you would not like a file name to be automatically assigned to your stored file, you may use the storeAs method, which receives the path, the file name, and the (optional) disk as its arguments:
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
You may also use the putFileAs method on the Storage facade, which will perform the same file manipulation as the example above:
$path = Storage::putFileAs(
'avatars', $request->file('avatar'), $request->user()->id
);
The third argument for the putFileAs is the visibility of the file. If you would like every users can access it, use public instead of $request->user()->id.
You can read more here.

How to get the full name of a folder if I only know the beginning

I am receiving an input from the user which looks like follows:
echo +++Your input:+++
read USER_INPUT
The way I should use it is to retrieve the full name of a folder which starts with that input, but that contains other stuffs right after. All I know is that the folder is unique.
For example:
User input
123456
Target folder
/somepath/someotherpath/123456-111-222
What I need
MYNEED=123456-111-222
I was thinking to retrieve this with an MYNEED=$(ls /somepath/someotherpath/$USER_INPUT*), but if I do this I will get instead all the content of /somepath/someotherpath/123456-111-222 because that's the only folder existing with that name so the ls command directly goes to the next step.
May I have your idea to retrieve the value 123456-111-222 into a variable that I will need to use after?
basename extracts the filename from the whole path so this will do it:
MYNEED=$(basename /somepath/someotherpath/123456*)

SQLLDR file path argument

I have more than 30 files to load the data.
The path changes at every run in those files. So the path becomes
INFILE "/home/dmf/Cycle7Data/ITEM_IMAGE.csv"
INFILE "/home/dmf/Cycle8Data/ITEM_IMAGE.csv"
The file names change on every control file (SUPPLIER.csv)
Is there any way to pass the File path in a variable, or set any Env. Variable?
So that the control file is not edited everytime
You can pass the data file name on the command line; from the documentation:
DATA specifies the name of the data file containing the data to be loaded. If you do not specify a file extension or file type, then the default is .dat.
If you specify a data file on the command line and also specify data files in the control file with INFILE, then the data specified on the command line is processed first. The first data file specified in the control file is ignored. All other data files specified in the control file are processed.
So pass the relevant file name with each invocation, e.g.
sqlldr user/passwd control=myfile.ctl data=/home/dmf/Cycle7Data/ITEM_IMAGE.csv
If you have lots of files to load from a directory you could have a shell script that loops over the directory contents and passes each file name in turn to an SQL*Loader session.

Laravel downloads a file with unknown type

I am using Laravel 4. I allow users to upload a file, which is programmatically renamed with some number and stored. I also allow them to download the files, though I am supposed to rename their file from some funny number to their name, and download it.
My problem is, how can I change filename just before its downloaded?
My code:
return Response::download($pathToFile, $name);
When I do that, the file is downloaded with unknown format.
The second $name parameter, needs to include the full filename (including the file extension). The download method does not automatically detect the extension from the file path and append it to the name, so that needs to be done manually. Something like this will work:
return Response::download(
$pathToFile,
$name . '.' . pathinfo($pathToFile, PATHINFO_EXTENSION)
);

Resources