Ver Mensaje Individual
  #1 (permalink)  
Antiguo 22/02/2008, 05:43
printonic
 
Fecha de Ingreso: diciembre-2007
Mensajes: 38
Antigüedad: 16 años, 4 meses
Puntos: 0
Guardar imagenes en distintas carpetas dentro de una funcion

Tengo varias condiciones if y dentro de ellas tengo una variable que llama a una funcion llamada UploadFile. El problema esta que en la funcion que pongo mas abajo me guarda las imagenes todas en una carpeta y yo quiero que las guarde en distintas carpetas , por ejemplo wallpaper_1 que lo guarde en la carpeta 800x600.

He pensado en crear UploadFile1, UploadFile2 etc..y asi las guarda donde quiero, pero lo veo un poco chapucero y demasiado codigo repetido, no?

Código PHP:
if ($_FILES['wallpaper1']['name']!=NULL) {
        
$Wallpaper_1=UploadFile("wallpaper1");
    }
    if (
$_FILES['wallpaper2']['name']!=NULL) {
        
$Wallpaper_2=UploadFile("wallpaper2");
    }
    if (
$_FILES['wallpaper3']['name']!=NULL) {
        
$Wallpaper_3=UploadFile("wallpaper3");
    }
    if (
$_FILES['wallpaper4']['name']!=NULL) {
        
$Wallpaper_4=UploadFile("wallpaper4");
    }
    if (
$_FILES['wallpaper5']['name']!=NULL) {
        
$Wallpaper_5=UploadFile("wallpaper5"); 
Y la funcion:

Código PHP:
function UploadFile($uname)
{
        global 
$_FILES;
        global 
$path;
        
$type substr($_FILES[$uname]['type'],0,6);
        if(!isset(
$_FILES)){
            
$error "You must select a valid file.";
        } else if(
$_FILES[$uname]['error']!=0){
            
$error "An unexpected error occurred.";
        } else if(
$type != 'image/')
        {
            
$error "The file you have selected is not a valid image file.";
        }
        if (! 
$error)
        {
            
$_IMAGE = array();
            
$_IMAGE['name'] = $_FILES[$uname]['name'];
            
$_IMAGE['size'] = $_FILES[$uname]['size'];
            
$_IMAGE['date'] = date("D d F Y");
            
$_IMAGE['type'] = substr($_FILES[$uname]['type'],6);
            
$_IMAGE['code'] = microtime();
            
$_IMAGE['name'] = htmlentities($_IMAGE['name']);
            
$_IMAGE['size'] = htmlentities($_IMAGE['size']);
            
$_IMAGE['date'] = htmlentities($_IMAGE['date']);
            
$_IMAGE['type'] = htmlentities($_IMAGE['type']);
            
$_IMAGE['code'] = htmlentities($_IMAGE['code']);
            
$type htmlentities($type);
            
$type strtolower($_IMAGE['type']);
            if (
strtolower($type) == "pjpeg") { $type "jpeg"; }
            if (
strtolower($type) == "x-png") { $type "png"; }
            if(
checktype($type) == false)
            {
                
$error "That file type is not allowed.";
            } else if(
$_IMAGE['size']>5024000){
                
$error "That image is too large.";
            }
            if (! 
$error)
            {
                
$target_path $path."imagenes/wallpapers/";
                
$_IMAGE['name'] = $_FILES[$uname]['name'];
                
$rnd rand(0,1000000);
                
$name substr($_IMAGE['name'],0,strpos($_IMAGE['name'], ".")).'-'.$rnd;
                
$name .= ".".$type;
                
$target_path $target_path basename($name);

                if(
move_uploaded_file($_FILES[$uname]['tmp_name'], $target_path))
                {
                    return 
$name;
                } else
                {
                    echo 
$_FILES[$uname]['tmp_name']."<br />".$target_path."<br />t";
                    return 
false;
                }
            }
        } else
        {
            echo 
$error;
            return 
false;
    }

Esta es la variable dentro de la funcion que hace que me guarde todo en la misma carpeta :
Código PHP:
$target_path $path."imagenes/wallpapers/"