-1

How can I upload the thumbnail in this code?
When I click upload, it only moves the first move_uploaded file and doesn't move the second one. Can anyone help me, please?

<?php
if(isset($_POST['submit'])){
    $cap = mysqli_real_escape_string($con, $_POST['cap']);
    $image = $_FILES['image'] ['name'];
    $tmp_name= $_FILES['image'] ['tmp_name'];
    $thumb = $_FILES['thumb'] ['name'];
    $tmp_name= $_FILES['thumb'] ['tmp_name'];
    $type = $_POST['type'];
    

    if(empty($image) or empty($thumb) or empty($cap)){
        $error = "<p style='font-seize: 20px; font-weight: bolder; color: red;'>
        Please Fill out all required stared inputs!</p>";
    }
    else{
        $pic = "INSERT INTO photos(image, thumb, caps, type)VALUES('$image', '$thumb', '$cap', '$type')";
        if(mysqli_query($con, $pic)){
            $success = "<p style='font-seize: 25px; font-weight: bolder; color: green;'>
            <i class='fa fa-smile-beam'></i> Your Data 
            Published Successfully</p>";
            move_uploaded_file($tmp_name, "img/photos/$image");
            move_uploaded_file($tmp_name, "img/thumbs/$thumb");
            
        }
        else{
            $fail = "<p style='font-seize: 20px; font-weight: bolder; color: red;'><i class='fa fa-sad-tear'>
            </i> Unsuccessful!</p>";
        }
    }
}
?>
Hakim
  • 1
  • 3

1 Answers1

0

A variable can only hold one thing. You're using $tmp_name for both the image and thumb, and after the second assignment it just has the thumb's temp name.

You need to use different variables.

<?php
if(isset($_POST['submit'])){
    $cap = mysqli_real_escape_string($con, $_POST['cap']);
    $image = $_FILES['image'] ['name'];
    $tmp_image= $_FILES['image'] ['tmp_name'];
    $thumb = $_FILES['thumb'] ['name'];
    $tmp_thumb= $_FILES['thumb'] ['tmp_name'];
    $type = $_POST['type'];
    

    if(empty($image) or empty($thumb) or empty($cap)){
        $error = "<p style='font-seize: 20px; font-weight: bolder; color: red;'>
        Please Fill out all required stared inputs!</p>";
    }
    else{
        $pic = "INSERT INTO photos(image, thumb, caps, type)VALUES('$image', '$thumb', '$cap', '$type')";
        if(mysqli_query($con, $pic)){
            $success = "<p style='font-seize: 25px; font-weight: bolder; color: green;'>
            <i class='fa fa-smile-beam'></i> Your Data 
            Published Successfully</p>";
            move_uploaded_file($tmp_image, "img/photos/$image");
            move_uploaded_file($tmp_thumb, "img/thumbs/$thumb");
            
        }
        else{
            $fail = "<p style='font-seize: 20px; font-weight: bolder; color: red;'><i class='fa fa-sad-tear'>
            </i> Unsuccessful!</p>";
        }
    }
}
?>
Barmar
  • 741,623
  • 53
  • 500
  • 612