Envio de ficheros a traves de formulario(olviden el anterior)
| From: | Webmaster Alphacom | Date: | Sat, 28 Dec 2002 12:37:37 +0000 |
| Subject: | Envio de ficheros a traves de formulario(olviden el anterior) | ||
| Groups: | php.general.es | ||
| Request: | Send a blank email to general-es+get-30777@lists.php.net to get a copy of this message | ||
Tengo un script para enviar ficheros a traves de un formulario, todo
lohace bien pero el archivo que me llega mal, a excepcion de los .txt y
.rtf, porlo que creo que se debe tratar de un problema en la
construccion del attach. Pongo aqui el codigo por si sirve de algo.
Gracias de antemano,
Javier Gálvez
El form es el siguiente:
<form action="PHPFormmail.php" method="post"
enctype="multipart/form-data" name="form" onSubmit="return
validate()">
El de script de envio(...)
function Attach( $filename,
$filetype='application/x-unknown-content-type', $disposition = "inline"
) {
// TODO : si filetype="", alors chercher dans un tablo de MT
connus / extension du fichier
$this->aattach[] = $filename;
$this->actype[] = $filetype;
$this->adispo[] = $disposition;
}
/* Get()
* return the whole e-mail , headers + message
* can be used for displaying the message in plain text
or logging it
*/
function Get() {
$this->_build_headers();
if( sizeof( $this->aattach > 0 ) ) {
$this->_build_attachement();
$this->body= $this->body . $this->attachment;
}
$mail = $this->headers;
$mail .= "\n$this->body";
return $mail;
}
(...)
function _build_attachement() {
$this->boundary= "------------" . md5( uniqid("myboundary") );
// TODO : variable bound
$this->headers .= "MIME-Version: 1.0\nContent-Type:
multipart/mixed;\n boundary=\"$this->boundary\"\n\n";
$this->fullBody = "This is a multi-part message in MIME
format.\n--$this->boundary\nContent-Type: text/plain;
charset=us-ascii\nContent-Transfer-Encoding: 7bit\n\n" . $this->body
."\n";
$sep= chr(13) . chr(10);
$ata= array();
$k=0;
// for each attached file, do...
for( $i=0; $i < sizeof( $this->aattach); $i++ ) {
$filename = $this->aattach[$i];
$basename = basename($filename);
$ctype = $this->actype[$i]; // content-type
$disposition = $this->adispo[$i];
if( ! file_exists( $filename) ) {
echo "Class Mail, method attach : file $filename
can't be found"; exit;
}
$subhdr= "--$this->boundary\nContent-type: $ctype;\n
name=\"$basename\"\nContent-Transfer-Encoding:
base64\nContent-Disposition: $disposition;\n filename=\"$basename\"\n";
$ata[$k++] = $subhdr;
// non encoded line length
$linesz= filesize( $filename)+1;
$fp= fopen( $filename, 'r' );
$data= base64_encode(fread( $fp, $linesz));
fclose($fp);
$ata[$k++] = chunk_split( $data );
}
$this->attachment= implode($sep, $ata);
}
} // class Mail
(...)