Re: Fw: SUBIR ARCHIVOS GRANDES A LA BASES DE DATOS
| From: | Jabi | Date: | Thu, 10 Feb 2005 18:32:59 +0000 |
| Subject: | Re: Fw: SUBIR ARCHIVOS GRANDES A LA BASES DE DATOS | ||
| References: | 1 2 | Groups: | php.general.es |
| Request: | Send a blank email to general-es+get-53331@lists.php.net to get a copy of this message | ||
> Tengo este problema: estoy trabajando con un sistema que permite subir archivos a la base de
> datos desde una página WEB, pero al hacerlo con archivos superiores a 1M, me aparece la página del
> sistema que me indica que la página a la que se llama para grabar los datos del formulario no
> existe.
>
> Pero con archivos más pequeños funciona perfectamente.
>
El problema con los ficheros puede venir por dos ditios:
La configuración del apache o la configuración de MySQL.
Por defecto apache permite subir ficheros de 2 megas,
y creo que por defecto el max_packet_file de MySQL es de 1MB
Por lo que supongo que el fichero llegará en tu caso al servidor, y te
dará el error al hacer la insert contra el campo BLOB.
Para modificar el tamaño máximo de archivo permitido por apache puedes
probar bien modificando el .htaccess o bien por php con la funcion
ini_set (http://es2.php.net/manual/en/function.ini-set.php)
concretamente los valores post_max_size y upload_max_filesize
Para el tema del Max allowed packet en MySQL me da a mi que si no es un
server dedicado al que tengas acceso root es bastante complicado
cambiarlo.
Pero en un comentario el apartado file upload de php encontre una
solución que a mi por lo menos me ha convencido bastante.
(http://es.php.net/features.file-upload)
El título del comentario es "therebechips". Aunque lo pasteo a
continuación por si acaso...
Un saludo
Jabi
Re: Hadling uploads and downloads of large files and storing in MySQL.
Use two tables to store data about the file and the file data itself.
***Important: to preserve the integrity of the data use base64_encode()
NOT addslashes().
<?php
// Max packet size
define("MAX_SQL",50000);
$filehandle = fopen($tmp, "rb") or die( "Can't open file!" );
$query= "INSERT INTO files (name, type, size) VALUES(".
$DB->quote($name).", ".
$DB->quote($type).", ".
$DB->quote($size).
")";
// Execute Query
$result = $DB->query($query);
$file_id = mysql_insert_id();
// Copy the binary file data to the filedata table in sequential rows
each containing MAX_SQL bytes
// Your table should have an index set to auto_increment
// Store the file_id to identify the data fragments
while (!feof ($filehandle)) {
$data = base64_encode(fread($filehandle,MAX_SQL));
$query = "INSERT INTO filedata (file_id, data) VALUES($file_id,
\"".$data."\")";
$result = $DB->query($query);
}
fclose ($filehandle);
?>
Decode the data fragments and recombine them:
<?php
$file_id =$_GET ['file_id'];
$query ="select file_id, name, type, size from files where
file_id='$file_id'";
$result = $DB->query($query);
$row= mysql_fetch_array ($result);
$type = $row ["type"];
$name = $row ["name"];
$size = $row ["size"];
$file_id = $row ["file_id"];
// get the file data
$query = "select id, data from filedata where file_id='$file_id'
ORDER by id";
$result = $DB->query($query);
// decode the fragments and recombine the file
$data = "";
while ($row = mysql_fetch_array($result)) {
$data .= base64_decode($row ["data"]);
}
// output the file
header ("Content-type: $type");
header ("Content-length: $size");
header ("Content-Disposition: attachment; filename=$name");
header ("Content-Description: PHP Generated Data");
echo $data;
?>