1

I have some docx file at server with some specific place and i just want to download them using php.but we can't redirect them at given places because there is actual client documents are stored

I tried to google but didn't get much appropriate solution for that

Current code :

    function  users_export_resume($id){

        $filename=base_url()."user_info/".$id."/resume.docx";
        header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=Resume.docx"); 
header("Content-Transfer-Encoding: binary ");  
        readfile($filepath);
    }

Any suggestions ? is there anything wrong ? Please take me out Thanks in Advance

Ankit Doshi
  • 1,164
  • 3
  • 21
  • 43
  • Why don't you just do a redirect as the result `header('Location: '.$newURL);` – hoss Jun 14 '15 at 20:12
  • possible duplicate of [How to make a redirect in PHP?](http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) – hoss Jun 14 '15 at 20:12
  • I don't believe like need to redirect at that place where i am store my client document so please give me another alternative because of that even i need also file name must be need to change instead of same file name given that's need for security purpose – Ankit Doshi Jun 14 '15 at 20:15
  • Its not redirect issue so please remove your comment like its duplicate of redirect – Ankit Doshi Jun 14 '15 at 20:25
  • Mods will remove it if it's not helpful. – hoss Jun 14 '15 at 20:26

1 Answers1

2

I think error is:

  1. you use undefined variable $filepath
  2. variable $filename must contain path to file, not link.

So, your code must be:

define('WWW_ROOT', '/var/www/your_site_address'); // put it into C:\www\etc on Windows

function  users_export_resume($id){
    $filename=WWW_ROOT."/user_info/".$id."/resume.docx";
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");;
    header("Content-Disposition: attachment;filename=Resume.docx"); 
    header("Content-Transfer-Encoding: binary ");  
    readfile($filename);
}

and in this case your code work well for me.

I hope it will help.

EDIT 1:

I fixed code for multi-OS issue

stepozer
  • 1,143
  • 1
  • 10
  • 22
  • Nope it will not work if i will consider like my app must need to compatible with linux as well as microsoft than it will create issue I can't give static path for that – Ankit Doshi Jun 14 '15 at 20:23
  • i just want to know is there anything wrong with header because i can't get exact solution for that please help me if you get better solution with dynamic path instead of static path – Ankit Doshi Jun 14 '15 at 20:35
  • @AnkitDoshi your code contain 2 errors. I fixed it in my example. All headers is correct. Also i added constant for linux and windows. – stepozer Jun 14 '15 at 20:38
  • @Thanks for reply now download file size is same as actual file size but still whenever i am trying to export there is give me error please refer below url http://postimg.org/image/72kc0ywwr/ – Ankit Doshi Jun 14 '15 at 20:48
  • @AnkitDoshi, hmm very interesting. Try add `error_reporting(0);` into begin on your PHP file. Probably some external characters get to output... – stepozer Jun 14 '15 at 20:53
  • @AnkitDoshi save the file instead of opening it in word - then open the file in a text editor and check the top and bottom for php or anything else odd –  Jun 14 '15 at 20:59
  • @stepozer i am using Codeigniter and i put error_reporting(E_ALL); in index.php for development enviroment and application/config,php i put log_threshold =4 but there is nothing to show me which we can say or can think like its cause the error so anything else which i have to try – Ankit Doshi Jun 14 '15 at 21:04
  • @Dagon check the top and bottom for php or anything else odd means i can't get it can you please explain some more info ? – Ankit Doshi Jun 14 '15 at 21:07
  • when you click the link and the browser asks you what to do with the file, save it, don't open it in word ... –  Jun 14 '15 at 21:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80532/discussion-between-ankit-doshi-and-dagon). – Ankit Doshi Jun 14 '15 at 21:17
  • @AnkitDoshi I have same problem in window machine http://postimg.org/image/72kc0ywwr/ but when I tried into Linux machine its working correctly – Renu Thakur Mar 25 '16 at 07:40