doupeng6890 2015-10-03 15:38
浏览 128
已采纳

获取文件名和绝对路径的问题 - Laravel 5 PHP

I'm trying to automatically fetch some details of a file that has been uploaded, however there are two slight issues. I can't manage get the file name to show up without an extension, and the absolute path turns out wonky such as C:\Users\Tyrx\Desktop\php part 2\FileDepository - back to\public/assets/7ofmMvm.jpg.

Is there any chance somebody could guide me in the right direction? Here's the two snippets of code for generating the file name and file path.

File Name (for example, this results in 7ofmMvm.jpg instead of 7ofmMvm)

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

File Path

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

The full code of the function

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();
  • 写回答

1条回答 默认 最新

  • douzhuo1858 2015-10-03 15:51
    关注

    From php manual

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

    The above example will output:

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

    and windows + php messes up paths with / and ...

    There is this contribution from Legoas on the manual http://php.net/manual/en/function.pathinfo.php

    "The best way to get the absolute path of the folder of the currently parsed PHP script is:

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

    This will result in an absolute unix-style path which works ok also on PHP5 under Windows, where mixing '\' and '/' may give troubles. "

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ue5运行的通道视频都会有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数