Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel中上载某些文件类型的问题_Php_Laravel_File_File Upload - Fatal编程技术网

Php 在Laravel中上载某些文件类型的问题

Php 在Laravel中上载某些文件类型的问题,php,laravel,file,file-upload,Php,Laravel,File,File Upload,在我的应用程序中,我有一个TemplateController,其中包含一个批量上传方法。它工作得相当好,但每当我尝试上传.eps文件时,Laravel都会返回一个验证错误 文件.0必须是以下类型的文件:jpg、jpeg、png、gif、ai、eps、mp3、, ogg、mpga、mp4、mpeg、doc、docx、dotx、pdf、odt、xls、xlsm、xlsx、ppt、, pptx,vsd 我的问题是,我可以在验证器期望的文件扩展名列表中看到eps 这是TemplateControlle

在我的应用程序中,我有一个
TemplateController
,其中包含一个批量上传方法。它工作得相当好,但每当我尝试上传
.eps
文件时,Laravel都会返回一个验证错误

文件.0必须是以下类型的文件:jpg、jpeg、png、gif、ai、eps、mp3、, ogg、mpga、mp4、mpeg、doc、docx、dotx、pdf、odt、xls、xlsm、xlsx、ppt、, pptx,vsd

我的问题是,我可以在验证器期望的文件扩展名列表中看到
eps

这是
TemplateController

<?php

namespace App\Http\Controllers\Editable;

use App\FileMetaData;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\Controller;

class TemplateController extends Controller
{
    /**
    * Instantiate a new controller instance.
    *
    * @return void
    */
    public function __construct()
    {
        $this->middleware(['role:Template Manager']);
    }

    /**
     * Set allowed extensions for each file category
     * This can be appended to as necessary as it's somewhat restrictive
     */
    private $image_ext = [
        'jpg', 'jpeg', 'png', 'gif', 
        'ai', 'eps'
    ];
    private $audio_ext = [
        'mp3', 'ogg', 'mpga'
    ];
    private $video_ext = [
        'mp4', 'mpeg'
    ];
    private $document_ext = [
        'doc', 'docx', 'dotx', 'pdf', 'odt',
        'xls', 'xlsm', 'xlsx',
        'ppt', 'pptx',
        'vsd'
    ];

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $files = FileMetaData::orderBy('category', 'DESC')->get();

