【问题标题】:Laravel Return NULL when uploading large filesLaravel 上传大文件时返回 NULL
【发布时间】:2015-04-08 13:45:57
【问题描述】:

我的表格

{{ Form::open(array('url' => 'upfile', 'files' => 'true', 'method' => 'post', 'class' => 'form')) }}
        <p>Sólo se admiten archivos en formato .pdf y no mayor a 127MB</p>
        <p>
        {{ Form::file('expediente', array('class' => 'text-field column_one', 'required' => 'required')) }}
        </p><br>
        <p>
        {{ Form::text('nombrearchivo', null, array('class' => 'text-field column_one', 'required' => 'required', 'placeholder' => 'Nombre del Expediente.')) }}
        </p><br>

        {{ Form::hidden('username', $username) }}

        <p style="color: red;">
            <ul>
            @foreach ($errors->all() as $message)
                <li style="color:red;">{{$message}}</li>
            @endforeach
            <ul>
        </p>
        {{ Form::submit('Añadir Expediente', array('class' => 'submit submitNavy submitForm')) }}
 {{ Form::close() }}

我的控制器:

public function saveExpediente(){
    ini_set("memory_limit","7G");
    ini_set('upload_max_filesize', '127M');
    ini_set('post_max_size', '127M');
    ini_set('max_input_time', 0);
    ini_set('max_execution_time', 0);
    set_time_limit(0);
    ignore_user_abort(true);

    $rules = array(
        'username' => 'required|exists:users,username',
        'expediente' => 'required',
        'nombrearchivo'    =>  'required|min:5'
    );

    $validator = Validator::make(Input::all(), $rules);
    //$fileExtension = Input::file('expediente')->guessClientExtension();
    $file = Input::file('expediente');
    if ($validator->fails()) {
        //dd($file);
        return Redirect::back()
            ->withErrors($validator) // send back all errors to the login form
            ->withInput(); // send back the input (not the password) so that we can repopulate the form
    }/*else if ($fileExtension != 'pdf'){
        $validator->failed();
        return Redirect::to('upload')->withErrors([
            'expediente' => 'El archivo debe estar en formato PDF!',
        ])->withInput();
    }*/else {

        File::makeDirectory('expedientes/'.Input::get('username'), 0770, true, true);
        Input::file('expediente')->move('expedientes/'.Input::get('username'),Input::file('expediente')->getClientOriginalName());

        $expediente = new Expediente;
        $expediente->username =  Input::get('username');
        $expediente->archivo = Input::file('expediente')->getClientOriginalName();
        $expediente->nombrearchivo = Input::get('nombrearchivo');
        $expediente->save();

        return Redirect::back()->with('message', 'Se añadió correctamente el expediente al usuario.')->with('tipo','message-success');
    }
}

当我上传大于 8mb 的 laravel 返回 NULL 时,就像字段一样,它们是空的。我已经得到了变量 PHP.init 的值。我使用 Laravel 4.2 和 Apache,需要上传 25-40 mb 的文件大小。如果我尝试 dd(Input::file('expediente')) 返回 NULL

【问题讨论】:

  • 您是否在 php.ini 中检查了您的最大上传文件大小?
  • 还有 post_max_size 吗?
  • 哦..不!它是 7MB... 现在我们确定这就是问题所在。该应用程序位于共享服务器上。我们不能在不联系公司支持的情况下更改变量值吗?
  • 您不能为此使用“ini_set”。您必须联系您的托管公司。也许他们有解决方案。

标签: php apache laravel file-upload


【解决方案1】:

您需要增加文件上传的内存限制。您可以通过在控制器开头提及限制大小来做到这一点。

ini_set('memory_limit', 'size');

例如:

ini_set('memory_limit', '40M');

您可能还需要在表单中提及enctype(编码类型)

例子:-

{{ Form::open(array( 'url' => 'upfile', 'method' => 'post', 'class' => 'form', 'enctype' => 'multipart/form-data' )) }}

也许你不需要这个 'files' =&gt; 'true'

【讨论】:

    【解决方案2】:
    1. 检查错误:

      $request-&gt;file('image')-&gt;getErrorMessage()

    2. 如果错误看起来像:

      The file "image_name.jpeg" exceeds your upload_max_filesize ini directive (limit is 2048 KiB)

    3. 在空白页执行下一条命令定位php.ini:

      &lt;?php phpinfo() ?&gt;

    4. 在 php.ini 文件中编辑下一行:

      post_max_size

      upload_max_filesize

    5. 增加内存限制(默认2M),10M可以试试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 2020-02-03
      • 1970-01-01
      • 2022-07-06
      • 2014-07-23
      • 1970-01-01
      • 2021-12-31
      相关资源
      最近更新 更多