Search code examples
phplaravelgetimagesize

"getimagesize(): Filename cannot be empty" Allowing it to be empty


I'm working on a comment section where people can post comments under a post and if they want to they can also upload an image with the comment, It works when I type a comment and choose a file to upload. But not when I want to only write a comment and leave the file input empty. Then I get this error:

getimagesize(): Filename cannot be empty

Here is my Form

{!! 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() !!}

And here is my Method

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);
}

Solution

  • You just need to check that there is a file before running all the script that deals with the file.

    $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();