skip to Main Content

I’m trying to save all the information inserted by user into database, all the info are able to save except the pdf file.

My code:

usermanual.php

<div id="usermanualModal" class="modal fade">
    <div class="modal-dialog">
        <form method="post" enctype="multipart/form-data" id="usermanualForm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title"><i class="fa fa-plus"></i>&nbsp; Add New File</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="um-area" class="control-label">Area</label>                         
                        <select id="um-area" name="um-area" class="form-control" placeholder="Area...">                 
                            <?php $usermanual->getUMAreas(); ?>
                        </select>                       
                    </div>
                    <div class="form-group">
                        <label for="um-subject" class="control-label">Subject</label>   
                        <input type="text" class="form-control" id="um-subject" name="um-subject" placeholder="Subject" required>
                    </div>
                    <div class="form-group">
                        <label for="um-description" class="control-label">Description</label>   
                        <input type="text" class="form-control" id="um-description" name="um-description" placeholder="Description" required>
                    </div>
                    <div class="form-group">
                        <label for="pdf" class="control-label"> File <span class="pdf-label">&emsp;(PDF file only)</span></label>
                        <input type="file" id="pdf" name="pdf" class="form-control" accept=".pdf" required/>
                    </div>
                </div>
                <div class="modal-footer">
                    <input type="hidden" name="usermanualId" id="usermanualId" />
                    <input type="hidden" name="action" id="action" value="" />
                    <input type="submit" name="save" id="save" class="btn save" value="Upload" />
                </div>
            </div>
        </form>
    </div>
</div>

usermanual_action.php:

if (!empty($_POST['action']) && $_POST['action'] == 'addFile') {
    $usermanual->area = $_POST["um-area"];
    $usermanual->subject = $_POST["um-subject"];
    $usermanual->description = $_POST["um-description"];
    $usermanual->filename = $_FILES["pdf"]["name"];
    $usermanual->insertUM();
}

usermanual.js:

$(document).ready(function() {  

    $('#addFile').click(function(){
    $('#usermanualModal').modal('show');
    $('#usermanualForm')[0].reset();
    $('.modal-title').html("<i class='fa fa-plus'></i> Add File");
    $('#action').val('addFile');
    $('#save').val('Save');
    });
    
    $(document).on('submit','#usermanualForm', function(event){
        event.preventDefault();
        $('#save').attr('disabled','disabled');
        var formData = $(this).serializeArray();
        $.ajax({
            url:"usermanual_action.php",
            method:"POST",
            data:formData,
            success:function(data){             
                $('#usermanualForm')[0].reset();
                $('#usermanualModal').modal('hide');                
                $('#save').attr('disabled', false);
                location.reload();
                usermanualData.ajax.reload();
            }
        })
    });

Usermanual.php:

    public function insertUM() {
        if($_POST['um-area']) {
            $this->usermanual = strip_tags($this->usermanual);              
            $queryInsert = "INSERT INTO ".$this->usermanualFiles." (area, subject, description, filename) VALUES('".$_POST['um-area']."', '".$_POST['um-subject']."', '".$_POST['um-description']."', '".$_FILES['pdf']['name']."')";

            mysqli_query($this->dbConnect, $queryInsert);

            $uploadDirectory = 'pdf/';

            $targetFile = $uploadDirectory . basename($_FILES['pdf']['name']);

            if (move_uploaded_file($_FILES['pdf']['tmp_name'], $targetFile)) {
                echo "The file " . basename($_FILES['pdf']['name']) . " has been uploaded.";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
            if ($_FILES['pdf']['error'] !== UPLOAD_ERR_OK) {
                echo "File upload failed with error code: " . $_FILES['pdf']['error'];
                exit;
            }
        }
    }

I have tried different ways, but the pdf file still not able to insert into database, following is the example outcome:
enter image description here

Please help if you have any solution, thanks a lot!

2

Answers


  1. You should save the pdf file on disk and only save the file path in the database

    Login or Signup to reply.
  2. Actually, MySQL database cannot support saving files directly, so you need 2 steps:

    provide an API to upload the PDF file to your disk and create the path of your uploaded
    then save the path you uploaded to the database

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search