0

in my form i have file input where user can upload image,now i want to use this input not only be able to get images but also other type of files such as zip,pdf,txt,doc and docx

Code

my current code supports for images only

//validation
"attachment" => "nullable:image",

//function
if ($request->hasFile('attachment')) {
  $attachment = $request->file('attachment');
  $filename = 'project-attachment' . '-' . time() . '.' . $attachment->getClientOriginalExtension();
  $location = public_path('images/' . $filename);
  Image::make($attachment)->resize(1300, 362)->save($location);
  $project->attachment = $filename;
}

form

{{Form::file('attachment', array('class' => 'form-control'))}}

Question

  1. How can I add my other formats support in my code?
  2. How to make size limits 3 MB?

UPDATE

based on answer below I've made:

//validation

"attachment" => "nullable|mimetypes:image/jpeg,application/pdf,image/gif,image/png,text/plain,application/msword,application/zip|max:3000",

and my function

if ($request->hasFile('attachment')) {
  $attachment = $request->file('attachment');
  $filename = 'project-attachment' . '-' . time() . '.' . $attachment->getClientOriginalExtension();
  $location = public_path('images/' . $filename);

  $valid_images = ['image/jpeg','image/gif','image/png'];
  if(in_array($_FILES['attachment']['type'], $valid_images)){
    Image::make($attachment)->resize(1300, 362)->save($location);
  }

  $project->attachment = $filename;
}

This code above does upload images and not returning error when i upload for example zip file but the issue is if file isn't image type, it will not store to the host

any idea?

1 Answer 1

1

Validation

Your Validations will probably not work because, loosely speaking mimes are not just file extension but the header information that describes the content of file or document.

The correct validation should be something like this:

"attachment" => "nullable|'mimetypes:image/jpeg,application/pdf ..'|max:3000",

You can add the rest referring here: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

As for the error about Image

You should execute the parts related to image only if the file uploaded is in fact an image. For that you should create an array of the mimes for JPG, PNG, GIF or WebP Images.

$valid_images = ['image/jpeg','',''];
if(in_array($_FILES['attachment']['type'], $valid_images)){
     //Code Related to Images
     Image::make($attachment)->resize(1300, 362)->save($location);
}

To Store Any Other Files I suppose this would be in the else part because the images are already being saved.

$attachment->storeAs($location,$filenaem);

Increase Upload Size

In the php.ini file located at etc/php/php.ini edit the

upload_max_filesize = xM
post_max_size = xM

Replace the x with the new limit you want to set. Please keep in mind that size should not be greater than that of server, else there will be a server error next.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the answer, i updated my question based on your suggestions please see the issue there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.