Foros del Web » Programando para Internet » Javascript » Frameworks JS »

upload de archivos: función que cambia el nombre del archivo a subir

Estas en el tema de upload de archivos: función que cambia el nombre del archivo a subir en el foro de Frameworks JS en Foros del Web. Hola a todos! Tengo el siguiente scrip que realiza un upload de fotos y muestra el thumbail debajo. Funciona perfectamente pero hay una funcion que ...
  #1 (permalink)  
Antiguo 12/05/2010, 11:06
 
Fecha de Ingreso: mayo-2002
Ubicación: Capital Federal
Mensajes: 630
Antigüedad: 22 años
Puntos: 1
upload de archivos: función que cambia el nombre del archivo a subir

Hola a todos! Tengo el siguiente scrip que realiza un upload de fotos y muestra el thumbail debajo. Funciona perfectamente pero hay una funcion que al subir el archivo al servidor, lo convierte en números y lo que yo quisiera es que mantenga el nombre del mismo.

El código que hace el upload es el siguiente:
Código PHP:
<?php
    
// keep our session
    
session_start();

    
// Check the upload file type

    
$filetype $_FILES['myfile']['type'];
    
$gd_function_suffix = array(  
         
'image/pjpeg' => 'JPEG'
         
'image/jpeg' => 'JPEG'
         
'image/gif' => 'GIF'
         
'image/bmp' => 'WBMP',
        
'image/png' => 'PNG',
         
'image/x-png' => 'PNG' 
        
);
        
$function_suffix $gd_function_suffix[$filetype];
        
        
// Build Function name for ImageCreateFromSUFFIX 
        
if(!$function_suffix){
            
$err "Unsupported file type, must be JPG|PNG|GIF|BMP";
        }
        else{
        
$function_to_read 'ImageCreateFrom' $function_suffix;
        
    
// Get the image and create a thumbnail
    
$img $function_to_read($_FILES['myfile']['tmp_name']);
    if (!
$img) {
        
$err =  "could not create image handle";
    }

    
$width imageSX($img);  // Get the image width
    
$height imageSY($img); // Get the image height

    
if (!$width || !$height) {
        
$err =  "No Width or Height";
    }

    
// Build the thumbnail
    
$target_width 100;  // Thumnail width
    
$target_height 100// Thumbnail height
    
$target_ratio $target_width $target_height;  //New target ratio

    
$img_ratio $width $height;  // Original ratio

    
if ($target_ratio $img_ratio) {
        
$new_height $target_height;
        
$new_width $img_ratio $target_height// Tall Image
    
} else {
        
$new_height $target_width $img_ratio// Wide Image
        
$new_width $target_width;
    }

    if (
$new_height $target_height) {
        
$new_height $target_height;
    }
    if (
$new_width $target_width) {
        
$new_height $target_width;
    }

    
$new_img ImageCreateTrueColor($target_width$target_height); // Create a new image handle for thumbnail
    
$white imagecolorallocate($new_img255255255); // Allocate the color white
    
if (!@imagefilledrectangle($new_img00$target_width-1$target_height-1$white)) {    // Fill the image white
        
$err "Couldn't fill image";
    }
    
// Copy the uploaded image to the newly created image
    
if (!@imagecopyresampled($new_img$img, ($target_width-$new_width)/2, ($target_height-$new_height)/200$new_width$new_height$width$height)) {
        
$err "couldn't copy new image";
    }
    
    
// Create php Session array for image information
    
if (!isset($_SESSION["file_info"])) {
        
$_SESSION["file_info"] = array();
    }

    
// Use a output buffering to load the image into a variable
    
ob_start();
    
imagejpeg($new_img);
    
$imagevariable ob_get_contents();
    
ob_end_clean();

    
// Create a unique id or name for the image
    
$file_id md5($_FILES["myfile"]["tmp_name"] + rand()*100000);
    
$new_name $file_id.".$function_suffix";
    
$temp_name $_FILES["myfile"]["tmp_name"];
    
    
// assign upload directory, and target path for image
    
$uploaddir "dir/"//
    
$target_path $uploaddir.DIRECTORY_SEPARATOR.$new_name;
 
     
// Move uploaded image to uploaddir location
    
if(!@move_uploaded_file($temp_name$target_path)){
        
$err "Couldn't Copy File to Filesystem";
    }
    
// Create a Session array with name, type, and image object for use in displaying thumbnails
    
if (!isset($_SESSION["file_info"][$file_id])) {
        
$_SESSION["file_info"][$file_id] = array(
                                            
"name" => $new_name
                                            
"type" => $function_suffix
                                            
"obj" => $imagevariable
                                            
);
    }
}
   
// If no error, then pass good result and file id to Javascript for generating the thumbnail.
   // Pass our $err varialbe along

    
if(!$err){
        
$result "'1','".$file_id."'";    
    }
    else{
        
$result "'.$err.','".$file_id."'";
       }
 
   
sleep(1); // in case of really fast upload, sleep script to show loading animation for a bit longer
    
    // Javascript calls the "stopUpload" function with its paramaters
?>

<script language="javascript" type="text/javascript">window.top.window.stopUpload(<?php echo $result?>);</script>
Y el estracto dentro de la función que realiza el cambio del nombre es este:
Código PHP:
    // Create a unique id or name for the image
    
$file_id md5($_FILES["myfile"]["tmp_name"] + rand()*100000);
    
$new_name $file_id.".$function_suffix";
    
$temp_name $_FILES["myfile"]["tmp_name"];
    
    
// assign upload directory, and target path for image
    
$uploaddir "dir/"//
    
$target_path $uploaddir.DIRECTORY_SEPARATOR.$new_name
¿Me podrían decir por favor cómo hacer para que mantenga el mismo nombre con el que lo selecciono para subirlo?

Muchas gracias y saludos.

Marx.
__________________
"Todo lo que somos es el resultado de lo que hemos pensado". Budda.
  #2 (permalink)  
Antiguo 12/05/2010, 11:35
Avatar de caricatos
Moderador
 
Fecha de Ingreso: abril-2002
Ubicación: Torremolinos (Málaga)
Mensajes: 19.607
Antigüedad: 22 años
Puntos: 1284
Respuesta: upload de archivos: función que cambia el nombre del archivo a subir

Hola:

Lo de usar cosas de terceros, debería implicar revisar su documentación (supongo que la habrá...), pero sin ánimo de reprochar, hay una cocadenación que parece ser el problema...

$new_name = $file_id.".$function_suffix";

Creo que podrías probar sin el $function_suffix;

Ya nos dirás.

Saludos
__________________
Por favor:
No hagan preguntas de temas de foros en mensajes privados... no las respondo

Etiquetas: ajax, nombre, subir, upload, cambios
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:24.