473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Download Header Error Trapping

ak1dnar
1,584 Expert 1GB
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:
Expand|Select|Wrap|Line Numbers
  1. function file_down(fid)
  2. {
  3. window.location = "download.php?fid="+fid;
  4. }

Calling Function
[PHP]<a href="javascript: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_query($sql) or die("File Not Found : " . mysql_error());
$row = mysql_fetch_assoc($result);
$file = $row['p_man'];
$FILE_ROOT = "user_manuals/";
$path = $FILE_ROOT.$file;
if($file)
{
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($path));
header('Content-Disposition: attachment; filename="'.$file.'"');
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.
Mar 15 '07 #1
6 2290
vssp
268 100+
Can you send me the error so we can easily identify your problem
Mar 15 '07 #2
ak1dnar
1,584 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. Warning: filesize() [function.filesize]: stat failed for user_manuals/1.pdf in D:\AppServ\www\WEB_SITE\download.php on line 13
  2.  
  3. 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
  4.  
  5. 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
  6.  
  7. 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
  8.  
Mar 15 '07 #3
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
Mar 16 '07 #4
ak1dnar
1,584 Expert 1GB
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_manuals/";
$path = $FILE_ROOT.$file;

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.
Mar 17 '07 #5
devsusen
136 100+
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
Mar 17 '07 #6
ak1dnar
1,584 Expert 1GB
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. :)
Mar 18 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Frank Thorstens | last post by:
Hi, i try to give my script headers so the output text would be downloaded in the client's browser and not displayed. But it doesn't work at all in my IE 6 and Opera. <?...
5
by: Brandon Walters | last post by:
I wrote a file download module for my website. The reason for the file download module is that my website downloads work on a credit based system. So I need to keep track of and limit daily...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
3
by: Smriti Dev | last post by:
Hi There, I have the following code and when I try to run it, I get a type mismatch error. I would really appreciate your help with this. Thanks kindly, smriti --- Private Sub cmdOK_Click()
2
by: Captain Nemo | last post by:
I'm still using Office 2000 myself, but some of my clients have Office 2003. I've recently added a piece of code to create an instance of Word, open a document, fill in the blanks and become...
0
by: jinnareddy | last post by:
Hi, I'm unable to download a file that is having a 2-byte char in its name (e.g.テ) using force download option. Though, am able to download file names involving ASCII chars. I have tried URL...
14
by: Irfan12 | last post by:
i dont know php & i want a script that when a user click on a mp3 hyperlink, the mp3 file start downloading instead of start playing in media player. i got a sxript from internet search. i am using...
7
by: lawrence k | last post by:
I've got a music studio for a client. Their whole studio is run with Macintosh computers. Macintosh computers allow file names to have open white spaces, such as "animal hospital.mp3". I have a...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.