
Input type="file" con arreglo (upload)
Publicado por Ariel Olea (2 intervenciones) el 17/06/2009 18:55:48
Saludos a todos.
Aquí les pongo un código para poder subir (upload) más de un archivo mediante un input type="file".
Esto lo hice porque necesitaba subir varios upload usando jquery.
El jquery me permite subir más de un archivo, pero llegaba hasta el momento de subir los archivos a una carpeta del servidor.
Espero que les sirva este código, de algo puede ser útil.
Saludos
index.php
========
<form name="frm_visita_ing" method="post" action="graba.php" enctype="multipart/form-data">
<input name="archivo[]" id="archivo" type="file" />
<input name="archivo[]" id="archivo" type="file" />
<input name="archivo[]" id="archivo" type="file" />
<input type="submit" name="Grabar" value="Grabar" />
</form>
graba.php
========
<?
header("Cache-Control: no-store, no-cache, must-revalidate");
$path="/var/www/html/pagina/";
$i=0;
$archivos=array();
foreach($_FILES['archivo']['name'] as $id=>$file_upload){
$i++;
$realname = $_FILES['archivo']['name'][ $id ];//saber el nombre del archivo
$pos = strrpos($realname, ".");
$ext=substr($realname,$pos+1,5);
$file=$i.".".$ext;
$archivos[]=$file;
if (!file_exists($path."doctos/"))
mkdir ($path."doctos", 0770) ;
$file="$path"."doctos/$file";
move_uploaded_file($_FILES['archivo']['tmp_name'][ $id ], $file);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Archivos subidos</title>
</head>
<body>
<?
foreach($archivos as $id=>$nombre){
?>
<a href="<? echo "doctos/".$nombre; ?>"><? echo $nombre; ?></a><br />
<?
}
?>
</body>
</html>
Aquí les pongo un código para poder subir (upload) más de un archivo mediante un input type="file".
Esto lo hice porque necesitaba subir varios upload usando jquery.
El jquery me permite subir más de un archivo, pero llegaba hasta el momento de subir los archivos a una carpeta del servidor.
Espero que les sirva este código, de algo puede ser útil.
Saludos
index.php
========
<form name="frm_visita_ing" method="post" action="graba.php" enctype="multipart/form-data">
<input name="archivo[]" id="archivo" type="file" />
<input name="archivo[]" id="archivo" type="file" />
<input name="archivo[]" id="archivo" type="file" />
<input type="submit" name="Grabar" value="Grabar" />
</form>
graba.php
========
<?
header("Cache-Control: no-store, no-cache, must-revalidate");
$path="/var/www/html/pagina/";
$i=0;
$archivos=array();
foreach($_FILES['archivo']['name'] as $id=>$file_upload){
$i++;
$realname = $_FILES['archivo']['name'][ $id ];//saber el nombre del archivo
$pos = strrpos($realname, ".");
$ext=substr($realname,$pos+1,5);
$file=$i.".".$ext;
$archivos[]=$file;
if (!file_exists($path."doctos/"))
mkdir ($path."doctos", 0770) ;
$file="$path"."doctos/$file";
move_uploaded_file($_FILES['archivo']['tmp_name'][ $id ], $file);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Archivos subidos</title>
</head>
<body>
<?
foreach($archivos as $id=>$nombre){
?>
<a href="<? echo "doctos/".$nombre; ?>"><? echo $nombre; ?></a><br />
<?
}
?>
</body>
</html>
Valora esta pregunta


0