Forum for discussing general topics related to Couch.
2 posts Page 1 of 1
I have a PHP script that is supposed to allow the users to download a file without showing the URL. When I try out the following, the file I download is empty. Could someone please help?

Original code actually work
Code: Select all
$fullpath = '/assets/file/sample-design.pdf'; 
$filename ='Sample-Design.pdf';


download.php
Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<?php header('X-Robots-Tag: noindex',true); ?>
<cms:template title='Download Sample Design' />
<cms:set my_page_id="<cms:gpc 'pid' method='get' />" />
<cms:php> global $CTX; $my_page_id = $CTX->get( 'my_page_id' ); if( !KFuncs::is_natural($my_page_id) ){ $CTX->set( 'my_page_id', '' ); } </cms:php>
<cms:set my_template_link=k_template_link />
<cms:if my_page_id >
<cms:pages masterpage='project.php' id=my_page_id>
<?php

$fullpath = '<cms:show sample_pdf />';
$filename ='<cms:show k_page_title />';
header("Cache-Control: public, must-revalidate");
header("Content-Type: application/exe");
header("Content-Length: " .(string)(filesize($fullpath)) );
header('Content-Disposition: attachment; filename="'.$filename.'"');                 
readfile($fullpath);

exit;
?>
</cms:pages>
</cms:if>
<?php COUCH::invoke(); ?>


project.php
Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template title='Project' clonable='1'>
<cms:config_list_view  searchable='1' />
<cms:editable name='this_content' label='Content' type='group' />
<cms:editable name='news_content' label='Article' toolbar='full' type='richtext' group='this_content' />
<cms:editable name='sample_pdf' label='PDF File' type='file' group='this_content' />
<cms:editable label='Visitor' name='page_hits' type='text' search_type='integer' />
</cms:template>
<cms:if k_is_page>
<cms:no_cache />
<cms:php>
global $CTX;if( isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT']) ){$CTX->set( 'is_bot', '1', 'global' );}
</cms:php>
<cms:if "<cms:not is_bot />"><cms:db_persist _masterpage=k_template_name _page_id=k_page_id _mode='edit' page_hits="<cms:add page_hits '1' />" /></cms:if>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<a href="<cms:show k_site_link />download/?pid=<cms:show k_page_id />">Download Sample Design</a>
</body>
</html>
<cms:else />
<cms:embed 'read_project.php' />
</cms:if>
<?php COUCH::invoke(); ?>
Prima-facie, I'd say the problem lies in 'download.php' where you are using native PHP (i.e. with <?php .. ?>) within <cms:pages> - native PHP gets executed *before* Couch tags and so your code wouldn't work as you expect.

Try using <cms:php> .. </cms:php> tags instead; so change the following code -
Code: Select all
<cms:pages masterpage='project.php' id=my_page_id>
<?php
    ...
    ...
?>
</cms:pages>

- to make it as follows
Code: Select all
<cms:pages masterpage='project.php' id=my_page_id>
<cms:php>
    ...
    ...
</cms:php>
</cms:pages>

That said, I wonder if had a look at https://docs.couchcms.com/concepts/cloaked-links.html
Couch natively has this feature of hiding links for downloadable files. Perhaps you'd like to use that feature instead?
2 posts Page 1 of 1
cron