Upload multiple images

Shubham Jha

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.

Slavic

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Upload multiple images with codeigniter

From Dev

Upload multiple images using AFNetworking

From Dev

Upload multiple images to Database MYSQLI

From Dev

Upload multiple images in one request

From Dev

Multiple Upload Images into Mysql Database

From Dev

upload Multiple images to desired folder

From Dev

Upload multiple images with titles and descriptions

From Dev

Upload multiple images to Database MYSQLI

From Dev

Validate multiple upload images with django

From Dev

Multiple Images upload with different titles

From Dev

Unable to upload multiple images with Carrierwave

From Dev

All images not uploading in multiple images upload

From Dev

Upload multiple images to a database from a singe fileinput

From Dev

Upload multiple images to server from Android app

From Dev

Proper way to upload multiple images to Parse

From Dev

Upload multiple images using AFNetworking in swift

From Dev

Upload multiple images parse.com javascript

From Dev

how to upload multiple images to a blog post in django

From Dev

Upload multiple featured images in a custom post (Wordpress)

From Dev

upload and display multiple images for one product?

From Dev

Upload multiple images to server from Android app

From Dev

Upload multiple images to a database from a singe fileinput

From Dev

Upload multiple images angular - mean stack

From Dev

FileNotFoundException when upload multiple images to FTP server

From Dev

Upload multiple images from Android to PHP

From Dev

trying to upload multiple images to database not working

From Dev

How to upload multiple images using codeigniter?

From Dev

Incorrect upload multiple images in Edit method

From Dev

HTML5 File API Upload Multiple Images with Ajax

Related Related

HotTag

Archive