Some error message and affected line would help ?
May 21, 2019
3
Level 1
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:
- 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
- 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
- Prior to my migration, this exact method handles request with zipped images of over 300 at a go without any issue successfully
- 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!
Please or to participate in this conversation.