        return view('editable.templates-and-tools.index', compact('files'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('editable.templates-and-tools.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // Get every extension that we allowed
        $all_ext = implode(',', $this->allExtensions());

        $this->validate($request, [
            'name'=>'required|unique:file_meta_data|min:3|max:40',
            'file' => 'required|file|mimes:' . $all_ext . '|max:50000'
        ]);

        // Grab data from the request to fill the necessary fields
        $name = $request->get('name');
        $department = $request->get('department');
        $category = $request->get('category');
        $uploadedFile = $request->file('file');

        // Get the extension and then use this to work out the file type
        $size = $uploadedFile->getSize();
        $extension = strtolower($file->getClientOriginalExtension());
        $type = $this->getType($extension);

        // Create a new instancce of this model
        $file = new FileMetaData();

        $file->name = $name;
        $file->department = $department;
        $file->category = $category;
        $file->size = $size;
        $file->type = $type;
        $file->extension = $extension;

        // Upload all the given files to Storage, within a specific directory
        if ($department != '') {
            $path = $uploadedFile->storeAs('library/' . $department, $name.'.'.$extension);
            $category = null;
        } elseif ($category != '') {
            $path = $uploadedFile->storeAs('library/' . $category, $name.'.'.$extension);
            $department = null;
        }

        // Grab the filepath so we can store it in the database
        $file->filepath = $path;

        // Finally, check that the file exists and save the model
        if (Storage::exists($path)) {
            $file->save();

            return redirect('editable/templates-and-tools')->with('success', $file->name . 'has been added');
        }
    }

    /**
     * Show the form to edit this resource
     */
    public function edit($id)
    {
        $file = FileMetaData::find($id);

        return view('editable.templates-and-tools.edit', compact('file'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // Get every extension that we allowed
        $all_ext = implode(',', $this->allExtensions());

        $this->validate($request, [
            'name'=>'required|unique:file_meta_data|min:3|max:40',
        ]);

        $file = FileMetaData::find($id);

        // Get the current path to this file
        $currentPath = Storage::url($file->name . '.' . $file->extension);

        $extension = pathinfo($currentPath, PATHINFO_EXTENSION);

        // Grab data from the request to fill the necessary fields
        $name = $request->get('name');
        $department = $request->get('department');
        $category = $request->get('category');

        // Upload all the given files to Storage, within a specific directory
        if ($department != '') {
            $newPath = 'library/' . $department . '/' . $name.'.'.$extension;
            Storage::move($file->filepath, $newPath);
            $category = null;
        } elseif ($category != '') {
            $newPath = 'library/' . $category . '/' . $name.'.'.$extension;
            Storage::move($file->filepath, $newPath);
            $department = null;
        }

        // Grab the filepath so we can store it in the database
        $file->filepath = $newPath;

        $file->name = $name.'.'.$extension;
        $file->department = $department;
        $file->category = $category;

        $file->save();

        return redirect('editable/templates-and-tools')->with('success', $file->name . 'has been edited successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $file = FileMetaData::find($id);

        if (Storage::delete($file->filepath)) {
            $file->delete();
        }

        return redirect('editable/templates-and-tools')->with('success', $file->name . ' has been deleted');
    }

    /**
     * Show page containing bulk upload form
     *
     * @return void
     */
    public function addBulk()
    {
        return view('editable.templates-and-tools.bulk');
    }

    /**
     * Process uploading of multiple files
     *
     * @param Request $request
     * @return void
     */
    public function bulkUpload(Request $request)
    {
        // Get every extension that we allowed
        $all_ext = implode(',', $this->allExtensions());

        $this->validate($request, [
            'files.*' => 'required|file|mimes:' . $all_ext . '|max:50000'
        ]);

        // Initialize a file upload counter
        $fileCount = 0;

        // Get the category and department from the form
        $department = $request->get('department');
        $category = $request->get('category');

        // Ensure that the request contains files
        if ($request->hasfile('files')) {
            // Loop through each file and add it to storage
            foreach ($request->file('files') as $file) {
                // Get the meta data for each file
                $name = $file->getClientOriginalName();
                $extension = strtolower($file->getClientOriginalExtension());
                $type = $this->getType($extension);
                $size = $file->getSize();

                // Upload all the given files to Storage, within a specific directory
                if ($department != '') {
                    $path = $file->storeAs('library/' . $department, $name);
                    $category = null;
                } elseif ($category != '') {
                    $path = $file->storeAs('library/' . $category, $name);
                    $department = null;
                }

                // Grab the filepath so we can store it in the database
                $file->filepath = $path;

                // Create the database entries for the newly uploaded files
                FileMetaData::firstOrCreate([
                    'name' => $name,
                    'department' => $department,
                    'category' => $category,
                    'type' => $type,
                    'extension' => $extension,
                    'size' => $size,
                    'filepath' => $path
                ]);

                $fileCount++;
            }

            return redirect('editable/templates-and-tools')->with('success', $fileCount . ' files have been added');
        }
    }

    /**
     * Get the file type based on the given extension
     * This could be used to categorise files by more generic types
     *
     * @param  string $ext Specific extension
     * @return string      Type
     */
    private function getType($ext)
    {
        if (in_array($ext, $this->image_ext)) {
            return 'image';
        }

        if (in_array($ext, $this->audio_ext)) {
            return 'audio';
        }

        if (in_array($ext, $this->video_ext)) {
            return 'video';
        }

        if (in_array($ext, $this->document_ext)) {
            return 'document';
        }
    }

    /**
     * Get all extensions by merging the individual arrays together
     * If any are added above remember to add them here
     *
     * @return array Extensions of all file types
     */
    private function allExtensions()
    {
        return array_merge($this->image_ext, $this->audio_ext, $this->video_ext, $this->document_ext);
    }
}
精简的代码段

foreach ($request->file('files') as $file) {
    // Get the meta data for each file
    $name = $file->getClientOriginalName();
    $extension = strtolower($file->getClientOriginalExtension());
    $type = $this->getType($extension);
    $size = $file->getSize();

    dd($extension);
}
当我尝试上载同一文件时,浏览器输出如下:

“每股收益”

由于脚本在获得扩展时失败了,无论如何,这清楚地表明了eps

eps文件有什么不同吗?或者我是不是太密集了

我已经测试了我允许的扩展列表中的所有其他扩展,如下所示:

/**
 * Set allowed extensions for each file category
 * This can be appended to as necessary as it's somewhat restrictive
 */
private $image_ext = [
    'jpg', 'jpeg', 'png', 'gif', 
    'ai', 'eps'
];
private $audio_ext = [
    'mp3', 'ogg', 'mpga'
];
private $video_ext = [
    'mp4', 'mpeg'
];
private $document_ext = [
    'doc', 'docx', 'dotx', 'pdf', 'odt',
    'xls', 'xlsm', 'xlsx',
    'ppt', 'pptx',
    'vsd'
];
只是澄清一下,
文件
是一个上传文件的数组,其HTML为:

<input type='file' name='files[]' multiple required />

有问题的文件


它是一个真正的
eps
文件还是只是一个重命名为
eps
的随机文件?我相信它是一个真正的eps,我会更新我的问题以显示该文件。我将从Windows文件资源管理器添加一个屏幕截图,除了文件属性之外,无需确保mimetype是正确的:)您是否可以制作一个虚拟的eps文件,我们可以使用它进行测试?我担心问题出在你身上。
<input type='file' name='files[]' multiple required />