En este ejemplo crearemos un formulario dinámico con HTML y Javascript y el sistema en PHP que se encargará de subir los archivos.
Fuente: https://www.yalpublicidad.com/blog/subir-multiples-archivos-o-imagenes-con-php
YAL PUBLICIDAD
Yuri Lizama A.
www.yalpublicidad.com
HTML
<!doctype html> <html> <head> <meta charset="utf-8"> <title>Subir múltiples archivos o imágenes con PHP</title> </head> <body> <form enctype="multipart/form-data" method="post"> <button type="button" onclick="add_file_img()">+ Agregar imagen</button> <div id="div1">Aquí de agregarán los input para examinar imágenes.</div> </form> </body> </html>
JAVASCRIPT
function add_file_img(){
 $contenedor = document.getElementById('div1');
 var $imagen_nueva = document.createElement('input');
  $imagen_nueva.setAttribute( "type", "file" );
  $imagen_nueva.setAttribute( "name", "imagenes[]" );
  $contenedor.appendChild( $imagen_nueva );
}
PHP
$path = '/home/mi_hosting/public_html/images/';
foreach ($_FILES["imagenes"]["error"] as $c => $error) {
 if ($error == UPLOAD_ERR_OK) {
  $tmp = $_FILES["imagenes"]["tmp_name"][$c];
  // Recibo nombre del fichero
  $name = basename($_FILES["imagenes"]['name']);
  // Limpio el nombre algún script malicioso
  $name = htmlspecialchars($name);
  // Limpio de espacios al inicio y final del nombre
  $name = trim($name);
  // Reemplazo todos los espacios por _ para evitar error de lectura al mostrar la imagen
  $name=str_replace(" ","_",$name);
  //copio los archivos a la carpeta de destino;
  if( copy( $tmp, $path . $name ) ){
   echo '- El archivo ' . $name . ' se cargó correctamente<br>';
  }
 }else{ echo 'ERROR AL SUBIR LOS ARCHIVOS'; }
}
Fuente: https://www.yalpublicidad.com/blog/subir-multiples-archivos-o-imagenes-con-php
YAL PUBLICIDAD
Yuri Lizama A.
www.yalpublicidad.com
Comentarios
Publicar un comentario