Reputation: 1103
I've trying to put one more field in upload form to be able to upload 3 images in one form. The problem that I have is that two of the images must save different path in DB from third image.
For example image_1
and image_2
are saved to http://example.com/img/
and third image into ../images/
Currently when I fill fields with all 3 images in database I got saved this name for them - 4649d81924f1b17db1444d72ee271f6c-Array
This is the html form
<form role="form" action="" method="post" enctype="multipart/form-data">
<!-- File Button -->
<div class="form-group">
<label for="image">image</label>
<input value="<?php echo !empty($image)?$image:'';?>" id="image" name="file[]" multiple="multiple" class="input-file" type="file">
</div>
<!-- File Button -->
<div class="form-group">
<label for="rest_img_big">Image 2</label>
<input value="<?php echo !empty($rest_img_big)?$rest_img_big:'';?>" id="rest_img_big" name="file[]" multiple="multiple" class="input-file" type="file">
</div>
<!-- File Button -->
<div class="form-group">
<label for="web_image">Image 3</label>
<input value="<?php echo !empty($web_image)?$web_image:'';?>" id="web_image" name="file[]" multiple="multiple" class="input-file" type="file">
</div>
</form>
and this is the php part for image upload
if ( !empty($_POST) && isSet($_POST['submit']))
{
define('MAX_FILE_SIZE', 20000000430);
$permitted = array('image/jpeg', 'image/jpeg', 'image/png', 'image/gif');
// keep track post values
$fileName = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
// make a new image name
$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time()) . '-' .$fileName;
// save image path
$path = "../../img/".$randName;
$forDB = "http://example/img/".$randName;
if (in_array($fileType, $permitted))
{
$result = move_uploaded_file($tmpName, $path);
if (!$result)
{
echo "Error uploading image file";
exit;
}
}
// update data
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE images set image = ?, rest_img_big = ?, web_image = ? WHERE user_id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($forDB,$forDB,$path,$user_id));
So any suggestion how can I fix this issue and to be able to upload 3 images at the same time?
UPDATE: Current code
if ( !empty($_POST) && isSet($_POST['submit']))
{
define('MAX_FILE_SIZE', 20000000430);
$permitted = array('image/jpeg', 'image/jpeg', 'image/png', 'image/gif');
// keep track post values
for($i=0; $i < count($_FILES['file']['name']); $i++){
$fileName = $i.$_FILES['file']['name'][$i]; //get unique name
$tmpName = $_FILES['file']['tmp_name'][$i];
$fileSize = $_FILES['file']['size'][$i];
$fileType = $_FILES['file']['type'][$i];
// make a new image name
$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time()) . '-' .$fileName;
// save image path
$web = "../../img/".$randName;
$smallImageForApp = "http://example/img/".$randName;
$bigImageForApp = "http://example/img/".$randName;
if (in_array($fileType, $permitted))
{
$result = move_uploaded_file($tmpName, $web);
if (!$result)
{
echo "Error uploading image file";
exit;
}
}
else {
// update data
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE images set image = ?, rest_img_big = ?, web_image = ? WHERE user_id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($smallImageForApp,$bigImageForApp,$web,$user_id));
}
UPDATE 2: var_dump($_FILES);
array(1) {
["file"]=> array(5)
{
["name"]=> array(3)
{
[0]=> string(21) "23.jpg_1418028921.jpg"
[1]=> string(15) "_1418028891.jpg"
[2]=> string(6) "78.jpg"
}
["type"]=> array(3)
{
[0]=> string(10) "image/jpeg"
[1]=> string(10) "image/jpeg"
[2]=> string(10) "image/jpeg"
}
["tmp_name"]=> array(3)
{
[0]=> string(14) "/tmp/phpfqqJG3"
[1]=> string(14) "/tmp/phptp9zcF"
[2]=> string(14) "/tmp/phpTkmuIg"
}
["error"]=> array(3)
{
[0]=> int(0)
[1]=> int(0)
[2]=> int(0)
}
["size"]=> array(3)
{
[0]=> int(25878)
[1]=> int(25878)
[2]=> int(35864)
}
}
}
array(1)
{
//same thing 3 times
}
Upvotes: 0
Views: 519
Reputation: 3520
Your current code will only upload single image, for 3 you need to loop over $_FILES['file']
and upload it, here is how a way to go
for($i=0; $i < count($_FILES['file']['name']); $i++){
$fileName = $i.$_FILES['file']['name'][$i]; //get unique name
$tmpName = $_FILES['file']['tmp_name'][$i];
$fileSize = $_FILES['file']['size'][$i];
$fileType = $_FILES['file']['type'][$i];
// make a new image name
$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time()) . '-' .$fileName;
// save image path
//check if its 3rd image change the path to '../images/' folder
$path = ($ === 2) ? '../images/' : "../../img/".$randName;
$forDB = "http://example/img/".$randName;
if (in_array($fileType, $permitted))
{
$result = move_uploaded_file($tmpName, $path);
if (!$result)
{
echo "Error uploading image file";
exit;
} else {
// update data in db here, only on upload
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE images set image = ?, rest_img_big = ?, web_image = ? WHERE user_id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($forDB,$forDB,$path,$user_id));
}
}
}
your $fileName
is messed up 4649d81924f1b17db1444d72ee271f6c-Array
because you are appending an array
to a string, last Array
is showing that
Note: don't store full url for images in db $forDB = "http://example/img/".$randName;
you should be storing only the name $forDB = $randName;
that way you can move your application from domain to domain but images will not broken.
Upvotes: 1
Reputation: 1193
You need upload file one by one through a loop
for($i=0; $i < count($_FILES['file']['name']); $i++){
$fileName = $_FILES['file']['name'][$i];
$tmpName = $_FILES['file']['tmp_name'][$i];
$fileSize = $_FILES['file']['size'][$i];
$fileType = $_FILES['file']['type'][$i];
// more stuff . . . . . .
}
Upvotes: 1