I create image upload code. This creates a directory first and then uploads images to it.
In the form, I upload 3 images.
My Code is:
$dir_path = 'assets/images/product_images/'.$model;
mkdir($dir_path, 0777);
$i = 1 ;
foreach ($image as $new_image)
{
$dir_path_up = 'assets/images/product_images/'.$model."/";
$filename = $_FILES["$new_image"]["name"];
$image_name = $dir_path_up .$filename . $i. ".jpg";
echo $image_name;
$i++;
}
die();
Result of echo
assets/images/product_images/255_2555/1.jpg
assets/images/product_images/255_2555/2.jpg
assets/images/product_images/255_2555/3.jpg
But that images are not getting uploaded into the directory. I echo the image name which I created. It's already been renamed. Then why are images not getting uploaded into the directory?
What is wrong with this??
This work fine with multiple image upload
<?php
$j = 0; // Variable for indexing uploaded image.
$target_path = 'assets/images/product_images/' . $last_id . '/'; // Declaring Path for uploaded images.
for ( $i = 0; $i < count($_FILES['image']['name']); $i++ )
{
// Loop to get individual element from the array
$validextensions = array("jpeg", "jpg", "png"); // Extensions which are allowed.
$ext = explode('.', basename($_FILES['image']['name'][$i])); // Explode file name from dot(.)
$file_extension = end($ext); // Store extensions in the variable.
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1]; // Set the target path with a new name of image.
$j = $j + 1; // Increment the number of uploaded images according to the files in array.
if ( ($_FILES["image"]["size"][$i] < 100000000) // Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)
)
{
move_uploaded_file($_FILES['image']['tmp_name'][$i], $target_path);
}
else
{
}
}
Update, you can use
foreach ($image as $new_image)
{
$dir_path_up = 'assets/images/product_images/'.$model."/";
$filename = $_FILES["$new_image"]["name"];
$image_name = $dir_path_up .$filename . $i. ".jpg";
if (move_uploaded_file($_FILES["$new_image"]["tmp_name"], $image_name))
{
echo "The file has been uploaded.";
}
else
{
echo "There was an error uploading the file.";
}
$i++;
}