PHP / Laravel我似乎无法上传.GIF图片

时间:2015-10-22 14:36:15

标签: php image laravel upload gif

当我上传常规图像(如.jpeg,.jpg,.png文件)时,会发生在我网站上的所有上传和帖子,就像没有错。但是一旦我尝试上传.gif文件。该站点刷新,清空我的输入字段,然后提示输出一个错误消息,说明该字段是必需的(当输入字段为空时说。)

我的方法:

public function upload(Requests\CreatePostsRequest $request)
    {
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES['fileToUpload']["name"]);
        $uploadOk = 1;
        $findme   = ".";
        $pos = strpos($target_file, $findme);
        //dd($_POST);
        $imageFileType = strtolower(substr($target_file,$pos+1));
        if(isset($_POST["submit"])) {
            $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
                //dd($check);
            if($check !== false) {
                echo "File is an image - " . $check["mime"] . ".";
                $uploadOk = 1;
            } else {
                echo "File is not an image.";
                $uploadOk = 0;
            }
        }
        $filecheck = 0;
        $orgFileName = $target_file;
        while (file_exists($target_file)) {
            $target_file = substr($orgFileName,0,$pos).$filecheck.substr($orgFileName,$pos);
            $filecheck++;

            $uploadOk = 1;
        }

        if ($_FILES["fileToUpload"]["size"] > 500000) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        } else if ($_FILES["fileToUpload"]["size"] < 5000) {
            echo "Sorry, your file is too small.";
            $uploadOk = 0;
        }

        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        && $imageFileType != "gif" ) {
            echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                echo $imageFileType;
            $uploadOk = 0;
        }

        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";

        } else {
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

                        $post = new Posts($request->all());
                        $post->fileToUpload = $target_file;
                        Auth::user()->posts()->save($post);

                        //$request = Request::all();
                        //$post = new Posts();
                        //$post->title = $request['title'];
                        //$post->user_id = Auth::user()->posts();
                        //$post->src = $target_file;
                        //$post->save();
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }
        return redirect('');
        echo $target_file;
    }

这是我的表格:

@if ($errors->any())
    <ul class="alert alert-danger">
      @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
      @endforeach
    </ul>
    @endif
        <div class="panel panel-default">
            <div class="panel-heading">Create a Post</div>
        <div class="panel-body">
            {!! Form::open(['url' => 'upload', 'enctype' => 'multipart/form-data']) !!}
                <div class="form-group">
                {!! Form::label('title', 'Title: ') !!}
                {!! Form::text('title', null, ['class' => 'form-control']) !!}
                </div>
                <div class="from-group">
                {!! Form::label('body', 'Select image to upload: ') !!}
                {!! Form::input('file', 'fileToUpload', null, ['class' => 'btn btn-default btn-lg btn-block', 'id' => 'fileToUpload']) !!}
                {!! Form::submit('Upload Image', ['class' => 'btn btn-default btn-lg btn-block', 'name' => 'submit', 'accept' => 'image/gif']) !!}
                </div>
            {!! Form::close() !!}

我的模型是正确的,一切都可以填写。

0 个答案:

没有答案