[英]Can't make downloadable a file
我从服务器下载时遇到麻烦。 如果我输入http://mypage.com,则无法下载某些.zip文件。 但是,如果我输入页面的IP,则会下载文件。
我遇到的其他类似问题与Godaddy有关,即使我使用IP或域进行访问,也无法进行zip下载。
这是生成XML和ZIP的代码的一部分:
**xmlzip.php**
$xmlfile = $rfc.$year.$month.'BN.xml';
$xml->formatOutput = true;
$el_xml = $xml->saveXML();
$xml->save($xmlfile);
$filename = $rfc.$year.$month.'BN';
shell_exec('zip ../'.$filename.' '.$xmlfile);
try {
$date= date('Ymd_Hi');
$data = '{
"filename":"xml'.$date.'.zip",
"filename2":"'.$filename.'.zip"
}';
echo '{"success":1,"message":"ok","data":['.$data.']}';
} catch (Exception $e) {
$data = '';
echo '{"error":1,"message":"error","data":['.$data.']}';
die();
}
然后我在ExtJS上得到它来创建Messagebox.wait:
**downloadzip button**
msg = Ext.MessageBox.wait('Generating XML ...', '');
Ext.Ajax.request({
url: 'cakephp/app/webroot/xml.php?',
params:{
rfc: rfc,
month: month,
year: year
},
method : "POST",
headers: {
'Content-Type': 'application/json'
},
jsonData: true,
timeout: 1000000,
withCredentials: true,
success : function(response) {
var jsonResponse = JSON.parse(response.responseText);
filename = jsonResponse.data[0].filename;
filename2 = jsonResponse.data[0].filename2;
if(jsonResponse.success === 1) {
msg.hide();
Ext.getCmp("winFormXML_XMLpanel").setHtml(
'<iframe id="" name=""'+
' src="cakephp/app/webroot/download_xml.php?filename='+
filename+'&filename2='+filename2+'" width="100%" height="100%"></iframe>');
Ext.getCmp('winFormXML').destroy();
} else {
msg.hide();
Ext.Msg.alert("ERROR","Error generating XML.");
}
},
failure : function(response) {
msg.hide();
var respObj = Ext.JSON.decode(response.responseText);
console.log(respObj);
Ext.Msg.alert("ERROR", respObj.status.statusMessage);
}
});
并以此下载生成的文件:
**downloadzip.php**
try {
$filename = $_REQUEST['filename'];
$filename2 = $_REQUEST['filename2'];
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$filename2);
header('Content-Length: ' . filesize($filename2));
readfile($filename2);
} catch(Exception $ex) {
echo $ex-getMessage();
}
就像我上面提到的,我知道它是可行的,因为我可以从其他计算机下载它,但可以通过IP,而不是从域下载。
编辑:
似乎该行Ext.getCmp('winFormXML').destroy();
在产生麻烦。 删除该行使其起作用!
“ Upgrade-Insecure-Requests:1 ”表示您的浏览器要求服务器将URL(http)转换为安全URL(https)。
为了获得最佳的逻辑路径,请创建一个小cakeph插件(也许该插件存在 ),或者仅使用一个控制器(例如pagesController或专用控制器),然后在该控制器内创建一个动作(函数),该动作将完成您所需要的所有工作需要(对xml文件,zip和下载的操作)
这样,您可以添加安全层(例如,仅允许经过身份验证的用户下载文件),还可以添加一些统计信息(将下载的计数器保存在数据库中)
而且我不确定使用shell_exec是一个好习惯,而是尝试ziparchive有用的Cakephp zip helper的示例或类似的例子
<?php
...
$filename2 = 'xml.zip';
$zip = new ZipArchive;
if ($zip->open($filename2, ZipArchive::CREATE)!==TRUE)
{
die("zip creation failed!");
} else {
$zip->addFile($xmlfile);
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$filename2);
header('Content-Length: ' . filesize($filename2));
readfile($filename2);
unlink($filename2);
}
?>
最后,如果您在使用IP地址时没有“ Upgrade-Insecure-Requests”消息,则可能是问题出在您的浏览器上。 尝试使用未实现此安全级别的浏览器(例如chrome或firefox),或仅将您的网站配置为使用https协议:-> .htaccess中的重定向(在cakephp根目录内部)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(s|g)etcmd?(.+)$
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^(.*)http(\:|\%3A)(.*)$
ReWriteRule .* - [F]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
->虚拟主机中的一些配置,以侦听端口443(如果在* nix下,则在/ etc / apache2 / site-available内)
# with the automatic HTTPS redirection you are not supposed to configure HTTP part (port 80)
<VirtualHost *:80>
ServerAdmin admin@mypage.com
ServerName mypage.com
ServerAlias mypage.com
DocumentRoot /var/www/mypage
<Directory /var/www/mypage/>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
ServerSignature Off
ErrorLog /var/log/apache2/error.log
</VirtualHost>
<VirtualHost *:443>
ServerAdmin admin@mypage.com
ServerName mypage.com
ServerAlias mypage.com
DocumentRoot /var/www/mypage
<Directory /var/www/mypage/>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
ServerSignature Off
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM
# If you have secure certificate
SSLCertificateFile /etc/apache2/certificats/YOURCRTFILE.crt
SSLCertificateKeyFile /etc/apache2/certificats/YOURPEMFILE.pem
SSLCertificateChainFile /etc/apache2/certificats/YOURPEMFILE.pem
SSLCACertificatePath /etc/ssl/certs/
SSLCACertificateFile /etc/apache2/certificats/YOURCRTFILE.crt
ErrorLog /var/log/apache2/error.log
</VirtualHost>
希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.