Comunidad de diseño web y desarrollo en internet online

Subir imágenes con Flash 8

Citar            
MensajeEscrito el 11 Dic 2005 05:34 pm
Gracias al tutorial de cristalab he logrado subir imágenes a una carpeta de mi servidor. Todo funciona correctamente, pero lo que quiero hacer es cambiar el nombre del archivo por el nombre de una variable que envío mediante _POST.
El código PHP es el siguiente:

Código :


<?php

if(!is_dir("./fotografias")) mkdir("./fotografias", 0755); 
move_uploaded_file($_FILES['Filedata']['tmp_name'], "./fotografias/".$_FILES['Filedata']['name']);
?>



Entonces yo paso la variable desde AS a PHP y el código php quedaría asi:

Código :


<?php
if(!is_dir("./fotografias")) mkdir("./fotografias", 0755); 
$foto = $_POST['nick'];
move_uploaded_file($_FILES['Filedata']['tmp_name'], "../fotografias/".$foto);
?>



Algo debe fallar en este código, si alguien lo sabe .... MIL GRACIAS

Por bluenet

100 de clabLevel



Genero:Masculino  

Sevilla

msie
Citar            
MensajeEscrito el 11 Dic 2005 05:52 pm
Puedes enviar el parámetro por GET cuando hagas la llamada al método upload();

Código :

fileReference.upload("http://tudominio.com/upload.php?foto=foto1");


Y así seguro que te funciona

Por Cyril

45 de clabLevel



 

firefox
Citar            
MensajeEscrito el 11 Dic 2005 06:06 pm
Cyril me puedes extender un poco más la respuesta, aquí te expongo el código AS al completo:

Código :


var t:SharedObject = SharedObject.getLocal("user", "/");
function not_set_yet() {
   t.flush();
}
if (t.data.id) {
   nick = t.data.id;
}
volar = new LoadVars();
volar.nick = t.data.id;
volar.sendAndLoad("upload.php", volar, "POST");
System.security.allowDomain("http://www.midominio.com");
import flash.net.FileReference;
// The listener object listens for FileReference events.
var listener:Object = new Object();
// When the user selects a file, the onSelect() method is called, and
// passed a reference to the FileReference object.
listener.onSelect = function(selectedFile:FileReference):Void  {
   //clean statusArea and details area
   statusArea.text = details.text="";
   // Flash is attempting to upload the image.
   statusArea.text += "Attempting to upload "+t.data.id+"\n";
   // Upload the file to the PHP script on the server.
   selectedFile.upload("upload.php");
};
// the file is starting to upload.
listener.onOpen = function(selectedFile:FileReference):Void  {
   statusArea.text += "Uploading "+t.data.id+"\n";
};
//Possible file upload errors
listener.onHTTPError = function(file:FileReference, httpError:Number):Void  {
   imagePane.contentPath = "error";
   imagePane.content.errorMSG.text = "HTTPError number: "+httpError+"\nFile: "+t.data.id;
};
listener.onIOError = function(file:FileReference):Void  {
   imagePane.contentPath = "error";
   imagePane.content.errorMSG.text = "IOError: "+t.data.id;
};
listener.onSecurityError = function(file:FileReference, errorString:String):Void  {
   imagePane.contentPath = "error";
   imagePane.content.errorMSG.text = "SecurityError: "+SecurityError+"\nFile: "+t.data.id;
};
// the file has uploaded
listener.onComplete = function(selectedFile:FileReference):Void  {
   // Notify the user that Flash is starting to download the image.
   statusArea.text += "Upload finished.\nNow downloading "+t.data.id+" to player\n";
   //Show file details
   details.text = "";
   for (i in selectedFile) {
      details.text += "<b>"+i+":</b> "+selectedFile[i]+"\n";
   }
   // Call the custom downloadImage() function.
   downloadImage(t.data.id);
};
var imageFile:FileReference = new FileReference();
imageFile.addListener(listener);
uploadBtn.onPress = uploadImage;
imagePane.addEventListener("complete", imageDownloaded);
// Call the uploadImage() function, opens a file browser dialog.
function uploadImage(event:Object):Void {
   imageFile.browse([{description:"Image Files", extension:"*.jpg;*.gif;*.png"}]);
}
// If the image does not download, the event object's total property
// will equal -1. In that case, display am error message
function imageDownloaded(event:Object):Void {
   if (event.total == -1) {
      imagePane.contentPath = "error";
   }
}
// show uploaded image in scrollPane
function downloadImage(file:Object):Void {
   imagePane.contentPath = "./fotografias/"+file;
}
stop();



