2

I am trying upload a file on server by using below lines of code.

    $.ajax({
                    url: '../common/uploadAllFiles.php?root=../student/certificates/', 
                    dataType: 'text', 
                    cache: false,
                    contentType: false,
                    processData: false,
                    data: form_data,                         
                    type: 'post',
                    success: function(php_script_response){
                        var document_unique_name = php_script_response;
                        $('#file-unique-name').val(document_unique_name);    
                        $('#upload-file_help').addClass("hide");   
                        $('#upload-file-div').removeClass("has-error");          
                    }
            });

In uploadAllFiles.php file, the code is written as

 <?php
        $uniqId = uniqid('file_');
        $root = $_REQUEST['root'];

        $target_file = "uploads/".basename($_FILES["file"]["name"]);

        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

       if ( 0 < $_FILES['file']['error']) 
       {
          echo 'Error: ' . $_FILES['file']['error'] . '<br>';
       }
       else if ($_FILES["file"]["size"] > 10000000) 
       {
         echo "Sorry, your file is too large.";
       }
       else 
       {   
          if( move_uploaded_file($_FILES['file']['tmp_name'], $root.$uniqId.".".$imageFileType))
           {
              echo $uniqId.".".$imageFileType;
           }
       }
    ?>

For downloading files, the code is written as

  function downloadCertificate(document_unique_name,document_actual_name) 
  {   
      window.location =  '../common/downloadFile.php?document_unique_name='+document_unique_name+'&document_actual_name='+document_actual_name; 
  }

in downloadFile.php, the code is written as

       <?php
        $fileName = $_REQUEST['document_unique_name'];
        $downloadFileName = $_REQUEST['document_actual_name'];
        $filePath = "../student/certificates/".$fileName;

        if(file_exists($filePath)) 
        {
           header('Content-Description: File Transfer');
           header('Content-Type: application/force-download');
           header('Content-Disposition: attachment; filename='.$downloadFileName);
           ob_clean();
           flush();
           readfile($filePath);
           exit;
       }
     ?>

On the local machine, these codes are working fine. But on the server, the files are uploaded in the certificates folder. When user clicks on the download file, it gets downloaded. When user clicks on that file in order to read, that file does not open. Note that in certificates folder of server, if we download that files from ftp and open it, it gets opened. I think the issue is in the download code. I am not able to trace where is the issue.

Please help !!!

Nida Amin
  • 735
  • 1
  • 8
  • 28

0 Answers0