简体   繁体   中英

How to access files in data folder in zend framework?

currently i am working on zf2. Right now i have to give download option to download pdf files.i have stored all the pdf files in data directory.How can i specify link to that pdf files from .phtml file?

thanks in advance.

A user will never gain direct access to your /data directory. This would be just not that good. But you can easily write yourself a download-script.php or the likes that will hand out the content of this directory to your users.

If you take a look at the first six lines of public/index.php you'll see the following:

<?php
/**
 * This makes our life easier when dealing with paths. Everything is relative
 * to the application root now.
 */
chdir(dirname(__DIR__));

With this in mind, you know that from PHP's side of things the access to anything inside the data directory is as simple as data/file.pdf

You'd always want to write yourself some sort of download-logger. Write yourself a controller. Have an action inside of that controller probably called something like download or anything like that. That action should have one parameter filename .

All that this action does is to check if filename exists file_exists('data/'.$filename) and if it exists, you simply deliver this file to your users. An example mix or zf2 and native php could be:

public function downloadAction() 
{
    $filename = str_replace('..', '', $this->params('filename'));
    $file     = 'data/' . $filename;

    if (false === file_exists($file)) {
        return $this->redirect('routename-file-does-not-exist');
    }

    $filetype = finfo_file($file);
    header("Content-Type: {$filetype}");
    header("Content-Disposition: attachment; filename=\"{$filename}\"");
    readfile($file);

    // Do some DB or File-Increment on filename download counter
    exit();
}

This is not clean ZF2 but i'm lazy right now. It may be much much more ideal to use a proper Response Object and do the File-Handling there!

Important Update this thing was actually quite insecure, too. You need to disallow parent-folders. You wouldn't wanna have this guy do something outside of the data/download directory like

`http://domain.com/download/../config/autoload/db.local.php` 

If I'm not totally mistaken, simply replacing all occurences of double-dots should be enough...

I would create a symbolic link in public directory for PDF files in data folder.

For example:

ln -s /your/project/data/pdfdir /your/project/public/pdf

and create links something like

<a href="/pdf/file.pdf">File.pdf</a>

Borrowing Sam's code, here's what it looks like in ZF2 syntax.

public function downloadAction()
{
    $filename = str_replace('..', '', $this->params('filename'));
    $file     = 'data/' . $filename;

    if (false === file_exists($file)) {
        return $this->redirect('routename-file-does-not-exist');
    }

    $filetype = finfo_file($file);
    $response = new \Zend\Http\Response\Stream();
    $response->getHeaders()->addHeaders(array(
        'Content-Type' => $filetype,
        'Content-Disposition' => "attachement; filename=\"$filename\""
    ));
    $response->setStream(fopen($wpFilePath, 'r'));
    return $response;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM