I a building a uploader and need some help if possible. I have an uploader that a person can add as many pictures as they want to the multi uploader with a javascript field adder.
I want echo all the images he uploaded as text, but using $fileName[0]
, $fileName[1]
, $fileName[2]
i dont think will work because its not certain how many they will upload. Is there an array I can use to implode or join all arrays to show into one.
Here is my code.
<?php
if (isset($_POST['addpart'])) {
$image = $_FILES['images']['tmp_name'];
$name = $_POST['username'];
$name = str_replace(' ', '_', $name);
$i = 0;
$i2 = 1;
$id = uniqid();
foreach ($image as $key) {
$fileData = pathinfo(basename($_FILES["images"]["name"][$i]));
$fileName[] = $name . '_' . $id . '_' . $i2 . '.' . $fileData['name'] . 'jpg';
move_uploaded_file($key, "image/" . end($fileName));
//copy("image/" . end($fileName) , "image_thumbnail/" . end($fileName));
$images = "image/" . end($fileName);
$new_images = "image_thumbnail/" . end($fileName);
$width = 100; //*** Fix Width & Heigh (Autu caculate) ***//
$size = GetimageSize($images);
$height = round($width * $size[1] / $size[0]);
switch ($size[2]) {
case IMAGETYPE_GIF:
$images_orig = imagecreatefromgif($images);
break;
case IMAGETYPE_JPEG:
$images_orig = imagecreatefromjpeg($images);
break;
case IMAGETYPE_PNG:
$images_orig = imagecreatefrompng($images);
break;
default:
die("Unknown filetype");
}
//$images_orig = imagecreatefromjpeg($images);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width + 1, $height + 1, $photoX, $photoY);
ImageJPEG($images_fin, $new_images);
$i++;
$i2++;
}
echo 'Uploaded<br />';
echo 'Main Image - <a href="image/' . $fileName[0] . '">' . $fileName[0] . '</a><br />';
echo 'Extra Image 1 - <a href="image/' . $fileName[1] . '">' . $fileName[1] . '</a><br />';
echo 'Extra Image 2 - <a href="image/' . $fileName[2] . '">' . $fileName[2] . '</a><br />';
echo '<hr>';
}
?>
I hope what i am asking is clear.
You can use a simple foreach loop and cycle through all the items in
$fileName
In this example, what you refer to as
$fileName[0]
,$fileName[1]
, ..., in your question is simply called$file
. The foreach loop will take care of counting the items inside of$fileName
, so that you don't necessarily have to know it.Because of this, I might suggest you to rename
$fileName
to$fileNames
(since it is an array) and write the foreach like this:($fileNames as $fileName)
.For other purposes, if you need to know how many items there are in an array you can use count().