Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sammie85's avatar

Error while uploading zipped images

While uploading images in a zipped file, the controller method handling the request hits a 500 internal server error. This method works fine with zipped images of about 5 or less.

    public function imageZipUploadExtract(Request $request)
    {
        $school = School::find(auth('admin')->user()->school_id);
        $validate = Validator::make($request->all(), [
            'type' => 'required',
            'file' => 'required|mimes:zip,rar'
        ]);
        if($request->type == 'teachers'){
            $destination = 'public/images/passports/teachers/'.$school->name.'-'.$school->id.'/';
        }elseif($request->type == 'students'){
            $destination = 'public/images/passports/students/'.$school->name.'-'.$school->id.'/';
        }else{
            $destination = 'public/uploads/passportUploadTypeError';
        }
        $file = $request->file('file');
        if($file){
            $upError = '';
            if($validate->passes()){
                $file = $request->file;
                $filename = 'zip-'.time() . '.' .$file->getClientOriginalExtension(); 
                if($request->file->move('public/uploads/zips', $filename)){
                    $file = 'uploads/zips/'.$filename;
                    $Path = public_path($file);
                    $zip = zip_open($Path);
                    if ($zip)
                    {
                        //extract
                        Zipper::make($Path)->extractTo($destination);
                        while ($zip_entry = zip_read($zip))
                        {
                            //store each of the photos to db
                            if($request->type == 'students'){
                                $pasport = '/images/passports/students/'.$school->name.'-'.$school->id.'/';
                                $regnum = explode('.', zip_entry_name($zip_entry));
                                if(Student::where('regnum', $regnum[0])->update([
                                    'passport' => $pasport.zip_entry_name($zip_entry)
                                ])){
                                }else{
                                    $upError .= 'Passports stored successfully. But no user found for student with reg-no <strong>"'.$regnum[0].'"</strong><br>';
                                }
                            }else{
                                $pasport = '/images/passports/teacher/'.$school->name.'-'.$school->id.'/';
                                $regnum = explode('.', zip_entry_name($zip_entry));
                                $itemPic = str_replace('-', '/', $regnum[0]);
                                if(Teacher::where('staff_no', $itemPic)->update([
                                    'passport' => $pasport.zip_entry_name($zip_entry)
                                ])){
                                }else{
                                    $upError .= 'Passports stored successfuly. But no user found for teacher with staff-no <strong>"'.$regnum[0].'"</strong><br>';
                                }
                            }
                        }
                        zip_close($zip);
                        unlink(public_path($file));
                        if(empty($upError)){
                            flash('Photos uploaded successfully')->success();
                            return redirect()->back();
                        }else{
                            flash($upError)->warning();
                            flash('Photos uploaded successfully')->success();
                            return redirect()->back();
                        }
                    }
                }else{
                    flash("Couldn't move file")->error();
                    return redirect()->back();
                }
            }else{
                flash('Please select a zip file')->error();
                return redirect()->back();
            }
        }else{
            flash('Please upload file')->error();
            return redirect()->back();
        }
    } 

Below is the server php ini config

allow_url_fopen = Off
allow_url_include = Off
display_errors = Off
enable_dl = Off
file_uploads = On
max_execution_time = 240
max_input_time = 240
max_input_vars = 1000
memory_limit = 4096M
post_max_size = 500M
session.gc_maxlifetime = 1440
session.save_path = "/var/cpanel/php/sessions/ea-php70"
upload_max_filesize = 500M
zlib.output_compression = Off
asp_tags = Off

Important facts:

  1. Currently, this method above works fine if the images enclosed in the zip upload are less than or equal to 5 images. But once the images in the zip exceed 7 or more, it hits the 500 error
  2. Interesting thing about this error is that I had no such issues while I was on the previous VPS server before being migrated to a bigger VPS plan by the host provider
  3. Prior to my migration, this exact method handles request with zipped images of over 300 at a go without any issue successfully
  4. It is also important to state that several other methods that handles data upload such as excel also encounter the error.

Being pulling my hair out trying to figure out what is missing on my new server.

Will be really pleased to have someone help look at this and proffer a solution. Thanks!

0 likes
3 replies
Snapey's avatar

Some error message and affected line would help ?

sammie85's avatar

The only error messages received has been:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

The error_log file doesn't log any error regarding this. So it makes it pretty tough for me...

mushood's avatar

try to check the error_log in public_html/error_log. If not present, try to look for error logger in cPanel. Usually error 500 are logged somewhere. Maybe you need to clear your tmp file. Or allow more size in your tmp file

Please or to participate in this conversation.