Question

php readfile() function on windows 2008

Asked by: vetrixicode

Hello experts.

I have a php web application where a user can download a picture. It's been working for years on windows 2003,  but now we have moved to windows 2008, and it is not working good. The readfile function doesn't deliver the whole file, it always stops near the end of the file.
Please check the links bellow.

- The first is a direct link to the JPG file, it downloads completely.
http://v1.ativo.com/img_campanhas/volks_2008/rds_2474.jpg

- The second, is a php that uses readfile to send the same file as the first link. It doesn't download the entire file:
http://v1.ativo.com/volks_2008/download_foto.php?arquivo=rds_2474.jpg

What can be wrong?
Thanks a lot



// php Code that reads the file:
                header("Pragma: public");
                header("Expires: 0");
                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                header("Cache-Control: private",false);
                header("Content-Type: image/jpeg");
                header("Content-Transfer-Encoding: binary");
                header("Content-Length: ".@filesize($arq));
                set_time_limit(0);
                readfile($arq) or die("File not found.");

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-01-13 at 16:45:51ID24049670
Tags

php

,

windows 2008

,

readfile

Topics

PHP for Windows

,

PHP Scripting Language

,

Windows Server 2008

Participating Experts
2
Points
500
Comments
26

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Question about readfile()
    I am trying to record a 'download' in a mySQL db. What I have so far prompts the user to "save as" and I need some way to evaluate readfile() to see if the download was sucuesfull. here's what I have: <?php $file = $_REQUEST['file']; header("Pragma: pub...
  2. readfile() perplexing benchmark results
    I'm at the end of my rope here. I am dynamically creating images for people based on information I get from a database lookup. I decided to cache a lot of the images because there are potentially thousands of images to be made, but millions of hits will come in, making it ...
  3. readfile() on network share
    Hi, I need to use the readfile function to read a file that is on a network share. I tried everything on the php.net help page, but it still doesn't work. File_exists always returns false and the readfile function returns the following error: ----------------- Warning: readf...
  4. Trouble with php readfile, css background and sessions
    I’m new to sessions. Using PHP 4.3.11 Overview: Artists on my site store their images in many different categorized folders. Although image theft can’t be stopped; print screen, etc., I’ve been asked to make it difficult for the normal visitor to steal images. Two main goals...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: nizsmoPosted on 2009-01-13 at 16:54:03ID: 23369069

http://v1.ativo.com/img_campanhas/volks_2008/rds_2474.jpg

This link generates a 404?

Also, your picture seems to download completely OK on my end. What gives you the indication that the download stops before it is completed?

 

by: Ray_PaseurPosted on 2009-01-13 at 18:08:13ID: 23369446

Something might be amiss with the headers -- on my machine it did not recognize an image.  It thought it was trying to download a PHP file. Can you please post the rest of the download script?

 

by: vetrixicodePosted on 2009-01-14 at 09:44:46ID: 23375290

Tks guys.

The v1.ativo.com DNS record was recently updated. It should point to 209.17.171.46. Please check if your DNS is up to date. You´ll get a 404 error on the old server.

I´ve attached the complete code bellow. It receives a file name on the url, and send the image as an attachment to the user browser, this way the user saves the file, instead of the browser displaying it.  This code has worked for years.

Thanks for the help


<?php
//---------------------------------------------------------------------------------
// Complete code
//  Intercode
//  Arquivo: download_foto.php
//  Propósito: Faz o download do Arquivo solicitado
//---------------------------------------------------------------------------------
session_start();
include('fotos_config.php');
set_time_limit(0);
 
if (! $_GET['arquivo']){
        $erro .= "Arquivo não informada<br>";
} else {
        $arquivo = $_GET['arquivo'];
}
 
 
if ($arquivo){
        $arq = "$hard_path\\$arquivo";
 
        if (file_exists($arq)){
 
                header("Pragma: public");
                header("Expires: 0");
                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                header("Cache-Control: private",false);
                header("Content-Type: image/jpeg");
                header("Content-Type: application/force-download");
                header("Content-Disposition: attachment; filename=\"".basename($arq)."\";");
                header("Content-Transfer-Encoding: binary");
                header("Content-Length: ".@filesize($arq));
                set_time_limit(0);
                readfile("$arq") or die("File not found.");
 
 
        } else {
                $erro = "Arquivo " . $arq . " não está mais disponível para download.";
        }
} else {
        $erro = "Arquivo não encontrado.";
}
 
if ($erro) echo $erro;
 
?>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:

Select allOpen in new window

 