Gracias por tu atención!

Por bluenet

100 de clabLevel



Genero:Masculino  

Sevilla

msie
Citar            
MensajeEscrito el 11 Dic 2005 06:37 pm
Creo que debería ir aquí:

Código :

   // Upload the file to the PHP script on the server.
   selectedFile.upload("upload.php");


Para recoger tu variable, debería ser:

Código :

   // Upload the file to the PHP script on the server.
   selectedFile.upload("upload.php?nick=nombreFoto");


Y el php:

Código :

<?php
if(!is_dir("./fotografias")) mkdir("./fotografias", 0755);
$foto = $_GET['nick'];
move_uploaded_file($_FILES['Filedata']['tmp_name'], "../fotografias/".$foto);
?> 


Salu2

Por Cyril

45 de clabLevel



 

firefox
Citar            
MensajeEscrito el 11 Dic 2005 06:45 pm
¿Cyril como le paso la variable? nombreFoto :(

Por bluenet

100 de clabLevel



Genero:Masculino  

Sevilla

msie
Citar            
MensajeEscrito el 11 Dic 2005 07:00 pm
Si es siempre el mismo nombre, simplemente cambia nombreFoto por lo que tú quieras.

Si tiene que cambiar, tendrás que crear una variable nombreFoto donde almacenes el nombre que quieras que tenga el archivo a subir, y la llamada sería:

Código :

   // Upload the file to the PHP script on the server.
   selectedFile.upload("upload.php?nick="+_root.nombreFoto);

Por Cyril

45 de clabLevel



 

firefox
Citar            
MensajeEscrito el 11 Dic 2005 07:12 pm
Perfecto, este paso lo entiendo, pero crees que el codigo php, ¿es correcto?

Según entendí qudaría asi:

PHP:

Código :


<?php 
if(!is_dir("./fotografias")) mkdir("./fotografias", 0755); 
$foto = $_POST['nick']; 
move_uploaded_file($_FILES['Filedata']['tmp_name'], "../fotografias/".$foto); 
?> 



AS:

Código :


var t:SharedObject = SharedObject.getLocal("user", "/");
function not_set_yet() {
   t.flush();
}
if (t.data.id) {
   nick = t.data.id;
                nombreFoto = nick;
}
volar = new LoadVars();
volar.nick = t.data.id;
volar.sendAndLoad("upload.php", volar, "POST");
System.security.allowDomain("http://www.ciberbluenet.com/ciber/");
import flash.net.FileReference;
// The listener object listens for FileReference events.
var listener:Object = new Object();
// When the user selects a file, the onSelect() method is called, and
// passed a reference to the FileReference object.
listener.onSelect = function(selectedFile:FileReference):Void  {
   //clean statusArea and details area
   statusArea.text = details.text="";
   // Flash is attempting to upload the image.
   statusArea.text += "Attempting to upload "+t.data.id+"\n";
   // Upload the file to the PHP script on the server.
   selectedFile.upload("upload.php?nick="+_root.nombreFoto);
};
// the file is starting to upload.
listener.onOpen = function(selectedFile:FileReference):Void  {
   statusArea.text += "Uploading "+t.data.id+"\n";
};
//Possible file upload errors
listener.onHTTPError = function(file:FileReference, httpError:Number):Void  {
   imagePane.contentPath = "error";
   imagePane.content.errorMSG.text = "HTTPError number: "+httpError+"\nFile: "+t.data.id;
};
listener.onIOError = function(file:FileReference):Void  {
   imagePane.contentPath = "error";
   imagePane.content.errorMSG.text = "IOError: "+t.data.id;
};
listener.onSecurityError = function(file:FileReference, errorString:String):Void  {
   imagePane.contentPath = "error";
   imagePane.content.errorMSG.text = "SecurityError: "+SecurityError+"\nFile: "+t.data.id;
};
// the file has uploaded
listener.onComplete = function(selectedFile:FileReference):Void  {
   // Notify the user that Flash is starting to download the image.
   statusArea.text += "Upload finished.\nNow downloading "+t.data.id+" to player\n";
   //Show file details
   details.text = "";
   for (i in selectedFile) {
      details.text += "<b>"+i+":</b> "+selectedFile[i]+"\n";
   }
   // Call the custom downloadImage() function.
   downloadImage(t.data.id);
};
var imageFile:FileReference = new FileReference();
imageFile.addListener(listener);
uploadBtn.onPress = uploadImage;
imagePane.addEventListener("complete", imageDownloaded);
// Call the uploadImage() function, opens a file browser dialog.
function uploadImage(event:Object):Void {
   imageFile.browse([{description:"Image Files", extension:"*.jpg;*.gif;*.png"}]);
}
// If the image does not download, the event object's total property
// will equal -1. In that case, display am error message
function imageDownloaded(event:Object):Void {
   if (event.total == -1) {
      imagePane.contentPath = "error";
   }
}
// show uploaded image in scrollPane
function downloadImage(file:Object):Void {
   imagePane.contentPath = "./fotografias/"+file;
}
stop();



Perdona tantas molestias, pero me va a estallar la cabeza y cada vez me encuentro mas perdido :crap:

Por bluenet

100 de clabLevel



Genero:Masculino  

Sevilla

msie
Citar            
MensajeEscrito el 11 Dic 2005 07:53 pm
Vale, a ver, que no me había fijado en algunas cosas.

Por lo visto, en ese código llamas dos veces al script php, lo cual creo que no es necesario al pasar el parámetro por get. Por lo que veo, tampoco sería necesaria la variable nombreFoto.

Debería quedar algo así:



Código :

var t:SharedObject = SharedObject.getLocal("user", "/");
function not_set_yet() {
   t.flush();
}
if (t.data.id) {
   nick = t.data.id;
}
System.security.allowDomain("http://www.ciberbluenet.com/ciber/");
import flash.net.FileReference;
// The listener object listens for FileReference events.
var listener:Object = new Object();
// When the user selects a file, the onSelect() method is called, and
// passed a reference to the FileReference object.
listener.onSelect = function(selectedFile:FileReference):Void  {
   //clean statusArea and details area
   statusArea.text = details.text="";
   // Flash is attempting to upload the image.
   statusArea.text += "Attempting to upload "+t.data.id+"\n";
   // Upload the file to the PHP script on the server.
   selectedFile.upload("upload.php?nick="+t.data.id);
};
// the file is starting to upload.
listener.onOpen = function(selectedFile:FileReference):Void  {
   statusArea.text += "Uploading "+t.data.id+"\n";
};
//Possible file upload errors
listener.onHTTPError = function(file:FileReference, httpError:Number):Void  {
   imagePane.contentPath = "error";
   imagePane.content.errorMSG.text = "HTTPError number: "+httpError+"\nFile: "+t.data.id;
};
listener.onIOError = function(file:FileReference):Void  {
   imagePane.contentPath = "error";
   imagePane.content.errorMSG.text = "IOError: "+t.data.id;
};
listener.onSecurityError = function(file:FileReference, errorString:String):Void  {
   imagePane.contentPath = "error";
   imagePane.content.errorMSG.text = "SecurityError: "+SecurityError+"\nFile: "+t.data.id;
};
// the file has uploaded
listener.onComplete = function(selectedFile:FileReference):Void  {
   // Notify the user that Flash is starting to download the image.
   statusArea.text += "Upload finished.\nNow downloading "+t.data.id+" to player\n";
   //Show file details
   details.text = "";
   for (i in selectedFile) {
      details.text += "<b>"+i+":</b> "+selectedFile[i]+"\n";
   }
   // Call the custom downloadImage() function.
   downloadImage(t.data.id);
};
var imageFile:FileReference = new FileReference();
imageFile.addListener(listener);
uploadBtn.onPress = uploadImage;
imagePane.addEventListener("complete", imageDownloaded);
// Call the uploadImage() function, opens a file browser dialog.
function uploadImage(event:Object):Void {
   imageFile.browse([{description:"Image Files", extension:"*.jpg;*.gif;*.png"}]);
}
// If the image does not download, the event object's total property
// will equal -1. In that case, display am error message
function imageDownloaded(event:Object):Void {
   if (event.total == -1) {
      imagePane.contentPath = "error";
   }
}
// show uploaded image in scrollPane
function downloadImage(file:Object):Void {
   imagePane.contentPath = "./fotografias/"+file;
}
stop();


Y el php como está.

Creo que debería funcionar, pero no veo qué hace exactamente el sharedObjects y qué hacía el loadVars, así que igual sólo estamos dando palos de ciego.

Si no te funciona, habrá que esperar el consejo de alguien más sabio ;)

