Search code examples
phpmysqlfile-uploadimage-uploading

i can not upload image php everything is ok but it failes uploading


everything is ok i can seperately insert query into my database but it does not move the uploaded file here is my code. see please if you can find any issue below.

it checked my image folder directory it is correct the directory takes me to the exact folder. upload_file is also on in php.ini
but still it is not working.

<?php

$conn = mysqli_connect("localhost", "root", "HERE_IS_MYCORRECT_PASSWORD", "HERE_IS_MYCORRECT_DTABASE_NAME");
if($conn) {

echo "<p style='color:green;'>connected</p>";
}
else {
    echo " connection failed";
}
if(isset($_POST['uploadfilesub'])){
    $id = $_POST['id'];
    $filename = $_FILES['uploadfile']['name'];
    $filetmpname = $_FILES['uploadfile']['tmp_name'];
    $folder = './Resources/style/images/' . $filename;
    echo "<p>".$filename."</p>";
    echo "<p>".$filetmpname."</p>";
    $sql = "INSERT INTO product_images VALUES ($id,'$filename') ";
    if(move_uploaded_file($filetmpname , $folder)){
         echo "<p color='green'>moved</p>";
         $qry = mysqli_query($conn, $sql);
         if($qry){
            echo "<p color='green'>Inserted into mysql</p>";
        } else {
            echo "<p color='red'>Failed to Insert</p>";
        }
     }
     else {
         echo "<p style='color:red;'>Failed to move</p>";
    }

}
?>

 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <meta http-equiv="X-UA-Compatible" content="ie=edge">
     <title>Document</title>
 </head>
 <body>
     <form action="" method="post" enctype="multipart/form-data">
        <input type="text" name="id">
        <input type="file" name="uploadfile" />
        <input type="submit" name="uploadfilesub" value="upload" />
     </form>
 </body>
 </html>

Solution

  • As far as I can see your error (in regards to the particular, mentioned, issue) is very simple:

    $folder = './Resources/style/images/';
    

    change to:

    $folder = './Resources/style/images/' . $filename;
    

    Simply move_uploaded_file expects full target path and not only target folder.