Come da oggeto del post ho un problema nel salvare gli allegati..

questo è il codice per visualizzare gli allegati:

Codice PHP:
$structure imap_fetchstructure($inbox$id);

$structure=imap_fetchstructure($inbox$id);

if(isset(
$structure->parts)) {

echo 
"<tr><td>Allegati:</td><td>";

while(list(
$key,$val) = each($structure->parts)) {

if(
$key 0) {

if(
$val->ifparameters) {

$attach=$val->parameters;

echo 
"<a href='gotodownload.php?id=$id&part=$key' TARGET=blank>";

$attachmenttext=$attach[0]->value;

$attachmenttext=str_replace("US-ASCII""HTML/HTM Document"$attachmenttext);

$attachmenttext=str_replace("us-ascii""HTML/HTM Document"$attachmenttext);

echo 
$attachmenttext;

echo 
"</a>,";

} else {

echo 
"<a href='gotodownload.php?id=$id&part=$key' TARGET=blank>";

echo 
"(unnamed)</a>
"
;
}}
}}

if(isset(
$structure->parts)) {

$body=imap_fetchbody($inbox$id,1);

} else {

$body=imap_body($inbox$id);

}
$partone $structure;
$parttwo $partone->parts[0];
if(
$parttwo->encoding == 3) {

$ambody imap_base64($body);

e questo è gotodownload.php

Codice PHP:
$mimetype=array("text","multipart","message","application","audio","image","video","other");



$structure=imap_fetchstructure($inbox$id);

$attach_body imap_fetchbody($inbox$id,$part+1);

$atype=$structure->parts[$part]->type;

if(
$atype==""$atype=0;

$asubtype=$structure->parts[$part]->subtype;

if(
$asubtype == ""$asubtype='PLAIN';

$attach_type=$mimetype[$atype]."/".$asubtype;

$attach_name=$structure->parts[$part]->parameters[0]->value;

if(
$attach_name ""$attach_name='(unnamed)';



$anencode=$structure->parts[$part]->encoding;

if(
$anencode == 3){

$return_content=imap_base64($attach_body);

} elseif(
$anencode == 4) {

$return_content=imap_qprint($attach_body);

} else {

$return_content=$attach_body;

}

header("Content-type: $attach_type");

if((
$atype != 0) && ($atype != 5)){

header("Content-Disposition: attachment; filename=$attach_name");

}

echo 
$return_content
quando clikko sul link dell'allegato, mi fa salvare o aprire il file gotodownload.php, invece dell'allegato!
Avete qualche consiglio?

Grazie