Download Header Error Trapping

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    Download Header Error Trapping

    Hi,
    I am creating script to download some files using download dialog box.First time i tried to use ajax for this, but i failed.
    So i went through this way,

    By Using this function i am going to submit the Data to process to PHP.
    JS Function:
    Code:
    function file_down(fid)
    {
    window.location = "download.php?fid="+fid;
    }

    Calling Function
    [PHP]<a href="javascrip t:file_down('.$ row['p_id'].')" ><img src="images/down.jpg" /></a>[/PHP]
    download.php
    [PHP] <?php
    $fid = $_REQUEST['fid'];
    require 'dbcon.php';
    $sql="SELECT p_man FROM products where p_id = '$fid'";
    $result=mysql_q uery($sql) or die("File Not Found : " . mysql_error());
    $row = mysql_fetch_ass oc($result);
    $file = $row['p_man'];
    $FILE_ROOT = "user_manua ls/";
    $path = $FILE_ROOT.$fil e;
    if($file)
    {
    header("Content-Type: application/octet-stream");
    header("Content-Length: " . filesize($path) );
    header('Content-Disposition: attachment; filename="'.$fi le.'"');
    readfile($path) ;
    }
    else
    {
    /*If the file Name is Not Available in the Table,
    I need to OPen another POP UP window to display the File Not Found Message.
    Or Any Other way to Display the Eror.
    */
    }
    ?>[/PHP]
    When I am sending ID from JS function to PHP side it will OPEN the save as Dialog box. But if the File Name is Missing in the table ERRORS will Display under download.php. What i need actually prevent this probleme. That means i want to I am not going to redirect th user back to the Download.php page if errors found.

    In that case from the first page itself i want to display a ERROR window.
    JS or HTMP Pop up.
  • vssp
    Contributor
    • Jul 2006
    • 268

    #2
    Can you send me the error so we can easily identify your problem

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      Errors will Get Based on two things here.
      One is in the Table Name of the File is Missing.
      second In the FILE_ROOT the file is Unavailable.

      I want trap those two errors. Again As i said earlier I need to display this Error Messages in this Manner.

      From the First Page ID will Pass to Dpwnload.php, from there if the is No Error Save as DIalog Box will display.
      If Not I need to Display the Error Like File Not Found. But User Should be there at the First page Not in the download.php page.
      Or elese first page is Still in the focus and File Not Found Pop up shoul Display. Any Idea.
      This Error is Getting if there is No File in The File Root DIR.

      Code:
      Warning: filesize() [function.filesize]: stat failed for user_manuals/1.pdf in D:\AppServ\www\WEB_SITE\download.php on line 13
      
      Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\WEB_SITE\download.php:13) in D:\AppServ\www\Akita\akita_store\download.php on line 13
      
      Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\WEB_SITE\download.php:13) in D:\AppServ\www\Akita\akita_store\download.php on line 14
      
      Warning: readfile(user_manuals/1.pdf) [function.readfile]: failed to open stream: No such file or directory in D:\AppServ\www\WEB_SITE\download.php on line 15

      Comment

      • merseyside
        New Member
        • Mar 2007
        • 48

        #4
        Try using the file_exists() function in the conditional of your if statement rather than the string. I think an empty or non empty string is always evaluated to true.

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          Originally posted by merseyside
          Try using the file_exists() function in the conditional of your if statement rather than the string. I think an empty or non empty string is always evaluated to true.

          http://uk2.php.net/manual/en/function.file-exists.php
          [PHP]$file = $row['p_man'];// File_name.pdf as the value to $file
          $FILE_ROOT = "user_manua ls/";
          $path = $FILE_ROOT.$fil e;

          if(file_exists( $path))
          {
          //headers goes here
          }[/PHP]

          Because of this $FILE_ROOT this will always evaluated to true. so my Php File will download.that means when there is no value in the table also this condition will set to true.

          Comment

          • devsusen
            New Member
            • Feb 2007
            • 136

            #6
            Hi,

            I think its better to use is_file() instead of file_exists() as the 2nd function can evaluate true if the path for the file exists. But is_file() will evaluate only true if the file really exists in the specified path.

            susen

            Comment

            • ak1dnar
              Recognized Expert Top Contributor
              • Jan 2007
              • 1584

              #7
              Originally posted by devsusen
              Hi,

              I think its better to use is_file() instead of file_exists() as the 2nd function can evaluate true if the path for the file exists. But is_file() will evaluate only true if the file really exists in the specified path.

              susen
              Thank you very much susen its a new function for me. thanks again. :)

              Comment

              Working...