Form:
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" multiple>
<input type="hidden" value="GALLUPLOAD" name="DO">
<input type="submit" value="Upload Gallery Photos">
</form>
Script:
echo "<pre>";
print_r($_FILES);
echo "</pre>";
Result
Array
(
[file] => Array
(
[name] => 9.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phphhER8w
[error] => 0
[size] => 269108
)
)
Files was selected as:
"9.jpg" "0.jpg" "1.JPG" "2.jpg" "3.JPG" "4.JPG" "5.jpg" "6.jpg" "7.jpg" "8.JPG"
So only first file go into array.
Getting PHP Warning:
foreach() argument must be of type array|object, string given in
foreach ($_FILES['file']['tmp_name'] as $key => $tmpName )
Full code, it works for single file upload, but stop working when i add foreach (
switch ($_POST['DO'])
{
case "MAINUPLOAD":
$relp="";
$maxsize=800;
$fn='/0.jpg';
break;
case "GALLUPLOAD":
$relp='/gallery';
$maxsize=1600;
$fn='/' . time() . '.jpg';
break;
}
if(isset($_FILES['file'])) {
$DirDestination = getcwd() . substr($user['PHOTODIR'], 2);
echo "<BR> DirDestination ".$DirDestination;
if (!file_exists($DirDestination))
mkdir($DirDestination, 0777, true);
if (!file_exists($DirDestination . '/gallery'))
mkdir($DirDestination . '/gallery', 0777, true);
$DirDestination = $DirDestination . $relp;
echo "<BR> DirDestination ".$DirDestination;
foreach ($_FILES['file']['tmp_name'] as $key => $tmpName )
{
echo "<BR> XXX";
if( !empty( $_FILES[ 'file' ][ 'error' ][ $key ] ) )
{
echo "<BR> err ".$_FILES[ 'file' ][ 'error' ];
// return false; // return false also immediately perhaps??
}
$check=can_upload($tmpName);
echo "<BR> check ".$check;
if ($check === true) {
echo "<BR> check ".$check;
$fileTempName = $_FILES['file']['tmp_name'][$key];
try {
$img = new Imagick($fileTempName);
$img->stripImage(); //"Removed EXIF data
if ($img->getImageWidth() > $img->getImageHeight()) {
$newWidth = $maxsize;
$newHeight = $maxsize * $img->getImageHeight() / $img->getImageWidth();
} else {
$newHeight = $maxsize;
$newWidth = $maxsize * $img->getImageWidth() / $img->getImageHeight();
}
$img->resizeImage($newWidth, $newHeight, imagick::FILTER_CATROM, 1);
$tile = new Imagick();
$tile->newImage($img->getImageWidth(), $img->getImageHeight(), 'transparent', 'png');
$watermarkx = new Imagick(getcwd() . '/cats/fflogo.png');
$watermarkx->resizeImage($newWidth / 2, $newWidth * $watermarkx->getImageHeight() / $watermarkx->getImageWidth() / 2, imagick::FILTER_CATROM, 1);
$img->compositeImage($watermarkx, imagick::COMPOSITE_ATOP, $newWidth - $watermarkx->getImageWidth(), $newHeight - $watermarkx->getImageHeight());
$img->writeImage($DirDestination .$fn);
$img->clear();
$img->destroy();
} catch (Exception $e) {
echo 'Exception caught: ', $e->getMessage(), "n";
}
}
else
{
echo "<strong>$check</strong>";
}
}
}