PHP/Laravel I can't seem to upload .GIF images -
what happens when upload regular images .jpeg, .jpg, .png files, uploads , posts on site nothing wrong. try upload .gif file. site refreshes, empties out input field, , prompts out error message saying field required (say's when input field empty).
my method:
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 image - " . $check["mime"] . "."; $uploadok = 1; } else { echo "file not 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, file large."; $uploadok = 0; } else if ($_files["filetoupload"]["size"] < 5000) { echo "sorry, file small."; $uploadok = 0; } if($imagefiletype != "jpg" && $imagefiletype != "png" && $imagefiletype != "jpeg" && $imagefiletype != "gif" ) { echo "sorry, jpg, jpeg, png & gif files allowed."; echo $imagefiletype; $uploadok = 0; } if ($uploadok == 0) { echo "sorry, file 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 error uploading file."; } } return redirect(''); echo $target_file; }
and here form:
@if ($errors->any()) <ul class="alert alert-danger"> @foreach ($errors->all() $error) <li>{{ $error }}</li> @endforeach </ul> @endif <div class="panel panel-default"> <div class="panel-heading">create 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 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() !!}
my model correct , fillable.
Comments
Post a Comment