Download File Program Using Php On Hp-ux Box
Solution 1:
As Vlad says you are outputting content before caling headers - therefore your code is throwing errors hence your first task is to resolve the problem that you can't se the errors it is throwing.
Your code checks if the file exists - but not if it is readable.
The next problem is that, even if it did find readable content it would be returning it embedded within HTML tags.
Solution 2:
Below is my original code working fine:
<?php$file = '/opt/hpws/apache/htdocs/barn/file2';
if (!file_exists($file)) {
die("The file does not exist");
}
if (!is_file($file)) {
die("Not a file"); // Worry about symlinks later
}
if (!is_readable($file)) {
die("The file is not readable");
}
// die("DEBUG: Okay, will send the file -- remove this line and retry");$name = basename($file); // Or anything else
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"{$name}\"");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit();
?>
Solution 3:
EDIT
To run it as one file and using action=''
to accomplish this, try the following:
<?php
ob_start();
if(isset($_POST['name'])) {
$file = $_POST['name'];
echo"file is $file" ;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream;charset=utf-8');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
// header("Content-Type: application/text");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
readfile($file);
exit;
}
}
/*
if (!file_exists($file)) {
echo "<br>";
echo "$file does not exist.";
}
*/?><formaction=''method='post'><b> Enter the file name: </b><inputtype='text'name='name'><br><br><buttontype='submit'> Upload </button></form>
Original answer
Try it this way instead with two seperate files. (tested)
Note: File must reside inside the same folder as executed code and must be the exact file name.
I.e.:File.zip
and file.zip
would be treated as two different filenames because the first starts with an uppercase letter.
HTML form
<formaction='download_file.php'method='post'><b> Enter the file name: </b><inputtype='text'name='name'><br><br><buttontype='submit'> Upload </button></form>
Note: enctype='multipart/form-data'
is not necessary.
PHP hander (download_file.php)
<?php
ob_start();
if(isset($_POST['name'])) {
$file = $_POST['name'];
echo"file is $file" ;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream;charset=utf-8');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
// header("Content-Type: application/text");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
readfile($file);
exit;
}
}
if (!file_exists($file)) {
echo"<br>";
echo"$file does not exist.";
}
?>
Solution 4:
Below is the one working fine on my laptop but not on unix box..
enter code here
<!DOCTYPE html><html><body><?phpif(isset($_POST["name"]))
{
ob_start();
$file = $_POST["name"];
if(!file_exists($file))
{
echo"$file <br>";
die( "your file does not exist:");
}
elseif (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream;charset=utf-8');
header('Content-Disposition: attachment; filename="'.basename($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;
}
}
else
{
echo"<form action='download1.php' method='POST' enctype='html/form-data'>
Enter your file name : <input type='text' name='name' >
<br>
<br>
<button type='submit'>Submit</button>
</form>";
}
?></body></html>
Solution 5:
I got it solved, I just removed tags from my code and it worked fine..It seems that output was buffering and either it was not sent to browser or it could not come out of buffer to display or to function as per my understanding..thanks for your time guys...
Post a Comment for "Download File Program Using Php On Hp-ux Box"