Ver Mensaje Individual
  #3 (permalink)  
Antiguo 02/10/2013, 02:18
xinxan22
 
Fecha de Ingreso: septiembre-2005
Mensajes: 522
Antigüedad: 21 años
Puntos: 0
Respuesta: Renombrar upload file

Era esa mi duda. Si puedes mover el tema a PHP, la función par subir al servidor hace lo siguiente:



Código HTML:
function subirArchivoServidor(current_file,xhr,destino_php){

  xhr = new XMLHttpRequest();
  xhr.upload.addEventListener('error',function(e){
    alert('Ha habido un error cargando el archivo ');
    return false;
  }, false);

  xhr.open('POST',destino_php);

  xhr.setRequestHeader("Cache-Control", "no-cache");
  xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  xhr.setRequestHeader("X-File-Name", current_file.name);

  xhr.send(current_file);

}


y el archivo php del servidor hace esto:


Código PHP:
if (array_key_exists('HTTP_X_FILE_NAME', $_SERVER) && array_key_exists('CONTENT_LENGTH', $_SERVER)) {
    
$fileName = $_SERVER['HTTP_X_FILE_NAME'];
    
$contentLength = $_SERVER['CONTENT_LENGTH'];
} 
else throw new 
Exception("Error retrieving headers");

$path = 'uploads/';

if (!
$contentLength > 0) {
    throw new 
Exception('No file uploaded!');
}

file_put_contents(
    
$path . $fileName,
    
file_get_contents("php://input")
);

chmod($path.$fileName, 0777); 

Entiendo que haciendo $filename="pepito.jpg" se cambiará el nombre y se guardará tal cual en el servidor.

Ahora mi pregunta es:



Como le paso el nuevo nombre del archivo "pepito.jpg" a la función anterior para poderla usar en javascript ?

Gracias

Saludos