I am willing to upload multiple image but having strange problem.. I am using this simple code...
<?php
require("query/config.php");
$p_id=$_POST['place_id'];
$file=$_FILES["image"]["name"];
print_r($file);
echo "hghghg";
foreach($file as $key=>$val)
{
move_uploaded_file($_FILES["image"]["tmp_name"],"/upload/" . $val);
$query=mysqli_query($con,"insert into city_gallery set place_id='$p_id',image='$val'") or die("Alert! Query failed.");
}
this is code not working and not displaying anything on upload.php page if i dont use foreach loop... for example if i have only this code on upload.php.. it display...
<?php
require("query/config.php");
$p_id=$_POST['place_id'];
$file=$_FILES["image"]["name"];
print_r($file);
echo "hghghg";
here output is...Array ( [0] => 86662.jpg [1] => beautiful-girl-baby-hd-images.jpg [2] => [3] => ) hghghg
so its mean the array of image is present on this page...but when i use the top above code with foreach
it display nothing on this page and also not moving image in upload folder and not the image name is going in database,
here is my form..
<form action="upload.php" method="post" enctype="multipart/form-data">
<?php
include("query/config.php");
$query=mysqli_query($con,"select * from cite_guide order by id desc");
?>
<select id="place_id" name="place_id" >
<option value="0">Select Place</option>
<?php
while($row=mysqli_fetch_array($query))
{
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['place']; ?></option>
<?php
}
?>
</select>
<fieldset>Image 1
<input type="file" class="input-medium" name="image[]" />
</fieldset>
<fieldset>Image 2
<input type="file" class="input-medium" name="image[]" />
</fieldset>
<fieldset>Image 3
<input type="file" class="input-medium" name="image[]" />
</fieldset>
<fieldset>Image 4
<input type="file" class="input-medium" name="image[]" /> <input class="submit-green" type="submit" value="Upload" />
</fieldset>
</form>
Now image name is going into database but image not moving to upload foldar...for image i corrected this
move_uploaded_file($_FILES["file"]["tmp_name"],"/upload/" . $val);
to
move_uploaded_file($_FILES["image"]["tmp_name"],"/upload/" . $val);
now only image is not moving...why????
any idea why this is happening ...please suggest...Thanks
Thanks for your time, it was a problem of looping...I replace foreach
loop to for
and my script is running now, the main problem i faced with foreach loop to traversing $_FILES
array, And like temp_name
contain the temprory name for each file. So i made some thing like this..
I changed this code: From
foreach($file as $key=>$val)
{
move_uploaded_file($_FILES["image"]["tmp_name"],"/upload/" . $val);
$query=mysqli_query($con,"insert into city_gallery set place_id='$p_id',image='$val'") or die("Alert! Query failed.");
}
To
for($i=0;$i<sizeof($file);$i++)
{
$file=$_FILES["image"]["name"][$i];
move_uploaded_file($_FILES["image"]["tmp_name"][$i],"/upload/" .$file);
$query=mysqli_query($con,"insert into city_gallery set place_id='$p_id',image='$val'") or die("Alert! Query failed.");
}