0

I have a simple code to upload multiple images that uploads the image to a folder and saves the path to the database. The problem I have is that the image names are will saved to the database but the images are not uploaded to the folder. Here is the code I am using. Its a free hand code( with not too much of html) so you can practically try it. My Database consists of id, image1, image2, image3.

Here is the code for upload

<?php
include'includes/db.php';
  if(isset($_POST['submit'])){

    $extension = substr($_FILES['photo1']['name'],
    strrpos($_FILES['photo1']['name'], '.'));

    $extension = substr($_FILES['photo2']['name'],
    strrpos($_FILES['photo2']['name'], '.'));

    $extension = substr($_FILES['photo3']['name'],
    strrpos($_FILES['photo3']['name'], '.'));


     $extension = strtolower($extension);
     echo $extension;

    if( $extension == ".jpg" || $extension == ".jpeg" || $extension ==  ".gif" || $extension == ".png" )
    {
        $img1=$_FILES['photo1']['name'];
        $img2=$_FILES['photo2']['name'];
        $img3=$_FILES['photo3']['name'];

        $size=$_FILES['photo']['size'];
        $type=$_FILES['photo']['type'];
        $temp=$_FILES['photo']['tmp_name'];

        $limit_size = 1024000; 
        $size_in_kb = 1024; 
        $max_size = $limit_size/$size_in_kb; 


        if($size > $limit_size)
        {
            echo "<script>location.replace('test.php?err=File size exceeds $max_size KB')</script>";    

        }
        else 
        {
            move_uploaded_file($temp,"images/".$img1);
            move_uploaded_file($temp,"images/".$img2);
            move_uploaded_file($temp,"images/".$img3);

            $sql2="INSERT INTO ad_images(image1, image2, image3)VALUES('$img1', '$img2', '$img3')";
            $res2=mysql_query($sql2);

            if($res2){
            echo "<script>location.replace('test.php?success=Product added successfuly')</script>";
            }else{
            echo "<script>location.replace('test.php?vlx=Error. Try Again...')</script>";
        }
     }
   }
 }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Script Testing</title>
</head>

<body>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
  <p> Upload Image<br />
        <input type="file" name="photo1" id="photo"><br />
        <input type="file" name="photo2" id="photo"><br />
        <input type="file" name="photo3" id="photo"><br />

    <input type="submit" name="submit" id="submit" value="Add Product" style="margin-top: 25px; margin-left: 335px;"/>
  </p>

</body>
</html>

Everything seems fine but still the images are not uploaded to the specified folder. Please help me out guys.

*****SOLVED****** SOLUTION: I just had to place this in my code.

$temp1=$_FILES['photo1']['tmp_name'];
$temp2=$_FILES['photo2']['tmp_name'];
$temp3=$_FILES['photo3']['tmp_name'];

Here is what I did...

<?php
include'includes/db.php';
  if(isset($_POST['submit'])){

    $extension = substr($_FILES['photo1']['name'],
    strrpos($_FILES['photo1']['name'], '.'));

    $extension = substr($_FILES['photo2']['name'],
    strrpos($_FILES['photo2']['name'], '.'));

    $extension = substr($_FILES['photo3']['name'],
    strrpos($_FILES['photo3']['name'], '.'));


     $extension = strtolower($extension);
     echo $extension;

    if( $extension == ".jpg" || $extension == ".jpeg" || $extension ==  ".gif" || $extension == ".png" )
    {
        $img1=$_FILES['photo1']['name'];
        $img2=$_FILES['photo2']['name'];
        $img3=$_FILES['photo3']['name'];

        $size=$_FILES['photo']['size'];
        $type=$_FILES['photo']['type'];

        $temp1=$_FILES['photo1']['tmp_name'];
        $temp2=$_FILES['photo2']['tmp_name'];
        $temp3=$_FILES['photo3']['tmp_name'];

        $limit_size = 1024000; 
        $size_in_kb = 1024; 
        $max_size = $limit_size/$size_in_kb; 


        if($size > $limit_size)
        {
            echo "<script>location.replace('test.php?err=File size exceeds $max_size KB')</script>";    

        }
        else 
        {
            move_uploaded_file($temp1,"images/".$img1);
            move_uploaded_file($temp2,"images/".$img2);
            move_uploaded_file($temp3,"images/".$img3);

            $sql2="INSERT INTO ad_images(image1, image2, image3)VALUES('$img1', '$img2', '$img3')";
            $res2=mysql_query($sql2);

            if($res2){
            echo "<script>location.replace('test.php?success=Product added successfuly')</script>";
            }else{
            echo "<script>location.replace('test.php?vlx=Error. Try Again...')</script>";
        }
     }
   }
 }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Script Testing</title>
