【问题标题】:How to submit custom input file type data in php?如何在php中提交自定义输入文件类型数据?
【发布时间】:2021-11-03 02:04:48
【问题描述】:

我无法将文件数据发送到表单的action 页面。我正在使用 Laravel 8。

<label for='inputProductDescription' class='form-label'>Product Images</label>
<input id="fancy-file-upload" type="file" name="files[]" accept=".jpg, .png, image/jpeg, image/png" multiple>

使用jquery.fancy-fileupload.js,这个jquery 我的动作文件代码:

function add_product(Request $req) {
    $images = $req->file('file');
    if($req->hasFile('file')):
        foreach ($images as $item):
            $var = date_create();
            $time = date_format($var, 'YmdHis');
            $imageName = $time . '-' . $item->getClientOriginalName();
            $path = $item->move(base_path() . '\public\assets\upload\images', $imageName);
            $arr[] = $imageName;
        endforeach;
    endif;

    return $imageName;
}

当我上传图片时,$imageName 给我一个错误,它没有定义。

【问题讨论】:

    标签: php html laravel laravel-8


    【解决方案1】:

    在方法中声明 $imageName 属性,如 global。

      $imageName = null;
      $images = $req->hasFile('file') ? $req->file('file') : null;
       if($images)
          foreach ($images as $item)
            $time = now()->format('YmdHis');
            $imageName = $time . '-' . $item->getClientOriginalName();
            $path=  $item->move(base_path() . '\public\assets\upload\images', $imageName);
            $arr[] = $imageName; // seems this is unnecessary line of code
          endforeach;
        endif;
    
    
       return $imageName;
    

    【讨论】:

    • 现在我没有显示这个错误。我正在上传图像,但没有得到这个变量值 ($imageName)。当我不在输入字段中使用 id(id="fancy-file-upload") 时,代码工作正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2015-09-19
    • 2015-05-22
    • 2021-10-24
    • 1970-01-01
    • 2014-06-07
    • 2016-02-23
    相关资源
    最近更新 更多