Bulk rename multiple files when upload
Hi there, how can I rename each files on multiple files upload?
For example, if I upload 2 files, the file name array will be like this:
array:2 [▼
0 => "qwe"
1 => "rty"
]
and the file array will be (based on file original name):
array:2 [▼
0 => "1_1600.jpg"
1 => "1200x630bb.jpg"
]
and will be rename into something like this when storing to directory:
array:2 [▼
0 => "qwe.jpg"
1 => "rty.jpg"
]
Thank you.
store the files as anything you like.
If its not working then show your code.
@Snapey , the uploaded files already stored at the directory, but the file name not correct.
Here's the current code:
public function store(Request $request){
$imgname = explode(',', $request->img_name);
if ( $request->hasFile('img_file') ) {
$files = $request->file('img_file');
foreach ($files as $file) {
$ext = $file->getClientOriginalExtension();
foreach ($imgname as $filename) {
$newfilename = $user->id . '-' . $user->reg_num . '-' . str_random(30) . '-' . str_replace(' ' , '-', $filename) . '.' . $ext;
}
$path = 'public/users/' . $user->id . '-' . $user->reg_num . '/' . $newfilename;
Storage::put($path, File::get($file));
}
}
}
The new file name only take the last value from array.
For Example:
array:2 [▼
0 => "qwe"
1 => "rty"
]
// Current new name will be (which is not correct):
array:2 [▼
0 => "rty.jpg"
1 => "rty.jpg"
]
Get rid of this loop
foreach ($imgname as $filename) {
@Snapey , hi there, sorry for late response, if I remove the loop
foreach ($imgname as $filename) { }
it give me error: Array to string conversion
@Snapey , Hi there, after remove the loop like you mentioned, I alter the codes a little, and do array_combine, it work perfectly now.
Thx for the hint and your time.
Please or to participate in this conversation.