Download file program using php on unix box
NickName:theartist33 Ask DateTime:2013-09-08T03:41:03

Download file program using php on unix box

I have created a beginner program to forcefully download file from unix box to windows through browser, it is not throwing any error but shows nothing on browser just a blank page.

PHP version- 5.2.13
Apache-2.0
Unix Box- HP-UX 11.11 (old version latest is 11.31)
local PC- windows XP Prof.
Browser- IE 7, Mozilla.

Below is my code (this code resides on unix box):

<?php
    ob_start();
    $file = '/opt/hpws/apache/htdocs/barn/file2';

    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream;charset=utf-8');
        header('Content-Disposition: attachment; filename=$file');
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));

        readfile($file);
        exit;
    }
?>

Copyright Notice:Content Author:「theartist33」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/18677191/download-file-program-using-php-on-unix-box

Answers
Funk Forty Niner 2013-09-07T19:59:54

This line had quotation marks missing:\n\nheader('Content-Disposition: attachment; filename=$file');\n\n\nand in trying to use that line of code, the browser would prompt to save the file as $file.\n\nThe line of code should read as:\n\nheader('Content-Disposition: attachment; filename=\"'.basename($file).'\"');\n\n\nThe following (tested with a binary file) with file inside the same folder as executed code.\n\nNOTE: You could use header(\"Content-Type: application/text\"); if it's an ASCII file.\n\n<?php\n ob_start();\n $file = 'file.zip';\n\n if (file_exists($file)) {\n header('Content-Description: File Transfer');\n header('Content-Type: application/octet-stream;charset=utf-8');\n header('Content-Disposition: attachment; filename=\"'.basename($file).'\"');\n\n header('Content-Transfer-Encoding: binary');\n header('Expires: 0');\n header('Cache-Control: must-revalidate');\n header('Pragma: public');\n header('Content-Length: ' . filesize($file));\n\n readfile($file);\n exit;\n }\n?>\n",


More about “Download file program using php on unix box” related questions

Download file program using php on unix box

I have created a beginner program to forcefully download file from unix box to windows through browser, it is not throwing any error but shows nothing on browser just a blank page. PHP version- 5...

Show Detail

Download file program using PHP on HP-UX box

I'm having trouble modifying my PHP program. Originally, the program would download a particular file from a Unix box, which worked fine. Now I've modified it a little so the user can enter a file ...

Show Detail

Download file using FTP and PHP

I would like to start a upload and download website for huge files (up to 1gb). I thought that it would be better to download the files using FTP (with PHP), because this protocol is especially use...

Show Detail

Automatically download log file from Unix server to Windows machine

I have a Unix server on which a continuously running application generates a large text log. (aprox. 100megs an hour). My main development machine is a Windows computer and to see what's going on...

Show Detail

Download a file with alert box using PHP

I'm having a submit form where the form includes the following text fields First name Last name Email Password When user enters the general password of "DEFAULTDOC" it will automatically download...

Show Detail

Download html file using unix

I would like to download an html file using c++. I have some code that works with Visual Studio but I need it to run in unix and be able to be compiled with gcc. I found a lot of questions simila...

Show Detail

download wso2 identity server by using wget in unix

I am setting up WSO2 Identity server in my AWS EC2 instance. What is the URL to WGET so that i can download the installation.I cant find the URL in the website anywhere The version of identity serv...

Show Detail

Run java program on remote UNIX machine in JSP

I am developing Web Application in local machine using JSP. I want to execute a Java Program that resides on Unix box from my local JSP file. I have all the credentials of the UNIX box. Can you ple...

Show Detail

Unable to download the file from unix to windows using sftp connection

My requirement is to get the file from remote machine(Unix) to local machine(Unix/windows). When I run the code in Unix, I am able to download the file from Unix(remote) to Unix(local) but when I r...

Show Detail

BOX: How to download file using file url?

I want to use Box SDK to download a file. I have the file link available in local variable var url = https://mycompany.box.com/s/c565vhytyhx5s85vjg03bgtr0h47d6nh Currently BoxClient.FilesManager.

Show Detail