Welcome Guest, Not a member yet? Register   Sign In
Upload and replace file or image
#1
Photo 

Hello community, I need your help. !!
Work on this code:
Code:
  public function upload_files($names, $id_usuario){
       //Sube archivos
       $documentos = new Current_Documentos_Model();
      
       foreach ($names as $archivo => $info) {
            if($info['dato'] && $info['valid'] == "is-valid"){  //Valida si existe archvio y es correcto
               
                $file = $this->request->getFile($info['archivo']);
                $newName = substr($info['archivo'], 4).'_'.$id_usuario.'.'.$file->getExtension(); //Nombre archivo
                $ruta = './imagen/'.$id_usuario; //Ruta archivo
                $file->move($ruta,$newName);

                if ($file->hasMoved()){
                    $tbl_document_exists = $documentos->where('alumno_id', $id_usuario)->first();
                         $documentos->update($tbl_document_exists['ID'], [strtoupper(substr($info['archivo'], 4)) => 1]); //actualiza BD
                    }
            }
       }
    }



I define the name of the file with the document name followed by the user id.
When uploading a file with the same name I want it to be replaced, but what it does is create a new file with the following user id, as shown in the following image.


[Image: ejemplo-codeigniter.png]
Reply
#2

You can pass the third argument as true in the move() method to overwrite the file.
Reply
#3

(03-04-2021, 01:11 AM)iRedds Wrote: You can pass the third argument as true in the move() method to overwrite the file.

thank you very much, it worked ... !!!

Code:
$file = $this->request->getFile($info['archivo']);
   $newName = substr($info['archivo'], 4).'_'.$id_usuario.'.'.$file->getExtension(); //Nombre archivo
   $ruta = './imagen/'.$id_usuario; //Ruta archivo
   $file->move($ruta,$newName,true);
Reply
#4

Use an IDE or editor that can show method parameters.
This will greatly simplify development.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB