I have a module on my website that the people that created it no longer exist as company and I need to modify a couple things.
This module, among other things, uploads images to the hosting.
I managed to update text, images and even some html/css. But I am no programmer and this PHP bit is out of my area. Could this particular code be modified so the image that is on “the pipe” could be resized or changed it’s resolution before uploading it? Or should it be done after uploading it, resized and then copied with the same name?
I found a solution on this website, but in my case the $_FILES field contains 4 braketed fields, it’s a bigger array? I don’t know how to adapt that solution to my $_FILES field.
$_FILES[‘thefilesinput’][‘name’][$key][‘file’])
Thank you very much to anyone who can help me.
I strongly believe this is the part of the code that uploads said images (below code).
foreach ($_POST['thefilesinput'] as $key => $input) {
$categoria = $input['categoria'] ?? '';
$texto = $input['texto'] ?? '';
if (empty($categoria) ) continue;
$categoryKey = array_search($categoria, array_column($categories_info, 'categoria'));
if ($categoryKey === false) {
$categories_info[] = [
'categoria' => $categoria,
'texto' => $texto,
'fotos' => []
];
$categoryKey = count($categories_info) - 1;
} else {
$categories_info[$categoryKey]['texto'] = $texto;
}
if (isset($_FILES['thefilesinput']['name'][$key]['file'])) {
$num_files = count($_FILES['thefilesinput']['name'][$key]['file']);
for ($i = 0; $i < $num_files; $i++) {
if ($_FILES['thefilesinput']['error'][$key]['file'][$i] === UPLOAD_ERR_OK) {
$file_tmp_dir_gallery = $_FILES['thefilesinput']['tmp_name'][$key]['file'][$i];
$file_name_dir_gallery = $second_number . '_' . date('dmYHi') . '_' . basename($_FILES['thefilesinput']['name'][$key]['file'][$i]);
if (move_uploaded_file($file_tmp_dir_gallery, $upload_dir_gallery . $file_name_dir_gallery)) {
$categories_info[$categoryKey]['fotos'][] = $file_name_dir_gallery;
} else {
echo "Error";
}
}
}
}
}
I found similar solutions to this problem, but was unable to implement it correctly, the server would crash, I assume my code is not correct.
php resize image on upload
In the case from above, the $_FILES field contains only two bracketed fields, I don’t know how to manage that with my code above. $_FILES[‘photo’][‘tmp_name’]