Laravel 对于同一个文件,我会得到不同的随机值

Laravel 对于同一个文件,我会得到不同的随机值,laravel,laravel-6,Laravel,Laravel 6,我试图在数据库中存储照片,我使用下面的方法生成一个随机文件名并将其存储在数据库中 $path = $request->file('profile_photo')->store('public/profiles'); $profile = ltrim($path,"public/profiles/"); 然而,有时我会从中得到不同的值 DB 在我的文件夹中 我使用的是laravel 6。ltrim(),rtrim(),trim()按字符掩码移除,而不是完整字符串

我试图在数据库中存储照片,我使用下面的方法生成一个随机文件名并将其存储在数据库中

 $path = $request->file('profile_photo')->store('public/profiles');
        $profile = ltrim($path,"public/profiles/");
然而,有时我会从中得到不同的值 DB

我的文件夹中

我使用的是laravel 6。

ltrim()
rtrim()
trim()
按字符掩码移除,而不是完整字符串

$profile = ltrim($path,"public/profiles/");
这意味着从
$path
左侧删除所有“p”、“u”、“b”、“l”、“i”、“c”、““/”等

如果要获取不带路径的文件名,可以使用
basename()
函数

$profile = basename($path);
ltrim()
rtrim()
trim()
按字符掩码删除,而不是按完整字符串删除

$profile = ltrim($path,"public/profiles/");
这意味着从
$path
左侧删除所有“p”、“u”、“b”、“l”、“i”、“c”、““/”等

如果要获取不带路径的文件名,可以使用
basename()
函数

$profile = basename($path);

好的,看看这个
src/illumb/Http/UploadedFile.php

/**
 * Store the uploaded file on a filesystem disk.
 *
 * @param  string  $path
 * @param  array|string  $options
 * @return string|false
 */
public function store($path, $options = [])
{
    return $this->storeAs($path, $this->hashName(), $this->parseOptions($options));
}
/**
 * Store the uploaded file on a filesystem disk.
 *
 * @param  string  $path
 * @param  string  $name
 * @param  array|string  $options
 * @return string|false
 */
public function storeAs($path, $name, $options = [])
{
    $options = $this->parseOptions($options);

    $disk = Arr::pull($options, 'disk');

    return Container::getInstance()->make(FilesystemFactory::class)->disk($disk)->putFileAs(
        $path, $this, $name, $options
    );
}
你们可以这样做来避免碰撞和混乱

// cache the file
$file = $request->file('profile_photo');

// generate a new filename. getClientOriginalExtension() for the file extension
$filename = 'profile-photo-' . time() . '.' . $file->getClientOriginalExtension();

// save to public/photos as the new $filename
$path = $file->store('public/photos', $filename);

dd($path); // Check if you get the correct file path or not

好的,看看这个
src/illumb/Http/UploadedFile.php

/**
 * Store the uploaded file on a filesystem disk.
 *
 * @param  string  $path
 * @param  array|string  $options
 * @return string|false
 */
public function store($path, $options = [])
{
    return $this->storeAs($path, $this->hashName(), $this->parseOptions($options));
}
/**
 * Store the uploaded file on a filesystem disk.
 *
 * @param  string  $path
 * @param  string  $name
 * @param  array|string  $options
 * @return string|false
 */
public function storeAs($path, $name, $options = [])
{
    $options = $this->parseOptions($options);

    $disk = Arr::pull($options, 'disk');

    return Container::getInstance()->make(FilesystemFactory::class)->disk($disk)->putFileAs(
        $path, $this, $name, $options
    );
}
你们可以这样做来避免碰撞和混乱

// cache the file
$file = $request->file('profile_photo');

// generate a new filename. getClientOriginalExtension() for the file extension
$filename = 'profile-photo-' . time() . '.' . $file->getClientOriginalExtension();

// save to public/photos as the new $filename
$path = $file->store('public/photos', $filename);

dd($path); // Check if you get the correct file path or not

如果您想防止名称重复,可以使用当前时间戳并将其与实际文件名连接起来。时间戳\u realfilename。extension@MiladTeimouri
store()
函数已经生成了一个冲突几率非常非常低的文件名。如果要防止名称重复,可以使用当前时间戳并将其与实际文件名连接起来。时间戳\u realfilename。extension@MiladTeimouri
store()
函数已经生成了一个文件名,冲突的可能性非常非常低。