Hi, I coded a form which works perfectly to upload a single image to remote server and can randomly rename the image. The information also be stored into database. But while I try to upload multiple images at a time, it occurs an error and I can't upload anything, even no information inserts into database. Database: Table - snapshots Fields - snap_id, cat_id, snapshots uploader.php <?php function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } if(isset($_POST['Submit'])) { $catname=$_POST['selectCategory']; $image=$_FILES['image']['name']; $random=rand(000000,999999); if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //we will give an unique name, for example the time in unix time format $image_name=$random.'.'.$extension; //the new name will be containing the full path where will be stored $newname="images/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); } $sql ="INSERT INTO snapshots(snap_id, cat_id, snapshots) VALUES('', '$catname', '$image_name')"; if(mysql_query($sql)) { $msg = "<div class='message'>Images successfully uploaded.</div>"; } else { $msg = "<div class='message'> Failed to upload the images.</div>"; } } ?> <form name="frmPic" action="<?php echo $_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data"> Select Vehicle Name : <br /> <select name="selectCategory"> <option value="" selected>< Select One ></option> <option value="Flowers">Flowers</option> <option value="Fruits">Fruits</option> </select> Snapshot:<br /> <input type="file" name="image[]" size="43" /><br /> <input type="file" name="image[]" size="43" /><br /> <input type="file" name="image[]" size="43" /><br /> <input type="file" name="image[]" size="43" /><br /> <input type="file" name="image[]" size="43" /> <input type="submit" name="Submit" value="Upload Photo" /> </form> ====================================================== I hope, I could make you clear the problem. Can anybody help me to solve it and I just want to upload multiple images with rename at a time and stored into database. Thanks in advance.
What is the error? where does the error start start with error_reporting(E_ALL) at the top of your script!
I tried to insert 5 images, but only 1 inserted into database without image extension like as (000000.), surprised no extension. but no image uploads into images directory. Error says, 1. Warning: stripslashes() expects parameter 1 to be string, array given in D:\xampp\htdocs\dolphin\uploader.php on line 19 I mean, errors in $filename = stripslashes($_FILES['image']['name']); 2. Warning: copy() expects parameter 1 to be string, array given in D:\xampp\htdocs\dolphin\uploader.php on line 28 I mean, errors in $copied = copy($_FILES['image']['tmp_name'], $newname);
you have to do a loop like for 5 images for($i =0; $i<=5;$i++) { $image=$_FILES['image']['name'][$i]; $image_name=$random.'.'.$extension; //the new name will be containing the full path where will be stored $newname="images/".$image_name; $copied = copy($_FILES['image']['tmp_name'][$i], $newname); //add your database here }
I do set a loop as your instruction and 5 images upload into directory successfully. But only 1 record saves into database. I wrote the code here- uploader.php <?php function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } if(isset($_POST['Submit'])) { $catname=$_POST['selectCategory']; for($i =0; $i<=5;$i++) { $image=$_FILES['image']['name'][$i]; $random=rand(000000,999999); if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name'][$i]); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //we will give an unique name, for example the time in unix time format $image_name=$random.'.'.$extension; //the new name will be containing the full path where will be stored $newname="images/".$image_name; $copied = copy($_FILES['image']['tmp_name'][$i], $newname); } } $sql ="INSERT INTO snapshots(snap_id, cat_id, snapshots) VALUES('', '$catname', '$image_name')"; if(mysql_query($sql)) { $msg = "<div class='message'>Images successfully uploaded.</div>"; } else { $msg = "<div class='message'> Failed to upload the images.</div>"; } } ?>
try this <?php function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } if(isset($_POST['Submit'])) { $catname=$_POST['selectCategory']; for($i =0; $i<=5;$i++) { $image=$_FILES['image']['name'][$i]; $random=rand(000000,999999); if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name'][$i]); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //we will give an unique name, for example the time in unix time format $image_name=$random.'.'.$extension; //the new name will be containing the full path where will be stored $newname="images/".$image_name; $copied = copy($_FILES['image']['tmp_name'][$i], $newname); $sql ="INSERT INTO snapshots(snap_id, cat_id, snapshots) VALUES('', '$catname', '$image_name')"; if(mysql_query($sql)) { $msg = "<div class='message'>Images successfully uploaded.</div>"; } else { $msg = "<div class='message'> Failed to upload the images.</div>"; } } } } ?> PHP: