如果是对象,如何转换文件名中的数字?我试过str_replace但它不起作用



我正在尝试将文件名中的数字从阿拉伯语转换为英语,因为一些客户上传的文件包含阿拉伯数字,所以当我尝试下载时无法下载,它显示了一些错误。

使用str_replace函数后,它将名称更改为:

phpF77E.tmp

我只希望它将阿拉伯数字转换为英语,而不是将其更改为";tmp";文件

上传和存储文件功能:

public function storeCv(Request $request, $trainee_id)
{
$request->validate([
'cv' => 'required_without:file',
'file' => 'required_without:cv',
]);
$trainee = Trainee::findOrFail($trainee_id);
$file = $request->file('cv') ?: $request->file('file');
$convert_num = $this->convert($file);
$uploaded_file = $trainee->uploadToFolder($convert_num, 'cv');
// When the other has been filled, mark the application as pending approval from the administration.
if ($trainee->cv_url) {
$trainee->status = Trainee::STATUS_PENDING_APPROVAL;
$trainee->save();
}
return $uploaded_file;
}

上传到文件夹功能:

public function uploadToFolder($file, $folder)
{
return $this->addMedia($file)
->withAttributes([
'team_id' => $this->team_id,
])
->toMediaCollection($folder);
}

转换功能:

public function convert($str)
{
$arabic_eastern = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
$arabic_western = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
return preg_replace($arabic_eastern, $arabic_western, $str);
}

如果您使用Laravel,答案如下:

if (! $file->isValid()) {
// the file is not valid
}
$originalName = $file->getClientOriginalName();
$filename = $this->convert($originalName); // you function
$fullpathoffile = $file->storeAs('', $filename, ['disk' => 'folderSomething']); //define in filesystems.php
$filename = basename($fullpathoffile);

config/filessystems.php

'disks' => [
'folderSomething' => [
'driver' => 'local',
'root'   => public_path('uploads/folder-something'),
],
...

最新更新