Andrei K Posted December 14, 2014 Share Posted December 14, 2014 Hi, I have a download template with an integer field download_counter and file_link. The task is to increment counter every time user downloads the file. So, I tried the following code: wireSendFile($page->download_file_link); $page->$download_counter = $page->$download_counter + 1; $page->save(); It sends file but doesn't increment the counter. I have modified it: wireSendFile($page->download_file_link); $p = $pages->get($page->id); $p->$download_counter = $p->$download_counter + 1; $p->save(); Alas, result is the same. File being downloaded but counter remains unchanged. Any suggestions? Link to comment Share on other sites More sharing options...
Jan Romero Posted December 14, 2014 Share Posted December 14, 2014 $p->$download_counter Because of the $, $download_counter is a variable here. You’re not accessing the page field “download_counter”, but a field by the name of whatever that variable contains. You should also disable output formatting before modifying a field. Use $page->setOutputFormatting(false) or $page->of(false). Lastly, it’s possible to save just the field by calling $page->save('download_counter'). Untested: wireSendFile($page->download_file_link); $page->of(false); $page->download_counter = $page->download_counter + 1; $page->save('download_counter'); $page->of(true); 5 Link to comment Share on other sites More sharing options...
Andrei K Posted December 15, 2014 Author Share Posted December 15, 2014 Thanks! It works but only if the counter incrementation goes before wireSendFile. i.e. $page->of(false); $page->download_counter = $page->download_counter + 1; $page->save(); $page->of(true); wireSendFile($f); Have I put wireSendFile before the code of incrementation it doesn't work. wireSendFile($f); $page->of(false); $page->download_counter = $page->download_counter + 1; $page->save(); $page->of(true); Have I missed something about wireSendFile functionality? Why it prevents execution of following code? Link to comment Share on other sites More sharing options...
Jan Romero Posted December 15, 2014 Share Posted December 15, 2014 WireSendFile exits unless you set 'exit' to false in the options parameter. Sorry I didn't pick up on that earlier. I never used that function... Try $opts = array('exit' => false); wireSendFile($f, $opts); It's kind of unfortunate that one has to read the code to learn a lot of PW's features, but I can only recommend it. It's well documented and well written, so it's quite easy to read. Link to comment Share on other sites More sharing options...
grimezy Posted March 9, 2017 Share Posted March 9, 2017 Hi, I recently implemented Soma's tutorial here and works great. Just a question... I would like to have certain files such as PDF's open up in the users browser instead of having the file download, but still have the increment work... how would I go about this? I'm assuming I would have to bypass using the 'wireSendFile' method and somehow increment the 'download x times' field another way? Thanks! Link to comment Share on other sites More sharing options...
Robin S Posted March 9, 2017 Share Posted March 9, 2017 8 hours ago, grimezy said: I recently implemented Soma's tutorial here and works great. Just a question... I would like to have certain files such as PDF's open up in the users browser instead of having the file download, but still have the increment work... how would I go about this? I'm assuming I would have to bypass using the 'wireSendFile' method and somehow increment the 'download x times' field another way? In Soma's tutorial he covers the $options array - does setting 'forceDownload' to false achieve what you want? On 26/09/2013 at 10:52 AM, Soma said: To see the options you have with PW's wireSendFile() you can also overwrite defaults <?php // pdf.php if($page->pdf){ $options = array( // boolean: halt program execution after file send 'exit' => true, // boolean|null: whether file should force download (null=let content-type header decide) 'forceDownload' => false, // string: filename you want the download to show on the user's computer, or blank to use existing. 'downloadFilename' => '', ); wireSendFile($page->pdf->filename, $options); } Simple and powerful isn't it? Try it out. 4 Link to comment Share on other sites More sharing options...
grimezy Posted March 9, 2017 Share Posted March 9, 2017 Ah @Robin S, you are a gentleman and a scholar... I missed this... thank you! 2 Link to comment Share on other sites More sharing options...
grimezy Posted March 16, 2017 Share Posted March 16, 2017 Hey @Robin S, just noticed when setting force download to 'true' the 'downloadFilename' option is not taken into account? So when a file is downloaded, the filename is always 'download' ? Here is currently what I have as my download.php template: if($page->download_file) { $options = array( // boolean: halt program execution after file send 'exit' => false, // boolean|null: whether file should force download (null=let content-type header decide) 'forceDownload' => true, // string: filename you want the download to show on the user's computer, or blank to use existing. 'downloadFilename' => '', ); wireSendFile($page->download_file->filename, $options); $page->of(false); $page->counter = $page->counter + 1; $page->save(); $page->of(true); } Any help would be greatly appreciated! Link to comment Share on other sites More sharing options...
Chris Ernovsky Posted February 21, 2022 Share Posted February 21, 2022 On 12/14/2014 at 11:06 PM, Jan Romero said: wireSendFile($page->download_file_link); $page->of(false); $page->download_counter = $page->download_counter + 1; $page->save('download_counter'); $page->of(true); I don't know why but the above incrementation line didn't work for me. Using '+= 1' didn't work either. I had 'unsupported operand' errors or a counter couldn't start counting from 0. Maybe it's something with sanitization of integers or PHP version - I don't have such knowledge. BUT at some point I tried shorter '$page->download_counter++' and it works at all scenarios. So my final (working) code looks like this (maybe it can help someone): // $page->download is a field with files // $page->download_counter is an integer field for counting downloads if ($page->download) { $options = array ( 'exit' => true, 'forceDownload' => true ); $page->of(false); $page->download_counter++; $page->save('download_counter'); $page->of(true); wireSendFile($page->download->filename, $options); } 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now