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_img, 255, 255, 255); // Allocate the color white
if (!@imagefilledrectangle($new_img, 0, 0, $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)/2, 0, 0, $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.