Problem with upload file to folder.

Discussion in 'PHP' started by JoseA, Feb 15, 2007.

  1. #1
    :confused:
    Hi!
    At first, thanks all to read this message.
    I've this problem, this script upload till 4 files. I have used a common form with only one file and the action take me to this file, named: "upload_file.php". All right with this but now I want put the files uploaded in a specific folder, for example: "photos". In fact I´ve 2 folders: backend (where i keep the files that administrate the site, upload_file.php is in this folder) and photos (folder where I want save my uploaded files. I don´t know how modify this script to make that. Can you help me please?. This are my first steps in php.
    Thanks!.
    JoseA.

    <?php
    error_reporting(E_ALL);
    @ini_set('display_errors', '1');
    /*----------- start configuration-----------------*/
    //definir el nombre o la ip del servidor de db
    $server='localhost';
    //definir el nombre de usuario de la db
    $user='';
    //definir la contraseña
    $pass='';
    //definir el nombre de la db
    $db='';
    //en la siguiente línea, definir el nombre de la tabla de la db (escribirlo entre las comillas simples).
    $tabla='';
    //en esta otra línea colocar el nombre del archivo a mostrar al finalizar el proceso (escribirlo entre las comillas simples)
    $destino='';
    
    
    /*----------- end of configuration-----------------*/
    mysql_connect($server,$user,$pass);
    mysql_select_db($db);
    function insertar_form($tabla){
    foreach($_POST as $k => $v){
    if($k!='imageField_x' && $k!='imageField_y' && $k!='foto' && $k!='foto1' && $k!='foto2' && $k!='foto3' && $k!='Submit'){
    $listacampos[]=$k;
    $listavalores[]=(get_magic_quotes_gpc()) ? $v : addslashes($v);
    }}
    $formatocampos=implode(',',$listacampos);
    $formatovalores="'".implode("','",$listavalores);
    $formatovalores.="'";
    mysql_query("insert into $tabla ($formatocampos) values ($formatovalores)");
    }
    function subirarchivo($archivo,$archivotemp,$tabla,$campoarchivo,$error){
    $qry=mysql_query("select max(id) as ultimo from $tabla");
    $row=mysql_fetch_assoc($qry);
    $id=$row['ultimo'];
    if($archivo!=''){
    $extension200=end(explode(".",strtolower($archivo)));
    if($extension200!='jpg' && $extension200!='gif' && $extension200!='png' && $extension200!='doc' && $extension200!='zip' && $extension200!='pdf' && $extension200!='xls' && $extension200!='ppt' && $extension200!='swf'){
    eval($error);exit;}
    $foto2=md5(time()).$archivo;
    if(!copy($archivotemp,$foto2)){die('No pudo cargarse la foto');};
    @chmod($foto2,0777);
    mysql_query("update $tabla set $campoarchivo='$foto2' where id='$id'");
    }
    }
    insertar_form($tabla);
    if(isset($_FILES['foto']['name'])){
    subirarchivo($_FILES['foto']['name'],$_FILES['foto']['tmp_name'],$tabla,'foto','');
    }
    for($i=1;$i<4;$i++){
    if(isset($_FILES['foto'.$i]['name'])){
    subirarchivo($_FILES['foto'.$i]['name'],$_FILES['foto'.$i]['tmp_name'],$tabla,'foto'.$i,'');}
    }
    header("Location:$destino");
    ?>
    PHP:
    Thanks.
    JoseA. :)
     
    JoseA, Feb 15, 2007 IP
  2. techie007

    techie007 Peon

    Messages:
    261
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here is a simple code to upload files/images to a folder. You need to make sure that the folder, in which the images/files will be uploaded, should have write permissions.

    // The upload directory, where you want your files to be uploaded
    $upload_dir = "uploads";
    
    // If you want to set restriction on certain extensions. (extension check is optional)
    $extensions_allowed = "jpg, gif, png, pdf";
    $extension = pathinfo($_FILES['file']['name']);
    $extension = $extension[extension];
    $allowed_paths = explode(", ", $extensions_allowed);
    for($i = 0; $i < count($allowed_paths); $i++) 
    	{
           		if ($allowed_paths[$i] == "$extension") 
    		{
            			$ok = "1";
            	     	}
           	}
    // now the code that uploads your file
    if(is_uploaded_file($_FILES['file']['tmp_name']))
    	{
    		move_uploaded_file($_FILES['file']['tmp_name'],$upload_dir.'/'.$_FILES['file']['name']);
    	}
    	print "Your file has been uploaded uooohu";
    	}
    else
    	{
    	print "Oops, the file upload failed, check the extension!";
    	}
    PHP:
    I hope this helps :)
     
    techie007, Feb 16, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    nico_swd, Feb 16, 2007 IP
  4. JoseA

    JoseA Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks techie007 and nico_swd,
    I´ve read the code that you send me and I´ve understand this (remember this is my first step in PHP). In fact I would like modify the original code, perhaps beacause I´ve spent a lot of time on this. I know that read the comments in original code is difficult beacause these are in spanish, so I'll do this:
    1) Prove again with the original code.
    2) Make a new code based on your code.

    Then, I'll tell you how was that!!! But, if you want help me with this, NO PROBLEM!!!
    Thanks again!

    JoseA.
     
    JoseA, Feb 16, 2007 IP