文件上传和下载成功,但能够在服务器上打开

时间:2018-04-20 05:45:51

标签: php php-5.6 php-5.5

我正在尝试使用以下代码行在服务器上上传文件。

    $.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");          
                    }
            });

在uploadAllFiles.php文件中,代码写为

 <?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;
           }
       }
    ?>

为了下载文件,代码写为

  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; 
  }
在downloadFile.php中的

,代码写为

       <?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;
       }
     ?>

在本地计算机上,这些代码工作正常。但在服务器上,文件将上载到证书文件夹中。当用户点击下载文件时,它会被下载。当用户单击该文件以便读取时,该文件无法打开。请注意,在服务器的证书文件夹中,如果我们从ftp下载该文件并将其打开,它将被打开。我认为问题出在下载代码中。我无法追查问题所在。

请帮助!!!

0 个答案:

没有答案
相关问题