【问题标题】:Issues with getting file name & absolute path - Laravel 5 PHP获取文件名和绝对路径的问题 - Laravel 5 PHP
【发布时间】:2015-10-04 00:06:16
【问题描述】:

我正在尝试自动获取已上传文件的一些详细信息,但是有两个小问题。我无法在没有扩展名的情况下显示文件名,并且绝对路径结果很奇怪,例如 C:\Users\Tyrx\Desktop\php part 2\FileDepository - back to\public/assets/ 7ofmMvm.jpg.

有没有人可以引导我朝着正确的方向前进?这是生成文件名和文件路径的两个sn-ps代码。

文件名(例如,这会导致 7ofmMvm.jpg 而不是 7ofmMvm)

    $filelist->name = $this->request->file('asset')->getClientOriginalName();

文件路径

    $fullfilepath = $destinationPath . $filelist->name;
    $filelist->file_path = $fullfilepath;

函数的完整代码

public function store()
{
    $filelist = new Filelist($this->request->all());
    //store file details 
    $filelist->name = $this->request->file('asset')->getClientOriginalName();
    $filelist->file_type = $this->request->file('asset')->getClientOriginalExtension();
    $filelist->file_size = filesize($this->request->file('asset'));
    $filelist->uploader_name = Auth::user()->email;
    $filelist->save();
    // Save uploaded file
    if ($this->request->hasFile('asset') && $this->request->file('asset')->isValid()) {
        $destinationPath = public_path() . '/assets/';
        $fileName = $filelist->id . '.' . $this->request->file('asset')->guessClientExtension();
        $this->request->file('asset')->move($destinationPath, $fileName);

    }

    $fullfilepath = $destinationPath . $filelist->name;
    $filelist->file_path = $fullfilepath;
    $filelist->save();



    return redirect('filelists');
}

    if ($this->request->hasFile('asset') && $this->request->file('asset')->isValid()) {
        $destinationPath = public_path() . '/assets/';
        $fileName = $filelist->id . '.' . $this->request->file('asset')->guessClientExtension();
        $this->request->file('asset')->move($destinationPath, $fileName);

    }
    $fullfilepath = $destinationPath . $filelist->name;
    $filelist->file_path = $fullfilepath;
    $filelist->save();

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    来自 php 手册

    <?php
    $path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
    
    echo $path_parts['dirname'], "\n";
    echo $path_parts['basename'], "\n";
    echo $path_parts['extension'], "\n";
    echo $path_parts['filename'], "\n"; // since PHP 5.2.0
    ?>
    

    上面的例子会输出:

    /www/htdocs/inc
    lib.inc.php
    php
    lib.inc
    

    windows + php 用 / 和 ... 混淆路径

    Legoas 在手册http://php.net/manual/en/function.pathinfo.php 上有此贡献

    "获取当前解析的PHP脚本的文件夹绝对路径最好的方法是:

    <?php 
    
    if (DIRECTORY_SEPARATOR=='/') 
        $absolute_path = dirname(__FILE__).'/'; 
    else 
        $absolute_path = str_replace('\\', '/', dirname(__FILE__)).'/'; 
    
    ?> 
    

    这将产生一个绝对 unix 样式的路径,它在 Windows 下的 PHP5 上也可以正常工作,其中混合 '\' 和 '/' 可能会带来麻烦。 "

    【讨论】:

    • 我目前无法测试此代码,但会像 $filelist->name = pathinfo($this->request->file('asset'), PATHINFO_BASENAME );是否可以获取不带扩展名的文件名?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 2013-11-17
    • 2017-03-16
    • 2021-10-18
    • 2012-04-24
    相关资源
    最近更新 更多