1

Can somebody help me on how to upload an Image or any type of files using PHP MySQL Ajax JQuery and change its File Name by Patient Name? I tried to use this following code:

HTML:

<div class="card-body">
                    <input type="hidden" class="form-control" name="txt-ultra_id" id="ultra_id" placeholder="---" readonly>
                            <div class="form-group">
                              <button type="button" class="btn btn-primary" data-toggle="modal"
                              data-target=".bd-example-modal-xl">Find Patient</button>
                            </div>
                                  <div class="row g-3">
                                      <div class="col">
                                      <div class="col drop">
                                          <label>Patient Name</label>
                                            <input type="hidden" class="form-control" name="txt-patient_id" id="txt-patient_id" placeholder="---" readonly>
                                            <input type="text" class="form-control" name="txt-patient_name" id="txt-patient_name" placeholder="---" readonly>
                                        </div>
                                      </div>
                                      <div class="col">
                                        <div class="col drop">
                                          <label>Age</label>
                                            <input type="text" class="form-control" name="txt-patient_age" id="txt-patient_age" placeholder="---" readonly>
                                        </div>
                                      </div>
                                      <div class="col">
                                        <div class="col drop">
                                          <label>Gender</label>
                                          <input type="text" class="form-control" name="txt-patient_gender" id="txt-patient_gender" placeholder="---" readonly>
                                        </div>
                                      </div>
                                      <div class="col">
                                        <div class="col drop">
                                          <label>Date Performed</label>
                                            <input type="date" class="form-control datepicker" name="txt-datePerformed" id="txt-datePerformed" required="true">
                                        </div>
                                      </div>
                                  </div>
                                  <div class="row g-3">
                                      <div class="col">
                                        <div class="col drop">
                                          <label>Interpretation</label>
                                            <textarea class="form-control" name="txt-interpretation" id="txt-interpretation"></textarea>
                                        </div>
                                      </div>
                                  </div>
                                  <div class="card-header">
                                      <h4>Upload Results</h4>
                                  </div>
                                  <div class="card-body">
                                          <div class="fallback">
                                              <input type="file" class="fileToUpload form-control" name="image" id="image" multiple/>
                                          </div>
                                  </div>
                          
                                      <div class="text-right">
                                          <button type="button" class="btn btn-outline-primary" name="btnSaveUltrasound" id="btnSaveUltrasound">Save Result</button>
                                      </div>                    
                      </div>
                    </div>

In JS File:

$("#btnSaveUltrasound").click(function(){         
        var patient_id=$("#txt-patient_id").val();
        var patient_name=$("#txt-patient_name").val();
        var datePerformed=$("#txt-datePerformed").val();
        var interpretation=$("#txt-interpretation").val();
        var file_data = $('.fileToUpload').prop('files')[4];
        var form_data = new FormData();
        form_data.append("file",file_data);
        form_data.append("txt-patient_name",patient_name);

    if(patient_id.length==0){
        swal("Patient Name is required.");
        $("#txt-patient_name").focus();
    }else if(interpretation.length==0){
        swal("Please indicate Interpretaion.");
        $("#txt-interpretation").focus();
    }else{
        $.ajax({
            url: "crud-ultra.php",
            method: "POST",
            data:{
                addUltrasound:1,
                txtpatient_id:patient_id,
                txtpatient_name:patient_name,
                txtdatePerformed:datePerformed,
                txtinterpretation:interpretation,
                form_data
            }

        }).done(function(msg){
            cleanerUltrasound();
            swal('Saved', 'Ultrasound Result of the Patient was added successfully!', 'success');
        });
    }
});

In PHP File:

<?php
require_once("include/connect.php");

if(isset($_POST['addUltrasound'])){             //ADD ULTRASOUND RESULT

    $patient_id=htmlspecialchars(strip_tags($_POST['txtpatient_id']));
    $patient_name=htmlspecialchars(strip_tags($_POST['txtpatient_name']));
    $datePerformed=htmlspecialchars(strip_tags($_POST['txtdatePerformed']));
    $interpretation=htmlspecialchars(strip_tags($_POST['txtinterpretation']));

    $target_directory = "Exam Results/ULTRASOUND/";
    $target_file = $target_directory.basename($_FILES["image"]["name"]);   //name is to get the file name of uploaded file
    $filetype = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
    $newfilename = $patient_name." "."(".$datePerformed.")".$filetype;
  
    if(move_uploaded_file($_FILES["image"]["tmp_name"],$newfilename)) echo 1;
    else echo 0;

    $sql="INSERT INTO tbl_xrayecgechoultra(patient_id,service_id,datePerformed,interpretation,photoResult) VALUES(?,9,?,?,?)";
    $data=array($patient_id,$datePerformed,$interpretation,$newfilename);
    $stmt=$con->prepare($sql);
    $stmt->execute($data);
}
?>

When I see the result, File Name of the image and other data was saved in the Database, but the Image File itself is not save or move in the directory file or the target folder directory.

I have libraries needed to run this program, like JQuery Library, Sweetalert, and many more.

Thank you so much <3

4
  • You're not adding the $target_directory when you're moving the file. You're only adding it to $target_file, which you only use to get the file extension. Right now, it's trying to move the temp file to the same directory as you have the script in. Unless that's writable, it won't work. Does your code echo 0 or 1? Commented Jul 12, 2021 at 9:29
  • @MagnusEriksson how can I do that? Can you help me? Commented Jul 12, 2021 at 9:45
  • In your move_uploaded_file(), change $newfilename to $target_directory . $newfilename. Alternatively, add it to the $newfilename if you want the path to be stored in the database as well (just like you did for $target_file). Also make sure that folder is writable. Commented Jul 12, 2021 at 9:49
  • @MagnusEriksson do you have other solution? I'm sorry, it's not working in my program. :( Commented Jul 14, 2021 at 6:59

0

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.