How do I let users download files?

I have created one plugin other than the elgg core plugins. In that I have override the registration and I added 2 more file attachments in the registration form.

These attachments I saved in the plugin folder itself  ith the same name of the uploaded file , not in the data folder .

The saved attachments's path is  $cprPath = $CONFIG->pluginspath."student_users/documents/students/cpr/66";

and the images saved their successfully. Here 66 is the userid. Now I want to download the file using the below code

 

$cprPath = $CONFIG->pluginspath."student_users/documents/students/cpr/66";   

$single_addition .= '


';

 

when i click on the link i got the error like this below
The address wasn't understood
Firefox doesn't know how to open this address, because the protocol (d) isn't associated with any program.

 

How can I solve this issue.Please help me..

 

 

 

  • You can't use a system path as a url

    eg. /var/www/html/mysite/index.php is not a valid url

    http://mysite.com/index.php is a valid url

     

    Instead of $CONFIG->pluginspath you need to be using elgg_get_site_url() and registering a pagehandler to server your pages.

  • Hi Matt,

    I have added the page handler as you told..but i got a blank page only.

    Below is my codes:

    PAGE LINK

    $downloadCpr =  $CONFIG->wwwroot. "pg/simpleusermanagement/downloadFile/".$vars['user']->guid;

    <a href="'.$downloadCpr.'"><span>File Name</span></a>

    PAGE HANDLER IN START.PHP

    function simpleusermanagement_page_handler($page) {
            global $CONFIG;
            $guid = $page[1];
            if ($page[0]) {
                switch ($page[0]) {
                case "studentmanage":
                    include($CONFIG->pluginspath . 'simpleusermanagement/index.php');
                    break;
                case "facultymanage":
                    include($CONFIG->pluginspath . 'simpleusermanagement/facultyindex.php');
                    break;
                case "downloadFile":
                    set_input('userId',$guid);
                    include($CONFIG->pluginspath . 'simpleusermanagement/downloadFile.php');
                    break;
                default:
                    include($CONFIG->pluginspath . 'simpleusermanagement/index.php');
                    break;
                }
            } else {
                include($CONFIG->pluginspath . 'simpleusermanagement/index.php');
                return true;
            }
        }

    downloadFile.php

     

    <?php
    require_once(dirname(dirname(dirname(__FILE__))) . '/engine/start.php');
    global $CONFIG;
     $guid = get_input('userId');
    $user = get_entity($guid);
    print_r($user);


    echo $cprfilename =$user->cprfile;
    $cprPath = $CONFIG->site->url."mod/imam_users/documents/students/cpr/".$guid."/".$cprfilename;
    echo $fullPath = $cprPath;

    if ($fd = fopen ($fullPath, "r")) {
       echo "$$". $fsize = filesize($fullPath);
        $path_parts = pathinfo($fullPath);
        $ext = strtolower($path_parts["extension"]);
        switch ($ext) {
            case "pdf":
            header("Content-type: application/pdf"); // add here more headers for diff. extensions
            header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
            break;
            default;
            header("Content-type: application/octet-stream");
            header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
        }
        header("Content-length: $fsize");
        header("Cache-control: private"); //use this to open files directly
        while(!feof($fd)) {
            $buffer = fread($fd, 2048);
            echo $buffer;
        }
    }
    fclose ($fd);
    exit;
    ?>

    I got the userid in this page. But didint get the user with the code

     $guid = get_input('userId');
    $user = get_entity($guid);
    print_r($user);

    Why it happened like this.. My output is a blank white screen only..

    Please suggest a solution

     

    Thanks and regards

     

  • On an intital review I would say you probably don't need 

    require_once(dirname(dirname(dirname(__FILE__))) . '/engine/start.php');
    global $CONFIG;

    In your download.php, this is included when you call the download.php in the pagehandler.  

    Also please check that in the .htaccess that you are outputting PHP errors by default this is swtiched off. You can also turn this on in /admin/settings/advanced

    When you save the file are you using the filehanlder object? The way I understand how elgg works with files. You save the file using the filehanlder.  This object then saves the file to the filestore and creates an entity in the database you can referrance.

    You the create a pagehandler for the request of the the file which because it is inherited from a filehanlder object it will take care of the file headers.

     

  • Blank white screen means php fatal error

     

    pg/simpleusermanagement/downloadFile/".$vars['user']->guid;

    Are you using 1.7?  you don't need the 'pg' prefix in 1.8

     

    There's a good example of how to do this in the core files plugin

  • I edited the title to be more descriptive.

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking