problem with download files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

problem with download files

Post by dizeta »

hi,

i have a problem with download files:

Code: Select all

<a href=\"actions.php?action=download&id={$result["id_manu"]}\">download</a>
this querystring works well, i can download the file and i can open it correctly.

this one:

Code: Select all

<a href=\"index.php?page=auth/actions&action=download&id={$result["id_manu"]}\">download</a>
it doesn't work, i can' t open fdf or doc files...why??

actions.php is the same file in 2 different directories.
i tried to print the $vars that i send from download page to actions.php.

first one ( works )

Code: Select all

Array
(
    [action] => download
    [id] => 9
    [uid] => 9445687aeb726ab3fb8d2e5ebd44f9ee
)
second one ( doesn't work )

Code: Select all

Array
(
    [page] => auth/actions
    [action] => download
    [id] => 9
    [uid] => 9445687aeb726ab3fb8d2e5ebd44f9ee
)
as you can see, no differences

8O
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I can see a difference... there is a 'page' indice on the second one..

And it'll be the '/' in "auth/actions" causing you grief.
Last edited by Jenk on Thu Oct 27, 2005 8:19 am, edited 2 times in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Perhaps if you post the code that handles the download :)
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

Code: Select all

if(!isset($_GET)) $_GET = $HTTP_GET_VARS;

if($_GET["action"] && $_GET["id"] && is_numeric($_GET["id"])) {

include("dbconnect");

switch($_GET["action"]) {


case "view" :

$query = "SELECT * FROM manuali WHERE id_manu = '" . $_GET["id"] . "'";
$select = @mysql_query($query) or die("Failure !");

$result = @mysql_fetch_array($select);

$data = $result["bin_data"];
$type = $result["filetype"];

Header("Content-type: $type");
echo $data;

break;

// DOWNLOAD
case "download" :

$query = "SELECT * FROM manuali WHERE id_manu = '" . $_GET["id"] . "'";
$select = @mysql_query($query) or die("Failure !");

$result = @mysql_fetch_array($select);

$data = $result["bin_data"];
$name = $result["filename"];
$type = $result["filetype"];

// INTERNET EXPLORER
if(ereg("MSIE ([0-9].[0-9]{1,2})", $_SERVER["HTTP_USER_AGENT"])) {

header("Content-Type: application/octetstream");
header("Content-Disposition: inline; filename=$name");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");

} else {

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$name");
header("Expires: 0");
header("Pragma: no-cache");

}

echo $data;

break;

default :

// DEFAULT CASE, NO ACTIONS

break;

} // endswitch

@mysql_close();

} //endif
And it'll be the '/' in "auth/actions" causing you grief.
sorry, what i have to do? :?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

<!-- Ignore me -->
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Hmmm.... I can't see anything obvious and I doubt that it is the "/" that's the issue but just in case try replacing the "/" in the URL with "%2F".

urlencode() could do that for you ;) Not sure if it'll make a difference.
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

i've tried with urlencode

Code: Select all

$path = "auth/actions";
<a href=\"index.php?page=".urlencode($path)."&action=download&id={$result["id_manu"]}\">download</a>
but no changes...

well, maybe i've forgot to tell you that download works, but i can't open the file doc or pdf.
the file results corrupt...

:?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Just a thought, but try stripslashes($data) on the download part.... maybe it's got backslashes in that shouldn't be there ;)
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

:( no changes again


i've tried to print the .doc inside the page

Code: Select all

echo "<p style=\"color:blue\">".stripslashes($data)."</p><br />";
but still results corrupt

this is a big problem for me... :x
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why are you trying to show either type of file inside a page? They are both binary formats and not intended to be displayed in such a manor. Simply send the file with readfile() with the correct headers
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

hi,

ok, it was just a test...

correct headers?

the scripts with headers that i've posted are not correct?

as i've posted at beginning of this post, my problem is download files with :

<a href=\"index.php?page=auth/actions&action=download

the other querystring

<a href=\"actions.php?action=download

works correctly ( with the same headers, same file)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

url encode the keys and values for each key-value pair used in the query string.
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

feyd,

you mean something like this?

Code: Select all

$path = "page=auth/actions";
$path1 = "action=download";
$path2 = "id={$result['id_manu']}";

<a href=\"index.php?".urlencode($path)."&".urlencode($path1)."&".urlencode($path2)."\">download</a>
if yes, when i click on download link, i'm redirect to the homepage. download doesn't start, i don't receive nothing to the control script ( action ).....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sort-of... encode the key separate from the value, then attach each pair together like normal (equal sign) then attach all the parameters together like normal (ampersand)
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

ok, feyd

I hope to have understood, if not i'm sorry.. :?

Code: Select all

$path = "page";
$path1 = "auth/actions";
$path2 = "action";
$path3 = "download";
$path4 = "id";
$path5 = "{$result['id_manu']}";

<a href=\"index.php?".urlencode($path)."=".urlencode($path1)."&".urlencode($path2)."=".urlencode($path3)."&".urlencode($path4)."=".urlencode($path5)."\">download</a>
same problem...
Post Reply