flipper828 Posted April 7, 2010 Share Posted April 7, 2010 I have a site that requires login and password to access. It works fine. However, the downloadable documents (.doc and .pdf) meant only for logged in users, are available for anyone regardless of whether they are logged in or not. In my research, I found a way to mask the url for the download files by using a download.php script. I implemented the script and am having problems. When I click the link for a .pdf, all SEEMS to be working as it should. But when I try to open the .pdf, I get this message: Acrobat could not open "filename.pdf" because it is either not a supported file type or because file has been damaged........ When I click the link for the .doc, again all seems to go well until I open the document. The Word document opens up with this message: <br /> <b>Warning</b>: filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for /home/administrator/docs/trc/filename.doc in <b>C:\wamp\www\download.php</b> on line <b>12</b><br /> <br /> <b>Warning</b>: readfile(/home/administrator/docs/trc/filename.doc) [<a href='function.readfile'>function.readfile</a>]: failed to open stream: No such file or directory in <b>C:\wamp\www\download.php</b> on line <b>14</b><br /> This is the code from my download.php file <?php session_start(); if (! $_SESSION['ug_user']) { header("location:login.php"); } $dir="/home/administrator/docs/trc/"; if (isset($_REQUEST["file"])) { $file=$dir.$_REQUEST["file"]; header("Content-type: application/force-download"); header("Content-Transfer-Encoding: Binary"); header("Content-length: ".filesize($file)); header("Content-disposition: attachment; filename=\"".basename($file)."\""); readfile("$file"); } else { echo "No file selected"; } ?> When I set up the link, it looks something like this: <a href=\"http://example.com/download.php?file=filename.doc\" target=\"_blank\"><img src=\"images/word_icon.jpg\" alt=\"Word download icon\" width=\"25px\" height=\"25px\" border=\"0\"></a> <a href=\"http://example.com/download.php?file=filename.pdf\" target=\"_blank\"> <img src=\"images/pdf_icons_03.jpg\" alt=\"PDF download icon\" width=\"20px\" height=\"30px\" border=\"0\"></a> Both documents are in working condition. Please help? If you need any other information, please don't hesitate to ask. Quote Link to comment https://forums.phpfreaks.com/topic/197896-problems-with-downloadphp-script/ Share on other sites More sharing options...
flipper828 Posted April 8, 2010 Author Share Posted April 8, 2010 Thanks to everyone that looked at my problem. I finally found out what the problem was. I didn't have the documents above the root directory. So I moved them and adjusted the path in the script and it works beautifully! Quote Link to comment https://forums.phpfreaks.com/topic/197896-problems-with-downloadphp-script/#findComment-1038950 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.