0

I currently created a system that allowed a user to upload a photo only. The photo the already upload can be replaced if the user want to change it.

The current code shows that there's no error in this function. The URL updated at MySQL database. But the problem is the image doesn't update. Below is my current code:

update_photo_before.php

<?php

    require_once '../../../../config/configPDO.php';

    $report_id = $_POST['report_id'];
    $last_insert_id = null;

    //Allowed file type
    $allowed_extensions = array("jpg","jpeg","png");

    //File extension
    $ext = strtolower(pathinfo($_FILES['uploadFile']['name'], PATHINFO_EXTENSION));

    //Check extension
    if(in_array($ext, $allowed_extensions)) {

        $defID = "before_" . $report_id;
        $imgPath = "images/$defID.png";
        $ServerURL = "http://172.20.0.45/tgotworker_testing/android/$imgPath";

        $query = "UPDATE ot_report SET photo_before = '$ServerURL', time_photo_before = GETDATE() WHERE report_id = :report_id";
        $sql = $conn->prepare($query);
        $sql->bindParam(':report_id', $report_id);
        $sql->execute();

        if ($sql){

            move_uploaded_file($_FILES['uploadFile']['name'], $imgPath); //line 28
            echo "<script>alert('Saved')</script>";
            header("Location: view_task.php?report_id=".$_POST['report_id']);

        }else{
            echo "Error!! Not Saved";
        }


    } else {
        echo "<script>alert('File not allowed')</script>";
        header("Location: view_task.php?report_id=".$_POST['report_id']);    

    }

?>

My folder images are located at 'tgotworker_testing' --> 'android' --> 'images'.

For update_photo_before.php:

'tgotworker_testing' --> 'pages' --> 'dashboard' --> 'engineer' --> 'view_task' --> 'update_photo_before.php'

Can anyone fix my problem? Thanks!

6
  • if ($sql) { checks to see if the database was updated and if it was, then it uploads the image. Maybe you want to upload the image first, and then update the database? Then you can check to see if the sql worked and reverse the image upload? You'll want to make sure that the folder has the correct permissions to be able to upload the images as well (perhaps that is why it is failing) Commented Mar 5, 2020 at 1:27
  • 1
    Double check permissions for the folder at $imgPath. The user that php is running as will need write access to that location. Commented Mar 5, 2020 at 1:37
  • @JeffVdovjak can you help me to update my code above? the URL is updated at database, but the photo doest update at the server folder Commented Mar 5, 2020 at 1:38
  • @AlexBarker how to check? Commented Mar 5, 2020 at 1:38
  • 1
    @PeterSondak To check your folder permissions depends on how you access your server. If you use an FTP or a webFTP or you use the command line will be different. PHP image upload permission info can be found on this question: stackoverflow.com/questions/10990/… Commented Mar 5, 2020 at 2:47

1 Answer 1

0

You check to see if the database gets updated, but not if the image is saved to the server.

move_uploaded_file() returns true on success and false on failure. So you can do a similar test as you have for $sql in your code.

<?php 
require_once '../../../../config/configPDO.php';

$report_id = $_POST['report_id'];
$last_insert_id = null;

//Allowed file type
$allowed_extensions = array("jpg","jpeg","png");

//File extension
$ext = strtolower(pathinfo($_FILES['uploadFile']['name'], PATHINFO_EXTENSION));

//Check extension
if(in_array($ext, $allowed_extensions)) {

    $defID = "before_" . $report_id;
    $imgPath = "images/$defID.png";
    $ServerURL = "http://172.20.0.45/tgotworker_testing/android/$imgPath";

    // Check if image is uploaded to folder
    if(move_uploaded_file($_FILES['uploadFile']['name'], $imgPath) === true) {

      // If upload succeeds, then update database
      $query = "UPDATE ot_report SET photo_before = '$ServerURL', time_photo_before = GETDATE() WHERE report_id = :report_id";
      $sql = $conn->prepare($query);
      $sql->bindParam(':report_id', $report_id);
      $sql->execute();

      // Check if database updated
      if (!$sql){
            echo "Error!! Database not updated.";
      } else {
           // Success!
           header("Location: view_task.php?report_id=".$_POST['report_id']);
      }
  } else {
      // Could not upload file
      echo "Error!! Could not upload file to server.";
  }
}

When PHP can't upload the file it will throw a warning. If you turn on your warnings while you are debugging, it should give you the reason why it's not working and you will be better able to solve the problem.

Put these lines at the top of the script to show all warnings and errors:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Sign up to request clarification or add additional context in comments.

4 Comments

where to put the code since $ServerURL is not declare?
I've updated the code to include all of your code. The code block was meant to replace your if($sql) block... here it is assembled.
I got this error, Error!! Could not upload file to server.
That's the error from the script you wrote. You need to turn on PHP warnings in order to see why it's not uploading. Or you will need to learn how to use try/catch blocks so you can capture the error. Have you added the ini_set and error_reporting lines to the top of your code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.