Ver Mensaje Individual
  #5 (permalink)  
Antiguo 29/05/2010, 21:04
Avatar de egepe
egepe
 
Fecha de Ingreso: diciembre-2009
Mensajes: 310
Antigüedad: 14 años, 4 meses
Puntos: 7
Respuesta: Problemas con con upload de fotos

Yo haría un jueguito con DOM+HTML y PHP así

archivo.js

function arch_down(obj) {
Nfile++;
fi = document.getElementById('arc');
contarchs = document.createElement('div');
contarchs.id = 'div'+Nfile;
fi.appendChild(contarchs);
//alert(fi.appendChild(contarchs));
//
arch = document.createElement('input');
arch.type = 'file';
//ape1.name = 'text'+Nfile;
arch.setAttribute("name", "arch" + Nfile);
arch.setAttribute("size", "100");
arch.setAttribute("class", "box");
arch.setAttribute("maxlength", "200");
contarchs.appendChild(arch);
//
N_arch = document.createElement('input');
N_arch.type = 'hidden';
//ape1.name = 'text'+Ninvol;
N_arch.setAttribute("name", "N_arch");
N_arch.setAttribute("value", N_arch);
N_arch.setAttribute("size", "3");
N_arch.setAttribute("maxlength", "3");
contarchs.appendChild(N_arch);
//
L_arch = document.createElement('input');
L_arch.type = 'hidden';
//ape1.name = 'text'+Ninvol;
L_arch.setAttribute("name", "L_arch");
L_arch.setAttribute("value", true);
L_arch.setAttribute("size", "3");
L_arch.setAttribute("maxlength", "3");
contarchs.appendChild(L_arch);

en tu formulario agregas esta línea:
<fieldset id="dow"><input type="button" value="Agregar archivos" onclick="arch_down(this)"></fieldset>

OJO RECUERDA CAMBIAR TU FORM A: enctype="multipart/form-data"

y en el php que procesara esto:

if ($_POST['L_arch'])
{
$N = 1;
$arch ="arch".$N;
while (isset($_FILES["$arch"]['name'])){

$fileName = $_FILES["$arch"]['name'];
$tmpName = $_FILES["$arch"]['tmp_name'];
$fileSize = $_FILES["$arch"]['size'];
$fileType = $_FILES["$arch"]['type'];

//echo "<br><br><br>arch=".$arch."fileName =".$fileName." tmpName =".$tmpName." filisize =".$fileSize." fileType =".$fileType;
$fp = fopen($tmpName, 'r');
$content = fread($fp, $fileSize);
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}

if ($fileSize< 1024){
$numero=number_format($fileSize, 0, ',', '.');
$tipo_unidad="B";
}
if ($fileSize< 1048576){
$numero=number_format($fileSize/1024, 0, ',', '.');
$tipo_unidad="KB";
}
if ($fileSize>= 1048576){
$numero=number_format($fileSize/1048576, 0, ',', '.');
$tipo_unidad="MB";
}

$ins6 = "INSERT INTO inf_file (.... ) VALUES ('...... )";

$result = mysql_query($ins6)or die('Error, fallo el insert de inf_file'.mysql_error());

Te cuento que esto sirve para subir cualquier tipo de archivo y lo gracioso es que el usuario tiene la posibilidad de decidir cuando quiere subir, puede ser uno... 2 o n... Luego si los quieres ver es otra cosa.

Si te sirve esto no sé es trata de refinarla porque cogí un código viejito que se puede mejorar.

Espero que te sirva saludos...