Tags: PHP
A very simple and a very naive loop through uploaded files, which puts them in the specified directory and creates a thumbnail for each image.
foreach ($files_array as $i => $array) {
if ($files_array[$i]['error'] == 0) {
// Check size
if ($files_array[$i]['size'] > 10000000) {
$message .= "$filename exceeded file limit.";
continue;
}
// Make dir and thumb dir if they don't exist
if (!is_dir($dir)) {
mkdir($dir,0755,true);
chmod($dir,0755);
}
if (!is_dir($thumb_dir)) {
mkdir($thumb_dir,0755,true);
chmod($thumb_dir,0755);
}
$extension = pathinfo($files_array[$i]['name'], PATHINFO_EXTENSION);
/* Add the original filename to target path.
Result is "(upload folder)/filename.extension" */
$target_path = $dir . $files_array[$i]['name'];
$thumb_path = $thumb_dir . $files_array[$i]['name'];
// Move uploaded and make thumbs
if(move_uploaded_file( $files_array[$i]['tmp_name'], $target_path)) {
`convert $target_path -resize 80x50 $thumb_path`;
$message .= $files_array[$i]['name']." is uploaded.";
} else{
$message .= "Something went wrong: ".$files_array[$i]['name'];
$errors ++;
}
$uploaded++;
}
}
if (($errors == 0) && ($uploaded > 0)) {
$message .= '
'.$uploaded . ' image(s) total uploaded.';
}
**edit: use Imagick if you need something more complex. This is a really lightweight method for thumbnails only.
©2023, kirillsimin.com