Salu2

Por Cyril

45 de clabLevel



 

firefox
Citar            
MensajeEscrito el 11 Dic 2005 08:20 pm
Muchas gracias Cyril por preocuparte tanto, la imagen sube correctamente al server, pero sigue con el nombre original :( No logro cambiarlo.

Gracias!

Para que reciba el archivo correctamente he tenido que modificar el PHP asi:

Código :


<?php 
if(!is_dir("./fotografias")) mkdir("./fotografias", 0755); 
move_uploaded_file($_FILES['Filedata']['tmp_name'], "./fotografias/".$_FILES['Filedata']['name']); 
?> 

Por bluenet

100 de clabLevel



Genero:Masculino  

Sevilla

msie
Citar            
MensajeEscrito el 11 Dic 2005 08:31 pm
Claro, esa linea del php, al final $_FILES['Filedata']['name'] es el nombre original, por eso se queda como está.

Prueba a poner ahí $_GET['nick'] en vez de eso.

Por Cyril

45 de clabLevel



 

firefox
Citar            
MensajeEscrito el 11 Dic 2005 08:59 pm
Te entiendo perfectamente, yo habia pensado lo mismo, pero al quitar esto $_FILES['Filedata']['name'] o sustituirlo por GET no recibe nada :(

Por bluenet

100 de clabLevel



Genero:Masculino  

Sevilla

msie
Citar            
MensajeEscrito el 11 Dic 2005 10:15 pm
¿Has comprobado que estés enviando bien la variable desde flash?

Código :

listener.onSelect = function(selectedFile:FileReference):Void  {
   //clean statusArea and details area
   statusArea.text = details.text="";
   // Flash is attempting to upload the image.
   statusArea.text += "Attempting to upload "+t.data.id+"\n";
   // Upload the file to the PHP script on the server.
   selectedFile.upload("upload.php?nick="+t.data.id);
}; 


Pon aquí unos cuantos trace, para ver si llega "t.data.id", que puede que no alcanze y tengas que poner la ruta absoluta _root.t.data.id.

Mira a ver si así aparece el nombre de lo que estás enviando, o sale undefined o algo así...

No se me ocurre nada más :oops:

Salu2

Por Cyril

45 de clabLevel



 

firefox
Citar            
MensajeEscrito el 15 Dic 2005 06:10 pm
Gracias Cyril, FUNCIONO


MIL GRACIAS!!!!!!!! :) :lol: :wink: ^^ :D

[SWAT] Por favor no vuelvas a escribir de esa forma, descuadras el foro [/SWAT]

Por bluenet

100 de clabLevel



Genero:Masculino  

Sevilla

msie
Citar            
MensajeEscrito el 15 Dic 2005 08:27 pm
De nada ;)

Por Cyril

45 de clabLevel



 

firefox
Citar            
MensajeEscrito el 09 Feb 2008 04:09 pm
FUNCIONA PERFECTO!

*******PERO ahora insistiendo en la PRIMERSA OPCION de ***bluenet*** como seria si quisieramos que fuera mediante POST. y no GET..

ya que veo que en transcurso de las respuestas... SE OBLIGO AL EL SCRIPT a que se utilizara FORZADAMENTE "METODO GET" en ves del METODO POST que era el problema original.-

Saludos.-

Por xyyy7

16 de clabLevel



 

Béisbol, Petróleo y Mujeres Bellas.- Venezuela (Maracay)

firefox

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.