2017-09-04 115 views
0

我从服务器下载时遇到麻烦。如果我输入到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(); 
    } 

正如我前面提到的,我知道它的作品,因为我可以FR下载其他计算机,但通过IP,而不是来自域。


编辑:

看来,线Ext.getCmp('winFormXML').destroy();生成时被给予的烦恼。删除该行使它的工作!

+1

希望我没有错,但一般来说,如果您在使用IP地址时解决了URL问题,可能是[DNS](https:// en。 wikipedia.org/wiki/Domain_Name_System#Function)或[hosts文件](https://en.wikipedia.org/wiki/Hosts_(file)#Purpose)。尝试打开命令提示符pt命令并ping URL mypage.com,如果在命令结果/输出中没有看到正确的IP地址,请尝试检查您的DNS或主机文件。另一件事,不要在你的cakephp webroot文件夹中启动php脚本,这是一个安全问题/违规。 – Benfarhat

+0

upvote cuz你是答案。谢谢!首先,ping到mypage.com及其正确的IP。我检测到的其他问题是,当我点击downloadxml按钮时,我的downloadzip.php以两种方式都会变红(错误/警告),我的意思是当下载在网络选项卡中工作时.Request标题显示:'Upgrade-Insecure -REQUESTS:1'。没有预览或回应。在计时被暂停在21.00毫秒。有任何想法吗? :(。顺便说一句,最好的路径把PHP脚本? –

回答

1

Upgrade-Insecure-Requests:1”表示您的浏览器要求您的服务器将url(http)转换为安全url(https)。 (或者插件exist)或者只是使用一个控制器(如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地址没有升级不安全,请求消息,它意味着问题来自您的浏览器。尝试使用不实现此安全级别的浏览器(如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(内部的/ etc/apache2的/站点可用,如果你下的* nix)

# with the automatic HTTPS redirection you are not supposed to configure HTTP part (port 80) 
<VirtualHost *:80> 
    ServerAdmin [email protected] 
    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 [email protected] 
    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> 

希望它有帮助

+0

嘿!谢谢你是anwser!我是相对新的发展,并因为时间,我用一个PHP而不是控制器(因为我'在ZIP这一代中,我使用了ziparchive,但是尝试使用shell_exec来解决我的问题,但现在我已经解决了这个问题,并且根据你的说法,我将把它替换为shell_exec。还有最后一件事,你能帮我一个关于如何在cakephp/controller上创建xml和zip的例子吗? –

+0

cakephp的哪个版本? – Benfarhat

+0

对不起,迟了!我在版本2.6.1 –