PHP readfile(); the below code works on wamp but not on actual server
Posted by: peterfgoossens@gmail.com (190.141.101.---)
Date: July 21, 2015 09:04PM

Below is the relevant code. On the wamp server it works fine (file downloaded), but on the actual server the content of the file is echoed to the screen (without a specific instruction) but the file is not downloaded. Can somebody help me?

$FileName = $File; //"TestFile.txt";

Print ($FileName);

$len = filesize($File); // Calculate File Size

print ($len);

if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
if (file_exists("TestFile.txt"winking smiley) {
header('Content-Description: File Transfer');
header('Content-Type: text/txt');
header('Content-Disposition: attachment; filename=TestFile.txt');
header('Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . $len);
ob_clean();
flush();
@readfile("$File"winking smiley;
exit;
} else {
echo "no file";
}

Options: ReplyQuote
Re: PHP readfile(); the below code works on wamp but not on actual server
Posted by: RiggsFolly (---.as43234.net)
Date: July 22, 2015 01:25AM

Well normally you have to send headers before you output anything else, so I would suggest removing the

Print ($FileName);

and

print ($len);



What is the point in moving $File into $FileName?

- $FileName = $File; //"TestFile.txt";

and then not using $File or $Filename


Why do an ob_clean() followed by an ob_flush() to send a just cleaned and emptied buffer to the client?


Try a header('Content-Type: text/plain) or header('Content-Type: application/octet-stream');


    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=TestFile.txt');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize('TestFile.txt'));
    readfile('TestFile.txt');
    exit;

---------------------------------------------------------------------------------------------
(Windows 10 Pro 64bit) (Wampserver 3.2.5 32bit & 64bit)
<Apache versions MULTIPE> <PHP versions MULTIPLE> <MySQL Versions MULTIPLE>
<MariaDB versions MULTIPLE> <phpMyAdmin versions MULTIPLE> <MySQL Workbench 8.0.23>

Read The Manuals Apache -- MySQL -- PHP -- phpMyAdmin
Get your Apache/MySQL/mariaDB/PHP ADDONs here from the WAMPServer alternate Repo
-X-X-X- Backup your databases regularly Here is How dont regret it later! Yes even when developing -X-X-X-



Edited 1 time(s). Last edit at 07/23/2015 09:23PM by RiggsFolly.

Options: ReplyQuote
Re: PHP readfile(); the below code works on wamp but not on actual server
Posted by: peterfgoossens@gmail.com (190.141.101.---)
Date: July 22, 2015 05:19PM

Thanks RiggsFolly. I changed all that but same result. Any other ideas?

The issue about $File and $Filename is that at one stage (changing everything there was to changed I changed it all to "TestFile.txt"

Options: ReplyQuote
Re: PHP readfile(); the below code works on wamp but not on actual server
Posted by: RiggsFolly (---.as43234.net)
Date: July 22, 2015 05:34PM

I think your live server must be configured differently and is having issues with the mime types you have tried, which is a little wierd as they are not in any way special.


Is the live server accesible to you or is it hosted?

---------------------------------------------------------------------------------------------
(Windows 10 Pro 64bit) (Wampserver 3.2.5 32bit & 64bit)
<Apache versions MULTIPE> <PHP versions MULTIPLE> <MySQL Versions MULTIPLE>
<MariaDB versions MULTIPLE> <phpMyAdmin versions MULTIPLE> <MySQL Workbench 8.0.23>

Read The Manuals Apache -- MySQL -- PHP -- phpMyAdmin
Get your Apache/MySQL/mariaDB/PHP ADDONs here from the WAMPServer alternate Repo
-X-X-X- Backup your databases regularly Here is How dont regret it later! Yes even when developing -X-X-X-

Options: ReplyQuote
Re: PHP readfile(); the below code works on wamp but not on actual server
Posted by: peterfgoossens@gmail.com (190.141.101.---)
Date: July 22, 2015 08:32PM

It is hosted (GoDaddy), so pretty big and I cannot imagine it to be anything but standard
(?).

Options: ReplyQuote
Re: PHP readfile(); the below code works on wamp but not on actual server
Posted by: RiggsFolly (---.as43234.net)
Date: July 22, 2015 08:45PM

Good point.

Maybe it would be worth placing a ticket on them to see if they can identify what the difference is.

---------------------------------------------------------------------------------------------
(Windows 10 Pro 64bit) (Wampserver 3.2.5 32bit & 64bit)
<Apache versions MULTIPE> <PHP versions MULTIPLE> <MySQL Versions MULTIPLE>
<MariaDB versions MULTIPLE> <phpMyAdmin versions MULTIPLE> <MySQL Workbench 8.0.23>

Read The Manuals Apache -- MySQL -- PHP -- phpMyAdmin
Get your Apache/MySQL/mariaDB/PHP ADDONs here from the WAMPServer alternate Repo
-X-X-X- Backup your databases regularly Here is How dont regret it later! Yes even when developing -X-X-X-

Options: ReplyQuote
Re: PHP readfile(); the below code works on wamp but not on actual server
Posted by: peterfgoossens@gmail.com (190.141.101.---)
Date: July 23, 2015 05:02PM

Tried that too. According to them nothing special from their side!

Would you know why the content of the file echoes to the screen? Maybe the answer is in there somewhere

Options: ReplyQuote
Re: PHP readfile(); the below code works on wamp but not on actual server
Posted by: peterfgoossens@gmail.com (190.141.101.---)
Date: July 23, 2015 05:28PM

Changed the readfile() to :

$wasdownloaded = readfile("TestFile.txt"winking smiley;
If ($wasdownloaded) { echo "yes";} else { echo "no";}

and I get a "yes"

??

Options: ReplyQuote
Re: PHP readfile(); the below code works on wamp but not on actual server
Posted by: Otomatic (Moderator)
Date: July 23, 2015 06:22PM

Hi,

When you have something that is not working or wrong, the first thing to do is look at the error log ... but to do so would require that errors are not masked and remove the "@" of function calls.

A second thing is to thoroughly test the return value of a function.

PHP documentation for readfile said:
Returns the number of bytes read from the file. If an error occurs, FALSE is returned and unless the function was called as @readfile(), an error message is printed.

You test:
If ($wasdownloaded)
that is to say, you verify that the result is true, but it is not what to do, you have to test if the result is false.
if ($wasdownloaded === false)
  echo "error";
else
  echo "no error";
If the file exists but is empty, this will not be considered true, but it is not false either because there will be no errors.

------------------------------------------------------------------------------------------------------------
Wampserver 3.2.7 32 bit - Apache 2.4.51 - PHP 8.1.0…5.6.40 - MySQL 5.7.36 - MariaDB 10.5.13/10.6.5
Wampserver 3.2.7 64 bit - Apache 2.4.51 - PHP 8.1.0…5.6.40 - MySQL 5.7.36/8.0.27 - MariaDB 10.5.13/10.6.5
PhPMyadmin 5.1.1 - MysqlDumper 1.24.5
on W10 and W7 Pro 64 bit
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons
« Ce n'est pas parce qu'ils sont nombreux à avoir tort, qu'ils ont forcément raison. Coluche »
« It's not because they are many to be wrong, they are necessarily right. Coluche »

Options: ReplyQuote
Re: PHP readfile(); the below code works on wamp but not on actual server
Posted by: peterfgoossens@gmail.com (190.141.101.---)
Date: July 23, 2015 07:50PM

I get a "no error", so from the server side the download happened (is that true, by the way?). So what puzzles me is where does it go wrong then. On my side? And what could cause that?

Options: ReplyQuote
Re: PHP readfile(); the below code works on wamp but not on actual server
Posted by: peterfgoossens@gmail.com (190.141.101.---)
Date: July 23, 2015 07:55PM

And if I print out the value of $wasdownloaded then I get the correct file length.

Options: ReplyQuote
Re: PHP readfile(); the below code works on wamp but not on actual server
Posted by: peterfgoossens@gmail.com (190.141.101.---)
Date: July 30, 2015 05:46PM

I (finally got it resolved. The problem was that I had outputted something before the download function. By moving the download function to a separate file, it worked!!!!

Options: ReplyQuote
Re: PHP readfile(); the below code works on wamp but not on actual server
Posted by: RiggsFolly (---.as43234.net)
Date: July 30, 2015 06:03PM

Wasn't that the jist of my FIRST reply!

---------------------------------------------------------------------------------------------
(Windows 10 Pro 64bit) (Wampserver 3.2.5 32bit & 64bit)
<Apache versions MULTIPE> <PHP versions MULTIPLE> <MySQL Versions MULTIPLE>
<MariaDB versions MULTIPLE> <phpMyAdmin versions MULTIPLE> <MySQL Workbench 8.0.23>

Read The Manuals Apache -- MySQL -- PHP -- phpMyAdmin
Get your Apache/MySQL/mariaDB/PHP ADDONs here from the WAMPServer alternate Repo
-X-X-X- Backup your databases regularly Here is How dont regret it later! Yes even when developing -X-X-X-

Options: ReplyQuote
Re: PHP readfile(); the below code works on wamp but not on actual server
Posted by: peterfgoossens@gmail.com (190.141.101.---)
Date: July 31, 2015 08:57PM

I know .. !

Options: ReplyQuote


Sorry, only registered users may post in this forum.