简体   繁体   English

使用Laravel在页面上加载时获取文件信息

[英]Getting file information while loading on page with Laravel

I have upload form where I'm able to upload any type of files and save their path to database. 我有上传表单,可以在其中上传任何类型的文件并将其路径保存到数据库。 Without saving any relevant information in database such as: filetype , filesize etc can I get this information while I loop the results and showing them on page? 如果不将任何相关信息保存在数据库中,例如: filetypefilesize等,则可以在循环结果并将其显示在页面上时获取此信息?

Here is what I tried so far 这是我到目前为止尝试过的

public function fileUpload()
{
    $allFiles = Documents::paginate(20);

    $files = array();

    foreach ($allFiles as $file) {

        $files[] = $this->fileInfo(pathinfo(public_path() . '/uploads/' . $file));
    }

    return View::make('admin.files', [
        'files' => $files
    ]);
}

public function fileInfo($filePath)
{
    $file = array();
    $file['name'] = $filePath['filename'];
    $file['extension'] = $filePath['extension'];
    $file['size'] = filesize($filePath['dirname'] . '/' . $filePath['basename']);

    return $file;
}

The error which I get when I run the page is 运行页面时出现的错误是

ErrorException: filesize(): stat failed for ..... ErrorException:filesize():..的统计信息失败。

Is this the correct way of doing this? 这是正确的方法吗?

Update: dd($filePath['dirname'] . '/' . $filePath['basename']); 更新: dd($filePath['dirname'] . '/' . $filePath['basename']); return 返回

string(126) "/var/www/html/site/public/uploads/{"id":2,"document_path":"aut6MnADFrPZTz4TJf0Y.pdf","document_name":"Some title for the file"}"

Change this line 更改此行

$files[] = $this->fileInfo(pathinfo(public_path() . '/uploads/' . $file));

to

$files[] = $this->fileInfo(pathinfo(public_path() . '/uploads/' . $file->document_path));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM