【问题标题】:RuntimeException SplFileInfo::getSize(): stat failed in laravelRuntimeException SplFileInfo::getSize(): stat 在 laravel 中失败
【发布时间】:2019-04-06 10:38:13
【问题描述】:

我是 laravel 的新手。我尝试上传图片,但在 getClientsize 中出现错误。我尝试在谷歌中检查解决方案。我跟着它编辑 post_max_size 和 upload_max_size 文件,但它仍然对我不起作用。 任何人都可以帮助解决这个问题。我附上了下面的代码。

控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\file;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\UploadedFile;

class testing extends Controller
{
    public function store(Request $request)
    {
        $user = new file;
        $user->title = Input::get('name');
        if(Input::hasFile('image')){
            $file=Input::file('image');
            $file->move(public_path().'/images/', $file);
            $user->name=$file->getClientoriginalName();
            $user->size=$file->getClientsize();
            $user->type=$file->getClientMimeType();
        }
        $user->save();
        return "data saved";
    }

【问题讨论】:

    标签: php html laravel postgresql


    【解决方案1】:

    请更改

    getClientsize() -> getClientSize()
    

    但如果它没有帮助 - 你的问题不在你的 php 文件中,而是在 php 配置文件中。默认情况下,您可以上传 2M(兆字节)的文件。

    ; Maximum allowed size for uploaded files.
    ; http://php.net/upload-max-filesize
    upload_max_filesize = 2M
    

    但是你可以在这里改变它:

    在 .htaccess 中添加一行:

    php_value upload_max_filesize 50M

    或 Ubuntu:

    /etc/php/7.1(你的版本)/apache2/php.ini (找到upload_max_filesize, 并覆盖它)

    /etc/php/php.ini

    编辑文件后 - 重启 apache

    【讨论】:

    • tq 为您的评论..但它不起作用..我无法在 .htaccess 文件中添加 upload_max_filesize ..我尝试添加但它阻止我的 xampp 运行。
    • 你用的是apache还是nginx?
    • @SitiAsna 我更新了我对 htaccess 的回答。对不起,是我的错
    【解决方案2】:

    可能的原因是因为文件已从 tmp 移动到其分配的路径,它不再存在。所以从tmp文件中读取文件大小是无效操作。

    可能的解决方案:在移动文件之前读取文件大小。为我工作。

    if ($req->hasFile('uploadedFile')) 
    {
        $file = $req->file('uploadedFile');
        $filesize = $file->getSize();
        $filename = $file->getClientOriginalName();
    
        if ($file->move('upload/', $filename))
        {
            $insert = DB::table('table')->insert(["filename" =>$filename,
                                                 "size"      =>$filesize]);
        }
    }
    

    【讨论】:

      【解决方案3】:

      通过阅读 laravel 6.* 和 php 7.3 的文档,您可以看到上传文件可以使用 store 方法。 如果您想在第一次运行 php artisan vendor:publishconfig-&gt;filesystems.php 中将文件存储在 public 或 public_html 中,请定义新磁盘,例如名称为 public_local 并将此磁盘的根设置为公共地址:

      'public_local' => [
                  'driver' => 'local',
                  'root' => public_path(),
              ],
      

      然后为了将文件存储在此磁盘中,您可以使用为您的文件设置唯一名称的 store 方法或使用自定义名称的 storeas: 使用 store 方法:

      $path = $request->file('image_url')->store($imagePath, 'public_local');
      
      $path = $request->file('image_url')->storeAs($imagePath, 'custom_name', 'public_local');
      

      【讨论】:

        【解决方案4】:

        我花了将近8个小时终于找出原因,我发现:

        如果您在控制器中上传文件(使用 $file->move(public_path().'/images/', $file);)会出现此错误,如果您使用 '在模型中使用 HasMediaTrait' trait。

        原因: 在已经使用语句 '$file->move(public_path().'/images/', $file);' 上传的控制器文件中 同样,如果您在模型中使用“使用 HasMediaTrait”特征,系统会尝试再次上传文件然后导致此错误。

        解决方案: 如果您在控制器中上传,则无需对模型使用 'use HasMediaTrait' trait,否则只需删除控制器中的文件上传逻辑,只需在模型中使用 trait(使用 HasMediaTrait)。 p>

        我希望这能提供更多见解。

        • 阿迪

        【讨论】:

          猜你喜欢
          • 2020-01-20
          • 2014-07-31
          • 2020-01-18
          • 2020-01-11
          • 1970-01-01
          • 2020-01-20
          • 2023-03-04
          • 1970-01-01
          • 2012-01-26
          相关资源
          最近更新 更多