</head>

<body>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
  <p> Upload Image<br />
        <input type="file" name="photo1" id="photo"><br />
        <input type="file" name="photo2" id="photo"><br />
        <input type="file" name="photo3" id="photo"><br />

    <input type="submit" name="submit" id="submit" value="Add Product" style="margin-top: 25px; margin-left: 335px;"/>
  </p>

</body>
</html>

now the images are uploaded well to the folder and also saved the name to the database. Thanks to Slavic for pointing it out.

Shubham Jha
  • 37
  • 1
  • 8
  • Check this out http://stackoverflow.com/questions/22957893/post-multiple-image-using-1-field-in-a-form/22958171#22958171 for multiple image upload short and sweet !! – HaRsH Feb 19 '15 at 13:29

3 Answers3

0

It seems you are attempting to move a file that was not uploaded.

<input type="file" name="photo1" id="photo"><br />
<input type="file" name="photo2" id="photo"><br />
<input type="file" name="photo3" id="photo"><br />

Then in your PHP you have:

$temp=$_FILES['photo']['tmp_name'];
// this would have to be something you have uploaded in your form:
// $_FILES['photo1']['tmp_name']; 

Usually Power Programming helps in these cases. Use a buddy (he doesn't have to be better than you at programming, but it does help if he is) and explain to him the problem as well as you can. Next thing you know - you've solved your issue.

P.S. Unrelated to your explicit problem, one should not have duplicates of id values in html. You have three ids that are duplicates.

Slavic
  • 1,891
  • 2
  • 16
  • 27
  • $temp1=$_FILES['photo1']['tmp_name']; $temp2=$_FILES['photo2']['tmp_name']; $temp3=$_FILES['photo3']['tmp_name']; – Shubham Jha Feb 19 '15 at 13:07
0

I think it's something like this:

<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"    enctype="multipart/form-data">
<p> Upload Image<br />
    <input type="file" name="photo[]" id="photo"><br />
    <input type="file" name="photo[]" id="photo"><br />
    <input type="file" name="photo[]" id="photo"><br />

<input type="submit" name="submit" id="submit" value="Add Product"  style="margin-top: 25px; margin-left: 335px;"/>
</p>

</body>
</html>

And the iterate through the array $_FILES['photo']!!

krachleur
  • 356
  • 3
  • 14
  • No man..... I just had to place this... $temp1=$_FILES['photo1']['tmp_name']; $temp2=$_FILES['photo2']['tmp_name']; $temp3=$_FILES['photo3']['tmp_name']; now the images are uploaded well.. anyways thanks buddy :) – Shubham Jha Feb 19 '15 at 13:09
0

you must define name like as name="photo[]"

should be :

<input type="file" name="photo[]" id="photo"><br />
<input type="file" name="photo[]" id="photo"><br />
<input type="file" name="photo[]" id="photo"><br />
Mustafa Toker
  • 344
  • 3
  • 13
  • No man..... I just had to place this... $temp1=$_FILES['photo1']['tmp_name']; $temp2=$_FILES['photo2']['tmp_name']; $temp3=$_FILES['photo3']['tmp_name']; now the images are uploaded well.. anyways thanks buddy :) – Shubham Jha Feb 19 '15 at 13:09