Php 文件已成功上载和下载,但可以在服务器上打开
我正在尝试使用下面的代码行在服务器上上载文件Php 文件已成功上载和下载,但可以在服务器上打开,php,php-5.6,php-5.5,Php,Php 5.6,Php 5.5,我正在尝试使用下面的代码行在服务器上上载文件 $.ajax({ url: '../common/uploadAllFiles.php?root=../student/certificates/', dataType: 'text', cache: false, contentType: false,
$.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;
}
?>
在本地机器上,这些代码工作正常。但在服务器上,文件上载到证书文件夹中。当用户单击下载文件时,它将被下载。当用户点击该文件进行读取时,该文件不会打开。注意,在服务器的certificates文件夹中,若我们从ftp下载文件并打开它,它就会被打开。我认为问题在于下载代码。我无法追踪问题出在哪里
请帮忙 你们有并没有在你们的浏览器中通过图像的URL检查它是否显示?对不起,我并没有得到它。。。请解释您要读取哪些文件扩展名?jpeg、png、docx txt。您是否尝试在浏览器中运行图像url?