Ver Mensaje Individual
  #3 (permalink)  
Antiguo 15/06/2012, 16:38
experimenta
 
Fecha de Ingreso: abril-2010
Mensajes: 159
Antigüedad: 14 años
Puntos: 11
Respuesta: Problema con php de upload

El tamaño se coloca en bytes
15*1024*1024 = 15728640

Código PHP:
<?php
//ESTA FUNCION LA USAREMOS PARA OBTENER EL TAMAÑO DE NUESTRO ARCHIVO
function filesize_format($bytes$format ''$force ''){
    
$bytes=(float)$bytes;
    if (
$bytes <1024){
        
$numero=number_format($bytes0'.'',');
        return array(
$numero,"B");
    }
    if (
$bytes <1048576){
        
$numero=number_format($bytes/10242'.'',');
        return array(
$numero,"KBs");
    }
    if (
$bytes>= 1048576){
        
$numero=number_format($bytes/10485762'.'',');
        return array(
$numero,"MB");
    }
}
//VERIFICAMOS QUE SE SELECCIONO ALGUN ARCHIVO
if(sizeof($_FILES)==0){
    echo 
"No se puede subir el archivo";
    exit();
}
// EN ESTA VARIABLE ALMACENAMOS EL NOMBRE TEMPORAL QU SE LE ASIGNO ESTE NOMBRE ES GENERADO POR EL SERVIDOR
// ASI QUE SI NUESTRO ARCHIVO SE LLAMA foto.jpg el tmp_name no sera foto.jpg sino un nombre como SI12349712983.tmp por decir un ejemplo
$archivo $_FILES["archivo"]["tmp_name"];
//Definimos un array para almacenar el tamaño del archivo
$tamanio=array();
//OBTENEMOS EL TAMAÑO DEL ARCHIVO
$tamanio $_FILES["archivo"]["size"];
//OBTENEMOS EL TIPO MIME DEL ARCHIVO
$tipo $_FILES["archivo"]["type"];
//OBTENEMOS EL NOMBRE REAL DEL ARCHIVO AQUI SI SERIA foto.jpg
$nombre_archivo $_FILES["archivo"]["name"];
//PARA HACERNOS LA VIDA MAS FACIL EXTRAEMOS LOS DATOS DEL REQUEST
extract($_REQUEST);
//VERIFICAMOS DE NUEVO QUE SE SELECCIONO ALGUN ARCHIVO
if ( $archivo != "none" ){
    
//ABRIMOS EL ARCHIVO EN MODO SOLO LECTURA
    // VERIFICAMOS EL TAÑANO DEL ARCHIVO
    
$fp fopen($archivo"rb");
    
//LEEMOS EL CONTENIDO DEL ARCHIVO
    
$contenido fread($fp$tamanio);
    
//CON LA FUNCION addslashes AGREGAMOS UN \ A CADA COMILLA SIMPLE ' PORQUE DE OTRA MANERA
    //NOS MARCARIA ERROR A LA HORA DE REALIZAR EL INSERT EN NUESTRA TABLA
    
$contenido addslashes($contenido);
    
//CERRAMOS EL ARCHIVO
    
fclose($fp);
    
// VERIFICAMOS EL TAÑANO DEL ARCHIVO <-- Aki verifica el tamaño
    
if ($tamanio <15728640){
        
//HACEMOS LA CONVERSION PARA PODER GUARDAR SI EL TAMAÑO ESTA EN b ó MB
        
$tamanio=filesize_format($tamanio);
    }
    
    
//CREAMOS NUESTRO INSERT
    
$qry "INSERT INTO tbl_carrera_1 ( curso,titulo,nombre_archivo, descripcion, contenido, tamanio,tamanio_unidad, tipo ) VALUES
    ('$curso','$titulo','$nombre_archivo', '$descripcion','$contenido','{$tamanio[0]}','{$tamanio[1]}', '$tipo')"
;
    
    
//NOS CONECAMOS A LA BASE DE DATOS
    //REMPLAZEN SUS VALOS POR LOS MIOS
    
mysql_connect("localhost","user","pass") or die("No se pudo conectar a la base de datos");
    
    
//SELECCIONAMOS LA BASE DE DATOS CON LA CUAL VAMOS A TRABAJAR CAMBIEN EL VALOR POR LA SUYA
    
mysql_select_db("base de datos");
    
    
//EJECUTAMOS LA CONSULTA
    
mysql_query($qry) or die("Query: $qry <br />Error: ".mysql_error());
    
    
//CERRAMOS LA CONEXION
    
mysql_close();
    
//NOTIFICAMOS AL USUARIO QUE EL ARCHVO SE HA ENVIADO O REDIRIGIMOS A OTRO LADO ETC.
    
echo "Documento Subido Correctamente<br>";
    echo 
'<a href="form.html">Subir Otro Archivo</a><br > ';
}else{
    echo 
"No fue posible subir el archivo";
    echo 
'<a href="form.html">Subir Otro Archivo</a><br > ';
}
?>