laravel 上传文件时的自定义名称

问题描述 投票:0回答:1
我想问,如果我想在上传图像文件时自定义图像文件,例如“product-code_product-name.jpg”或“111_instant-noodle.jpg”之类的内容,我该如何在 $fileName 上写入?

这是代码:

$fileName = <something to code here>; $path = $request->file('the_file')->storeAs('the_path', $fileName, 'public'); return $path;


或者也许有其他方法可以让我实现这一目标?

我想要实现的是,当文件上传到数据库时,我希望文件以这种名称“product-code_product-name.jpg”保存,而不是使用它的原始名称。

我尝试过:

$fileName = time() . $request->file('the_file')->getClientOriginalName()


但它以原来的名称保存,我不想要这个,那我该怎么办?

感谢您回答我的问题,并为我糟糕的英语感到抱歉。

file-upload filenames laravel-9
1个回答
0
投票
您可以执行类似的操作,根据您的选择/要求创建自定义文件名。

// Combine the sanitized product code and product name $customFileName = $productCode . "_" . $productName . ".jpg"; // Replace any spaces with underscores $customFileName = str_replace(" ", "_", $customFileName); // Save the file with the custom filename $path = $request->file('the_file')->storeAs('the_path', $customFileName, 'public');
    
© www.soinside.com 2019 - 2024. All rights reserved.