在HP-UX上使用PHP下载文件程序

时间:2013-09-09 08:05:10

标签: php html forms unix

我在修改PHP程序时遇到问题。最初,该程序将从Unix盒中下载一个特定的文件,该文件工作正常。现在我对它进行了一些修改,以便用户输入要下载的文件名。

现在它不起作用了,我不知道为什么。它不会抛出我能看到的任何错误;页面只返回空白。

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

代码:
    

       <html>

      <body>


      <?php

      ob_start();
      if(isset($_POST['name']))

       {

       $file = $_POST['name'];
      echo "file is  $file" ;
     if(!file_exists($file))

       {

     die("file not found: " );

     }


       $name = basename($file);
            header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
          header('Content-Disposition: attachment; filename="'.$name.'"');

          header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
           header('Cache-Control: must-revalidate');
          header('Pragma: public');
            header('Content-Length: ' . filesize($file));
             ob_clean();
         readfile($file);
            exit;

             }

           else

              {
             echo " <form action='download1.php' method='post' enctype='multipart/form-data'>


       <b> Enter the file name: </b><input type='text' name='name'>
        <br> <br>
        <button type='submit'> Upload </button>

     </form>";
          }
      ?>
         </body>

         </html>

我做错了什么?

5 个答案:

答案 0 :(得分:0)

正如弗拉德所说,你在调用标题之前输出内容 - 因此你的代码正在抛出错误因此你的第一个任务就是解决你无法解决它抛出的错误的问题。

您的代码会检查文件是否存在 - 但如果文件是可读的则不会。

接下来的问题是,即使它确实找到了可读内容,它也会将其返回到HTML标记中。

答案 1 :(得分:0)

以下是我的原始代码正常工作:

<?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();
?>

答案 2 :(得分:0)

修改

要将其作为一个文件运行并使用action=''完成此操作,请尝试以下操作:

<?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.";
}
*/
?>

<form action='' method='post'>
<b> Enter the file name: </b><input type='text' name='name'>
<br> <br>
<button type='submit'> Upload </button>
</form>

原始答案

尝试这种方式而不是两个单独的文件。 (测试)

注意:文件必须与执行的代码位于同一文件夹中,并且必须是确切的文件名。

即: File.zipfile.zip将被视为两个不同的文件名,因为第一个以大写字母开头。

HTML表单

<form action='download_file.php' method='post'>
<b> Enter the file name: </b><input type='text' name='name'>
<br> <br>
<button type='submit'> Upload </button>
</form>

注意: enctype='multipart/form-data'不是必需的。

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.";
}
?>

答案 3 :(得分:0)

下面是我的笔记本电脑上的一个正常工作,但没有在unix盒上工作..

enter code here

        <!DOCTYPE html>

           <html>

            <body>




             <?php

     if(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>

答案 4 :(得分:0)

我得到了解决,我刚从我的代码中删除了标签并且工作正常......似乎输出是缓冲的,要么它没有发送到浏览器,要么它不能从缓冲区中显示出来或按功能运行我的理解......谢谢你的时间......