by: Ray_PaseurPosted on 2009-01-14 at 10:02:38ID: 23375484

This is still 404:
http://v1.ativo.com/img_campanhas/volks_2008/rds_2474.jpg

But see the code snippet.  Perhaps that will provide some inspiration.

best, ~Ray

<?php // RAY_force_download
 
function force_download($filename) {
	header("Content-Type: application/force-download");
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-length: ".(string)(filesize($filename)));
	header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
	header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
	header("Cache-Control: no-cache, must-revalidate");
	header("Pragma: no-cache");
	echo file_get_contents($filename);
}
 
force_download("http://v1.ativo.com/img_campanhas/volks_2008/rds_2474.jpg");
 
?>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:

Select allOpen in new window

 

by: vetrixicodePosted on 2009-01-29 at 04:29:25ID: 23496451

Hi Ray, thank you.

No, it didn't work, I got the same results.

You can check it using the link on the question, it is using your force_download() function now, but it just stops near the end of the file.

Anyone has any other ideias? Maybe a PHP bug on windows2008?

[]s

 

by: Ray_PaseurPosted on 2009-01-29 at 04:57:09ID: 23496658

Hi, vetrixicode: I just copied the code snippet out of my last post, stored it on my server (Linux) and ran it over the WWW from this desktop computer (WinXP).  

It downloaded the JPG perfectly.  I opened it in Photoshop and it looks fine - Adobe RGB colors.  I like that running outfit!

So the information is accessible over the www, and the data comes through to this machine intact.  That suggests that the download script is OK and the file is OK.  But it does not isolate the issue completely.  It could still be on the server or the client - both are on your end.  Maybe you can try running my stored copy of this script (from the Linux server) and see if it works for you.  If so, I would look toward the Windows server.  If not, it may be your client machine.  That's not a foolproof test, but the evidence will be useful.  Here is the URL:

http://www.laprbass.com/RAY_temp_vertixicode.php

Best regards, ~Ray

 

by: Ray_PaseurPosted on 2009-01-29 at 05:00:30ID: 23496688

Hmm... Interestingly EE's file uploader does not identify this as a JPG  (Possible File Type Mismatch).  Perhaps there is something about the file that is acceptable to Linux and Photoshop, but gives Windows a problem?  I clicked on the uploaded image in my post above and it came through fine.

What next?

 

by: vetrixicodePosted on 2009-01-29 at 12:13:26ID: 23501945

Hi Ray.

Both of your links worked, I've downloaded the image completely, BUT the image is not my image, it's a black BG with a red "INVALID IMAGE" text on it.

 

by: Ray_PaseurPosted on 2009-01-29 at 12:19:24ID: 23502044

It still works fine for me.  This time I saved it as a compressed JPG before uploading.

  • ativo.jpg
    • 47 KB

    Downloaded, saved and uploaded here.

    Downloaded, saved and uploaded here.
 

by: Ray_PaseurPosted on 2009-01-29 at 12:22:41ID: 23502075

So I am guessing that the issue may be isolated to your local ISP, server, or computer, maybe?  Can you test it from another Mac or PC, or via a different pipe to the internet?

Best regards, ~Ray

 

by: vetrixicodePosted on 2009-01-30 at 02:04:39ID: 23506602

I'm gonna try it at home.
Thank you.
[]s

 

by: Ray_PaseurPosted on 2009-01-30 at 05:42:14ID: 23508026

Makes sense; Let us know what you find.  Best regards, ~Ray

 

by: vetrixicodePosted on 2009-02-01 at 14:29:14ID: 23523352

Hi Ray.

I got the same results at home. I also tried several different pcs at the office, it doesn't work.
It just stops downloading.

What else can I do?

Thanks

 

by: Ray_PaseurPosted on 2009-02-02 at 12:32:40ID: 23531012

Do you and your office both use the same Internet Service Provider?

We tried to d/l in Safari on a Mac and it said, "Could not open the file, it may be corrupt or a format that Preview doesn't recognize".

We also tried it on a WinXP machine at a different ISP.  My colleague said, "It did not work for me. I tried both to open and then save to the desktop and open. It saved to my desktop but it wouldn't open."

So maybe I am the only one who can deal with it??  Anyway, the evidence is beginning to look like the file is defective in some way.

 

by: vetrixicodePosted on 2009-02-03 at 04:36:11ID: 23536430

Hi Ray.

My office and home are on different ISPs.
It's strange... it happens to all my files (several thousand) , and the thing is, the same files used to work just fine on my old win 2003 server.
And if the problem was on the file, it wouldn't work on the direct link (without the readfile() function), right? The direct link does work. ( http://v1.ativo.com/img_campanhas/volks_2008/rds_2474.jpg )

 

