Uploaded file is being saved as .txt while using storage::disk I am using following code to upload csv file. But, the file is being saved as
storage/apps/files/uniqid.csv/another_long_unique_id.txt
It is saving as .txt files and also one extra folder with same unique id is generating
$extension = $request->file('import_file')->getClientOriginalExtension();
$filename = uniqid().'.'.$extension;
Storage::disk('local')->put('/files/'.$filename, $request->file('import_file'));
'getClientOriginalExtension' will actually give the real extension.
Are you sure the file you are uploading ends with '.csv' and not something like 'test.csv.txt' ?
Yeah, I have checked that by printing out:
print_r($filename); //5af99307e3ee3.csv
Aah, I see it now:
// FilesystemAdapter:
public function putFile($path, $file, $options = [])
{
return $this->putFileAs($path, $file, $file->hashName(), $options);
// note the 3rd param: $file->hashName()
}
// FileHelpers:
public function hashName($path = null)
{
if ($path) {
$path = rtrim($path, '/').'/';
}
$hash = $this->hashName ?: $this->hashName = Str::random(40);
return $path.$hash.'.'.$this->guessExtension();
// $this->guessExtension()...... this is what returns .txt on your text file
}
Fix:
$extension = $request->file('import_file')->getClientOriginalExtension();
$filename = uniqid().'.'.$extension;
Storage::disk('local')->putFileAs('/files/', $request->file('import_file'), $filename);
Yeah, that works fine and how can I retrieve that file path? I tried with
$csv_file_path = storage_path('/files/contribution_data/').$request->filename;
But it returns following error,
C:\wamp64\www\project\storage\/files/contribution_data/5af9b0c1cfa8f.csv: failed to open stream: No such file or directory
But actually there is file with same name.
Remove the leading slash.
$csv_file_path = storage_path('files/contribution_data/').$request->filename;
Try checking the directory through the storage facade:
$files = Storage::files('files/contribution_data');
dd($files);
print_r returns
Array
(
[0] => files/contribution_data/5af9af79e1fd6.csv
[1] => files/contribution_data/5af9b0a25bc81.csv
[2] => files/contribution_data/5af9b0b0b27cf.csv
[3] => files/contribution_data/5af9b0c1cfa8f.csv
)
And if you get the file directly from the facade?
$contents = Storage::get('files/contribution_data/5af9b0c1cfa8f.csv')
dd($contents);
It provide the contents, but I need the path of the csv file so that i can parse it.
Well, we've just confirmed that the file is there at the expected path and the storage facade can access it.
From here it's difficult to tell where the problem might be without looking at your code, specifically, the part of it where you're trying to fetch the file and parse it.
I'm thinking this is one of those nice windows bugs.. you have mixed / and \ in your path.
$path = Storage::disk('local')->putFileAs('/files/', $request->file('import_file'), $filename);
dd($path);
Does that also still have both \ and / in there?
If so, try
$path = Storage::disk('local')->putFileAs('/files/', $request->file('import_file'), $filename);
$path = str_replace(['/', '\'], DIRECTORY_SEPARATOR, $path);
dd($path);
That should always return the correct slashes in the path
Please sign in or create an account to participate in this conversation.