/    Sign up×
Community /Pin to ProfileBookmark

Download script inserting line feeds into end of downloaded file

I have a download script that inserts data into a database table when someone downloads a file. For some reason, the insert is adding several linefeeds to the end of the file being downloaded even if I comment out the database code. If I remove the insert code everything works as expected. Can someone see what I’m doing wrong?

“`

<?php

$php_scripts = ‘../../php/’;
require $php_scripts . ‘PDO_Connection_Select.php’;
require $php_scripts . ‘GetUserIpAddr.php’;
function mydloader($l_filename=NULL)
{
$ip = GetUserIpAddr();
if (!$pdo = PDOConnect(“foxclone_data”))
{
exit;
}
if( isset( $l_filename ) ) {
header(‘Content-Type: octet-stream’);
header(“Content-Disposition: attachment; filename={$l_filename}”);
header(‘Pragma: no-cache’);
header(‘Expires: 0’);
readfile($l_filename);
$stmt = $pdo->prepare(“INSERT INTO download (address, filename) VALUES (?, ?)”);
$stmt->execute([$ip, $l_filename]) ;
}
else {
echo “isset failed”;
}
}
mydloader($_GET[“f”]);

“`

Thanks in advance

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 24.2020 — Does the script have a closing ?&gt; tag? If so, delete it, so that any white-space after it does not get output. Additionally, you may want to stick in an exit; at the earliest place that makes sense after the file is output, so that nothing unneeded that might output anything gets processed.
Copy linkTweet thisAlerts:
@larry29936authorMay 24.2020 — @NogDog#1618764

There is no ?> closing tag. What do you think about this?

if( isset( $l_filename ) ) {


header('Content-Type: octet-stream');

header("Content-Disposition: attachment; filename={$l_filename}");

header('Pragma: no-cache');

header('Expires: 0');


readfile($l_filename);

}

{

$stmt = $pdo->prepare("INSERT INTO download (address, filename) VALUES (?, ?)");

$stmt->execute([$ip, $l_filename]) ;

}
Copy linkTweet thisAlerts:
@NogDogMay 25.2020 — If anything is being output after the end of the file, then presumably it's after the readfile() call . . . unless whatever is _uploading_ the file is adding it? 🤔

Assuming not, then if the last thing you need to do is make the DB insert, you could just add an exit; right after the $stmt-&gt;execute(...); call, perhaps, or at the very least, right after your myloader(...); call to that function?
Copy linkTweet thisAlerts:
@larry29936authorMay 25.2020 — @NogDog#1618771 The upload is definitely not changing the file. The md5sum is checked before upload and is calculated and displayed on the website, they match.
Copy linkTweet thisAlerts:
@larry29936authorMay 25.2020 — @NogDog#1618771 I modified the script as follows and it seems to be working correctly:
``<i>
</i>
$php_scripts = '../../php/';
require $php_scripts . 'PDO_Connection_Select.php';
require $php_scripts . 'GetUserIpAddr.php';
function mydloader($l_filename=NULL)
{
$ip = GetUserIpAddr();
if (!$pdo = PDOConnect("foxclone_data"))
{
exit;
}
if( isset( $l_filename ) ) {
$stmt = $pdo-&gt;prepare("INSERT INTO download (address, filename) VALUES (?, ?)");
$stmt-&gt;execute([$ip, $l_filename]) ;

header('Content-Type: octet-stream');
header("Content-Disposition: attachment; filename={$l_filename}");
header('Pragma: no-cache');
header('Expires: 0');
readfile($l_filename);
}
else {
echo "isset failed";
}
}
mydloader($_GET["f"]);
exit;<i>
</i>
``

Thanks for the help
×

Success!

Help @larry29936 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...