by: Ray_PaseurPosted on 2009-02-03 at 06:04:28ID: 23537148

Well, the direct link works for ME and for YOU, so I will ask some colleagues on different platforms to test the direct link.  I am also asking them to test this revised script.  I'll post back here as the results come in.

Best regards, ~Ray

<?php // RAY_force_download
 
function force_download($filename)
{
   $basename = basename($filename);
   $filedata = file_get_contents($filename);
 
   if ($filedata)
   {
      header("Content-Type: application/force-download");
      header("Content-Disposition: attachment; filename=\"$basename\"");
      header("Content-length: ".(string)(strlen($filedata)));
      header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
      header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
      header("Cache-Control: no-cache, must-revalidate");
      header("Pragma: no-cache");
      flush();
 
      echo $filedata;
   }
}
 
force_download("http://v1.ativo.com/img_campanhas/volks_2008/rds_2474.jpg");
 
?>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:

Select allOpen in new window

 

by: Ray_PaseurPosted on 2009-02-03 at 06:10:17ID: 23537202

We are able to open the file directly and with the script above on a Safari browser on a Mac.  You might want to try running that new script on your Windows server.  So far, it looks like it works on a Linux server.  Nothing in the notes on PHP.net gave me a clue about any known differences.

 

by: Ray_PaseurPosted on 2009-02-03 at 08:35:11ID: 23538888

Script also works on WinXP and WinVista with IE and FF.  So if this new script does not work for you, it looks like we may have a bug in Windows 2008.  You can test the script from my Linux machine here:

http://www.laprbass.com/RAY_temp_vertixicode.php

HTH, ~Ray

 

by: vetrixicodePosted on 2009-02-04 at 04:09:47ID: 23547132

Hi Ray

I've updated the function force_download(), but same results again...  it stops.

http://v1.ativo.com/volks_2008/download_foto.php?arquivo=rds_2474.jpg

Thanks

 

by: Ray_PaseurPosted on 2009-02-04 at 04:39:09ID: 23547356

I just used your link and downloaded exactly the same file as I downloaded yesterday, using my link (one post above).  The files match precisely, bit-for-bit.  In other words, it works.

However it took a VERY long time to download, and the phenomenon was repeatable.  I timed it and it took 2:15 to D/L when running the script from v1.ativo.com.  In contrast, it took 0:04 when running the script from www.laprbass.com.

Even over HTTP, a 700K image file should download in a matter of a few seconds, at most.  I wonder if there is something about the client on your end that is less patient than my client machine?  The image came from your server and worked successfully here, whether the script ran on your server or on my server.  The only difference was the huge D/L time from v1.ativo.com

Can you get the script at www.laprbass.com to work correctly?

Best regards, ~Ray

 

by: vetrixicodePosted on 2009-02-19 at 09:49:14ID: 23684181

Hi Ray, sorry it tookme too long to reply, I was out of town for several days.

Your download was steadly slow, or it was fast at the begining and slow at the end?

What should I test on www.laprbass.com ? PDFs downloaded just fine.

Thanks

 

by: Ray_PaseurPosted on 2009-02-19 at 11:58:19ID: 23685466

From the post above... Try this:
http://www.laprbass.com/RAY_temp_vertixicode.php

There is no way for me to know whether the D/L was steadily slow, etc.  It was fast from my server, and slow from ativo.

 

by: vetrixicodePosted on 2009-02-19 at 14:28:10ID: 23686941

The link you've sent me worked perfectly, it took me about 3 seconds.

What I meant is, when you downloaded the the file from v1.ativo.com it took 2:15, right? Whatching the download progress bar, can you notice any speed changes?  Or the progress bar starts slow, and goes slow to the end?

thanks for your help and persistence

 

by: Ray_PaseurPosted on 2009-02-19 at 15:40:25ID: 23687420

Right, it was slow from ativo.  Here is a screen capture of the process.  It took about 8 seconds to start this box, then the progress bar shot across to the end, and it hung for a long time just like this.

 

by: vetrixicodePosted on 2009-03-04 at 05:11:01ID: 23794318

hi Ray, I'm going to give up on this for now, the project has ended. I'll look at it again when needed.

I'm giving you the points for your help and persistence.

Thank you very much

 

by: Ray_PaseurPosted on 2009-03-04 at 05:14:35ID: 23794351

Thanks for the points - sorry there is not an obvious solution to this (other than to reinstall all the server software).  Best regards, ~Ray

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...