[英]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.