-2

Main issue is image file is not moving from temp location to new location. But it is not giving any error. And all mysql queries are working. Al the html part also working.

$newFileName = $_POST['filename'];
$imageTitle = $_POST['filetitle'];
$imageDesc = $_POST['filedesc'];
$file = $_FILES['file'];
$fileName = $file['name'];
$fileType = $file['type'];
$fileTempName = $file['temp_name'];
$fileError = $file['error'];
$fileSize = $file['size'];
$fileExt = explode(".",$fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array("jpg","jpeg","png");

if(in_array($fileActualExt,$allowed)){
  if($fileError === 0){
    if($fileSize < 20000000){
      $imageFullName = $newFileName . "." . uniqid("",true) . "." . $fileActualExt;
      $fileDestination = "../gallery/" . $imageFullName;
      include_once 'dbh.inc.php';
      if(!empty($imageTitle) || !empty($imageDesc)){
        $sqlSelect = "SELECT * FROM gallery;";
        $stmt = mysqli_stmt_init($conn);
        if(mysqli_stmt_prepare($stmt,$sqlSelect)){
          mysqli_stmt_execute($stmt);
          $result = mysqli_stmt_get_result($stmt);
          $rowCount = mysqli_num_rows($result);
          $setImageOrder = $rowCount+1;
          $sqlInsert = "INSERT INTO gallery(title,description,imgfullname,ordergallery) VALUES(?,?,?,?);";
          if(mysqli_stmt_prepare($stmt,$sqlInsert)){

            mysqli_stmt_bind_param($stmt,"ssss", $imageTitle,$imageDesc,$imageFullName,$setImageOrder);
            mysqli_stmt_execute($stmt);
            move_uploaded_file($fileTempName, $fileDestination);
          }
        }
      }
    }
}

}

wahsandaruwan
  • 587
  • 5
  • 7
  • 2
    Make sure you have error reporting enabled. https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – Barmar May 06 '19 at 16:17
  • If the functions don't work properly for an reason. I suggest use copy(); function to upload directory then use @unlink(); to delete the original image. – Creative87 May 06 '19 at 16:28

2 Answers2

0

Your $imageTitle and $imageDesc variables are empty in this code since they are never given a value. Therefore it will not enter the if statement and move the files.

0

You've probably been downvoted here because your description of what the code actually does is somewhat vague. You also do not appear to have tested or published a MCVE. Why, for instance, do you SELECT * FROM gallery; when all you use the data for is to display a count (which is irrelevant to the problem you describe).

You're handling of the filename is sensibly done, although ideally the path should be outwith the document root (and subsequent read access mediated by a script).

That "all mysql queries are working" suggests that the execution thread is reaching move_uploaded_file(), we shouldn't be guessing how the code works / what diagnostics you may have run. Each of those "if" statements should have an "else". Each of the mysqli_*() calls should check the return value. You should be checking the return value of move_uploaded_file(). You should also be checking your log file for errors and warnings (after verifying that the logging mechanism is working as expected).

From a cursory glance through the code (I imagine that the relevant POST vars are populated) the next place I would be looking (after the return values and logs) is at the permissions on the target directory.

symcbean
  • 47,736
  • 6
  • 59
  • 94