readfile()函数读取zip文件而不是下载它(Zend)

时间:2017-02-23 11:04:26

标签: php .htaccess zend-framework

我必须触发下载zip文件(Zip文件在我的数据文件夹中)。 为此我正在使用代码,

$file = 'D:\php7\htdocs\Project\trunk\api\data\file.zip';
header('Content-Description: File Transfer');
header('Content-type: application/zip');
header('Content-disposition: attachment; filename=' . basename($file) );
readfile($file);`

这正在我所期望的核心php中工作。但是当我在Zend中使用相同的代码时,打印出如下的内容,

PKYsVJ)~�� study.xlsPKYsVJs�����+ tutorial-point-Export.xlsPKYsVJn��� 8��Zabc.xlsP

在内容之间我可以看到zip中所有文件的名称。但它没有被下载。

在我意识到这不起作用后,我开始搜索它并从堆栈中找到一些解决方案

尝试1:在每个随机行中添加不同的标题元素和ob函数

  • header('Content-Transfer-Encoding: binary');
  • header('Expires: 0');
  • header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  • header('Pragma: public');
  • header('Content-Length: ' . $file_size);
  • ob_start();
  • ob_clean();
  • flush();

所有这些尝试从不同的堆栈溢出问题和答案,并具有相同的结果

尝试2: PHP is reading file instead of downloading。这个问题没有任何公认的答案(他在询问核心php,但我只有zend的问题)。我尝试了所有这些,但它没有用。

尝试3: Changing the .htaccess。之后我认为这是我的.htaccess的一个问题,并找到了更改.htaccess文件的答案。

<FilesMatch "\.(?i:zip)$">
        ForceType application/octet-stream
        Header set Content-Disposition attachment
</FilesMatch>

这也给了我同样的结果。

尝试4: Using download functions in Zend。我在这个问题的答案中尝试了所有的zend函数。但是给我一个空输出,即使文件没有被读取。

尝试5:根据answer

删除php标记之前和之后的所有不需要的空格

还有其他方法可以在ZF2框架中触发下载吗?

修改

以下是我的确切功能。这是GET(API)函数,

public function getList(){
    try{
       //here i am getting the zip file name.
       $exportFile = $this->getRequest()->getQuery('exportid','');
       $file = 'D:\php7\htdocs\Project\trunk\api\data\\' . $exportFile . '.zip';
       header('Content-Description: File Transfer');
       header('Content-type: application/zip');
       header('Content-disposition: attachment; filename=' . basename($file) );
       readfile($file);
       return new JsonModel(["status"=>"Success"]);
    } catch(\Exception $e){
       return new JsonModel(["status"=>"Failed"]);
    }
}

6 个答案:

答案 0 :(得分:0)

根据此SO answer,您可以尝试对您的功能进行以下修改。

public function getList(){
    try{
       //here i am getting the zip file name.
       $exportFile = $this->getRequest()->getQuery('exportid','');
       $file = 'D:\php7\htdocs\Project\trunk\api\data\\' . $exportFile . '.zip';
        if (file_exists($file)) {
            $response = new \Zend\Http\Response\Stream();
            $response->setStream(fopen($file, 'r'));
            $response->setStatusCode(200);
            $response->setStreamName(basename($file));
            $headers = new \Zend\Http\Headers();

            $headers->addHeaders(array(
                'Content-Description' => 'File Transfer',
                'Content-Disposition' => 'attachment; filename="' . basename($file) .'"',
                'Content-Type' => 'application/zip',
                'Content-Length' => filesize($file)
            ));
            $response->setHeaders($headers);
            return $response;
            //return new JsonModel(["status"=>"Success"]);
        } else {
            return new JsonModel(["status"=>"Failed. No such file in \"".$file."\""]);
        }           
    } catch(\Exception $e){
       return new JsonModel(["status"=>"Failed"]);
    }
}

答案 1 :(得分:0)

这里有两个问题:

  • 您的浏览器尝试打开该文件,而不是下载它。
  • 另外,它没有正确打开文件。

两者都指向Content-Type错误。验证浏览器<%1} 收到是否正确(而不是重写为Content-Type)。

如果是,请将其更改为text/html。这可能不适用于Internet Explorer,它执行一些积极的内容类型嗅探。您可以尝试添加nosniff指令。

此外,在application/x-download之后(您可能会强制返回文件的内容而不是readfile()' - ,readfile),您应该停止所有输出都带有return file_get_contents($filename);。 ZIP文件目录位于最后,因此如果您在那里附加JSON消息,则浏览器可能无法下载文件,也无法正确显示该文件。

作为最后的手段,你可以去核并自己做所有事情。非常优雅,所有框架都应该提供替代方案,但以防万一...

return null;

可能一些创造性地使用atexit处理程序或析构函数挂钩可能会弄乱这个最后一个选项,但我觉得这不太可能。

答案 2 :(得分:0)

这对我有用!

public class MainActivity extends Activity {
    // Log tag
    private static final String TAG = MainActivity.class.getSimpleName();

    // Movies json url
    private static final String url = "http://api.androidhive.info/json/movies.json";
    private ProgressDialog pDialog;
    private List<Movie> movieList = new ArrayList<Movie>();
    private ListView listView;
    private CustomListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.list);
        adapter = new CustomListAdapter(this, movieList);
        listView.setAdapter(adapter);

        pDialog = new ProgressDialog(this);
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();

        // changing action bar color
        getActionBar().setBackgroundDrawable(
        new ColorDrawable(Color.parseColor("#1b1b1b")));

        // Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(url, new Response.Listener <JSONArray> () {
            @Override
            public void onResponse(JSONArray response) {
                Log.d(TAG, response.toString());
                hidePDialog();

                // Parsing json
                for (int i = 0; i < response.length(); i++) {
                    try {

                        JSONObject obj = response.getJSONObject(i);
                        Movie movie = new Movie();
                        movie.setTitle(obj.getString("title"));
                        movie.setThumbnailUrl(obj.getString("image"));
                        movie.setRating(((Number) obj.get("rating")).doubleValue());
                        movie.setYear(obj.getInt("releaseYear"));

                        // Genre is json array
                        JSONArray genreArry = obj.getJSONArray("genre");
                        ArrayList<String> genre = new ArrayList<String>();
                        for (int j = 0; j < genreArry.length(); j++) {
                            genre.add((String) genreArry.get(j));
                        }
                        movie.setGenre(genre);

                        // adding movie to movies array
                        movieList.add(movie);

                    } catch(JSONException e) {
                        e.printStackTrace();
                    }

                }

                // notifying list adapter about data changes
                // so that it renders the list view with updated data
                adapter.notifyDataSetChanged();
            }
        },
        new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hidePDialog();

                }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(movieReq);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.main, menu);
        int i = 1;
        return true;
    }

}

答案 3 :(得分:0)

如果你更正了标题的大小写它是否有效?即分别使用Content-Disposition和Content-Type over Content-disposition和Content-type?

无论如何,作为标准调试技术,我建议使用您的浏览器开发工具来检查正在进行的请求(inc标头),并将其与服务器端代码中的内容进行比较,以及服务器端响应中的内容和什么最终在客户端。我还会使用私人会话(Chrome等中的隐身模式)或新的配置文件/ VM安装来验证这一点,以消除其他任何问题。

另外,为什么不使用xsendfile并将发送文件的责任委托给Web服务器,这样您就不会在PHP代码中承担责任?您可以使用适当的服务器配置(有时通过.htaccess,但在当时和时代,您无论如何都可以完全控制),然后根据上述链接上的示例设置X-Sendfile标题:

header("X-Sendfile: $path_to_somefile");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$somefile\"");

答案 4 :(得分:0)

因为您返回JsonModel所以您的输出将是带有您的消息的json而不是缓冲以供下载。

修改:我注意到您遗失了Content-Transfer-Encoding: Binary,在我的os x - php5.6环境中进行了测试。

你应该试试这个

public function getList(){
    try{
       //here i am getting the zip file name.
       $exportFile = $this->getRequest()->getQuery('exportid','');
       $file = 'D:\php7\htdocs\Project\trunk\api\data\\' . $exportFile . '.zip';
       header('Content-Description: File Transfer');
       header('Content-type: application/zip');
       header('Content-disposition: attachment; filename=' . basename($file));

       header("Content-Transfer-Encoding: Binary");
       header("Content-length: " . filesize($file));
       header("Pragma: no-cache"); 
       header("Expires: 0"); 
       readfile("$file");
    } catch(\Exception $e){
       return new JsonModel(["status"=>"Failed"]);
    }
}

只需在响应时删除您的JSonModel。

答案 5 :(得分:-1)

您可以尝试下载文件而不是readfile(); 服务器端 - file_put_contents("file.zip", fopen("http://someurl/file.zip", 'r'));

客户方 - <a href="http://someurl/file.zip"><button>download file</button></a> download file