Hello friends,
I am working on downloading script. its working fine in all browsers.
but in IE when i click on download button its showing the size of every file zero bytes.
I have done lots of googling but didnot find anything. Please help its very urgent.

I am using the following function:-

function getDownloadData($fileid,$Finished='') {
					//echo $fileid;
			$url = "http://".$_SERVER['SERVER_NAME'] . '/uploads/fixfiles/';
					
				//$product = mysql_fetch_assoc(mysql_query("select * from products where  productId = '".$fileid."'"));
				 $file_path = $url.$fileid;
				//die;
				//echo $file_path = $server_path . 'uploads/products/downloads/'.$file;
				//die;
				$date = date('Y-m-d');
				 $sub = substr($fileid,-3);
				
				//echo $sub1 = substr($file_path,-3); die;
				$file_size =@filesize($file_path);
                                header("Pragma: public");
                                header("Expires: 0");
                                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                                header("Cache-Control: private",false);
				header('Last-Modified: '.date('r')); 
				header('Content-Description: File Transfer'); 
				header('Content-Type: '.returnMIMEType($sub).'');
				header("Content-disposition: attachment; filename=".$fileid."");
                               	header("Content-Length: ".$file_path."");
                                header('Expires: 0');
				ob_clean();
				flush();
				readfile($file_path);
				exit;
		}

Recommended Answers

All 9 Replies

You might want to try a different version of IE (e.g. from a different machine) to see if the problem is consistent. There are bugs and limitations that occur only in specific versions of IE. It won't really be a solution if it works in a different version but it might help to find out what's wrong.

I have already check in IE 7 and 8 and also checks on two different machines. But the problem is same. Can you suggest?

On line 21 you use a command returnMIMEType. I can't find any documentation on that so unless you have your own function with that name, I don't believe that it is valid. I get an error on that line when I tried your code. According to the documentation you can use a PECL extension (finfo_file) to get the MIME type but it didn't work for me (missing PECL?). When I changed line 21 to use image/png (because I tried it with a PNG file), it worked fine under IE8 and Chrome.

yes returnMIMEType is a custom function. please try to test with this function.

function returnMIMEType($ext)
    {
 
        switch(strtolower($ext))
        {
            case "js" :
                return "application/x-javascript";

            case "json" :
                return "application/json";

            case "jpg" :
            case "jpeg" :
            case "jpe" :
                return "image/jpg";

            case "png" :
            case "gif" :
            case "bmp" :
            case "tiff" :
                return "image/".strtolower($fileSuffix[1]);

            case "css" :
                return "text/css";

            case "xml" :
                return "application/xml";

            case "doc" :
            case "docx" :
                return "application/msword";

            case "xls" :
            case "xlt" :
            case "xlm" :
            case "xld" :
            case "xla" :
            case "xlc" :
            case "xlw" :
            case "xll" :
                return "application/vnd.ms-excel";

            case "ppt" :
            case "pps" :
                return "application/vnd.ms-powerpoint";

            case "rtf" :
                return "application/rtf";

            case "pdf" :
                return "application/pdf";

            case "html" :
            case "htm" :
            case "php" :
                return "text/html";

            case "txt" :
                return "text/plain";

            case "mpeg" :
            case "mpg" :
            case "mpe" :
                return "video/mpeg";

            case "mp3" :
                return "audio/mpeg3";

            case "wav" :
                return "audio/wav";

            case "aiff" :
            case "aif" :
                return "audio/aiff";

            case "avi" :
                return "video/msvideo";

            case "wmv" :
                return "video/x-ms-wmv";

            case "mov" :
                return "video/quicktime";

            case "zip" :
                return "application/zip";

            case "tar" :
                return "application/x-tar";

            case "swf" :
                return "application/x-shockwave-flash";

            default :
            return "application/octet-stream";
        }
    }

I made a few changes as shown below and the download worked in IE (and Chrome):

1. In returnMIMEType on line 21 you used $fileSuffix[1]. That is not defined. I changed that to $ext.

2. In getDownLoadData, on line 15 I changed header("Pragma: public"); to header("Pragma: no-cache");

3. In getDownLoadData, I deleted line 23 because it isn't needed and it isn't being given the right data and makes the downloaded file unusable.

When I checked, I found that the file I was able to download previously was not actually usable.

It is important and a lot less frustrating if you know how to debug these things for yourself. A few well placed echo's (or some actual debug function calls if you want to get a bit fancier) will tell you if you are getting the data that you expected. For the mime-type, it would have worked for most types but I happened to use a .png file and the code for that wasn't quite right. I did a separate call to the returnMIMEType function (before all the headers) and echo'd what came back. When I saw that I was getting 'image/' with no type I went and looked closer at the code and found the problem. In this case, I could have just looked at the code but if it was more complicated, using a bit of debug code will probably save time. For the header statements, I used a program of my own as a reference along with a bit of trial and error to see if the header statements were working correctly.

Even with these changes, there is no guarantee that it will work on your system but give it a try and see what happens.

Hey man its works. thats great. I know there must something wrong with the headers but i was not able to figure it out. keep it up.

how you managed to figure it out? i mean how you debug the code? can you share some knowledge here?

As far as the headers are concerned, I already have a program of my own that does downloads so I compared what I had with what you had and started experimenting. You had more header statements than I did so I tried it with and without some of the others to see if they had any impact. Once I had your returnMimeType routine working, I found that it would display the file but didn't give a chance to save it. By replacing your Pragma: Public with Pragma: no-cache it gave me the option to save it. I used no-cache in my own program. I didn't have a Content-Length: in my program so when I found that I couldn't open the downloaded file, I started trimming back your headers to see if that was the cause. After I removed Content-Length, the program worked. When I looked at that statement again it seemed likely that $file_path would not actually contain the file length.

So in conclusion:
1. Use your own work and others as a guide because most of us can't remember everything or do the full research on how every PHP command works. Do keep a copy of the PHP documentation handy and use it when your aren't sure of the command format, input or output.

2. Take advantage of the fact that there are many thousands of examples of php code on the internet. It's often easier to figure out how a piece of code that is already working does what it does (if it isn't too long and complex and if you really care) than to start from scratch and try to figure out why something isn't working.

3. Take advantage of the documentation and tutorials that are available on the net, especially W3Schools.

4. Let the machine do some of the work. Put in echo statements at key points if things aren't working to see if the data is what you expected it to be.

5. Get a debug tool and learn how to use it. This will go beyond what a simple echo statement can do in helping you to figure out why something isn't working. Most IDE's have a debug tool built in.

6. Be patient and just go step-by-step to confirm that what you expected to happen is actually happening. Also be persistent and don't give up. If you've given it a good try and still can't make sense of it, then consider Daniweb to get some help. If you don't spend the time and beat your head against the wall now and then, you never get any good at it and you will always be asking others to figure it out for you.

thanks for useful suggestions.
I have just download Netbeans IDE. and try to use debug tools in it.
can i Know your skype id?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.