“getimagesize():文件名不能为空”允许它为空

时间:2015-11-09 01:04:09

标签: php laravel getimagesize

我正在撰写评论部分,人们可以在帖子下发表评论,如果他们想要,他们也可以上传带有评论的图片,当我输入评论并选择要上传的文件时,它会起作用。但是当我只想写评论并将文件输入留空时。然后我收到了这个错误:

  

getimagesize():文件名不能为空

这是我的表格

{!! Form::open(['enctype' => 'multipart/form-data']) !!}
    {!! Form::label('comment', 'Write your comment: ') !!}
    {!! Form::textarea('comment', null, ['class' => 'form-control', 'rows' => '4', 'id' => 'editor1']) !!}<br>
    {!! Form::input('file', 'fileToUpload', null, ['style' => 'overflow: hidden;', 'class' => 'btn btn-default btn-lg btn-block']) !!}
    {!! Form::submit('Post Comment', ['class' => 'btn btn-default btn-lg btn-block', 'name' => 'submit']) !!}
{!! Form::close() !!}

这是我的方法

public function comment(Requests\CreateCommentsRequest $request, $post)
{
    $posts = Posts::whereId($post)->first();

    $target_dir = "uploads/images/comments/";
    $target_file = $target_dir . preg_replace("/[^A-Za-z0-9\_\-\.]/", '', basename($_FILES['fileToUpload']["name"]));
    $uploadOk = 1;
    $findme   = ".";
    $pos = strpos($target_file, $findme);
    //echo $target_file;
    //dd($_POST);
    echo ini_get('upload_max_filesize');
    echo $_FILES["fileToUpload"]["error"];
    $imageFileType = strtolower(substr($target_file,$pos+1));
    if(isset($_POST["submit"])) {
            $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
            if($check !== false) {
                    echo "File is an image - " . $check["mime"] . ".";
                    $uploadOk = 1;
                } else {
                echo "File is not an image.";
                    $uploadOk = 1;
            }
    }
    $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"] > 500000000*8) {
            echo "Sorry, your file is too large.";
            $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.";

                    $comment = new Comments(Request::all());
                    $comment->pubslished_at = Carbon::now();
                    $comment->fileToUpload = $target_file;
                    $comment->user()->associate(Auth::user());
                    $comment->posts()->associate($posts);
                    $comment->save();

            } else {
                    echo "Sorry, there was an error uploading your file.";
            }
    }

    return redirect('posts/' . $post);
}

1 个答案:

答案 0 :(得分:1)

在运行处理该文件的所有脚本之前,您只需要检查是否有文件。

$target_file = false; // set to false because we use this later regardless
if (isset($_FILES["fileToUpload"]) && is_uploaded_file($_FILES["fileToUpload"]["tmp_name"])){
  ...
}

// the error checking will be in the above, so if we 
// get here just check that $target_file is true to add it to the comment

$comment = new Comments(Request::all());
$comment->published_at = Carbon::now(); // NB there was a typo here
if ($target_file) $comment->fileToUpload = $target_file;
$comment->user()->associate(Auth::user());
$comment->posts()->associate($posts);
$comment->save();