Ver Mensaje Individual
  #1 (permalink)  
Antiguo 10/07/2013, 13:40
montaraz_09zaek
 
Fecha de Ingreso: julio-2013
Mensajes: 3
Antigüedad: 10 años, 9 meses
Puntos: 0
Problema al subir archivo Notice: Undefined index: file

Hola, soy nuevo y recurro a vuestra ayuda como solución desesperada:

Mi problema es que quiero subir un archivo al servidor, y al hacer clic en el botón, me llega al php, pero el array FILE llega vacío.

Para empezar, en el form no puedo usar action="" porque siempre que lo hago me lleva a una página gris y se queda asi.

Por lo tanto, recurro a javaScript y utilizo ajax con la función click(función).

Os adjunto mi código:


html:

<form id="sendfile" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<div style="margin:6px 0 0 10px;">
<button id="enviararchivo" type="submit" data-theme="b" class="button">
Enviar
</button>
</div>
</form>


javascript:

var sendfile=$('#sendfile');
var enviararchivo=$('#enviararchivo');

$(enviararchivo).click(function(){

$.ajax({
type:'POST',
url: 'enviarArchivo.php',
data:$('#sendfile').serialize(),
success: function(response) {
$('#ContactForm').find('.form_result').html(respon se);


},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});


});


y php;

<?php
var_dump( $_FILES );
session_start();
$variable=$_SESSION["nick"];
echo "Profile picture of $variable ";

if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}


?>



Por favor, estoy desesperado, ya no sé que más hacer.

Muchas gracias de antemano por vuestra ayuda.


P.D: Disculpad la forma de escribir el código, pero no se hacerlo de otro modo