Ver Mensaje Individual
  #8 (permalink)  
Antiguo 20/01/2012, 02:01
Avatar de geeck22
geeck22
 
Fecha de Ingreso: agosto-2010
Ubicación: Tijuana B.C.
Mensajes: 79
Antigüedad: 13 años, 8 meses
Puntos: 7
Respuesta: Duda pasar variable php → js → php

script.js
Código Javascript:
Ver original
  1. $(function(){
  2.    
  3.     var dropbox = $('#dropbox'),
  4.         message = $('.message', dropbox);
  5.    
  6.     dropbox.filedrop({
  7.         // The name of the $_FILES entry:
  8.         paramname:'pic',
  9.         data: {"album": "$album", "seccion":"$seccion"},
  10.        
  11.         maxfiles: 10,
  12.         maxfilesize: 3,
  13.         url: 'post_file.php',
  14.        
  15.         uploadFinished:function(i,file,response){
  16.             $.data(file).addClass('done');
  17.             // response is the JSON object that post_file.php returns
  18.         },
  19.        
  20.         error: function(err, file) {
  21.             switch(err) {
  22.                 case 'BrowserNotSupported':
  23.                     showMessage('No se pueden subir imagenes debido a que tu navegador no es compatible con HTML5.');
  24.                     break;
  25.                 case 'TooManyFiles':
  26.                     alert('Solo puedes subir 10 imagenes simultaneamente para agilizar la carga.');
  27.                     break;
  28.                 case 'FileTooLarge':
  29.                     alert(file.name+' Esta imagen es muy pesada, se admiten 3mb como maximo.');
  30.                     break;
  31.                 default:
  32.                     break;
  33.             }
  34.         },
  35.        
  36.         // Called before each upload is started
  37.         beforeEach: function(file){
  38.             if(!file.type.match(/^image\//)){
  39.                 alert('Only images are allowed!');
  40.                
  41.                 // Returning false will cause the
  42.                 // file to be rejected
  43.                 return false;
  44.             }
  45.         },
  46.        
  47.         uploadStarted:function(i, file, len){
  48.             createImage(file);
  49.         },
  50.        
  51.         progressUpdated: function(i, file, progress) {
  52.             $.data(file).find('.progress').width(progress);
  53.         }
  54.          
  55.     });
  56.    
  57.     var template = '<div class="preview">'+
  58.                         '<span class="imageHolder">'+
  59.                             '<img />'+
  60.                             '<span class="uploaded"></span>'+
  61.                         '</span>'+
  62.                         '<div class="progressHolder">'+
  63.                             '<div class="progress"></div>'+
  64.                         '</div>'+
  65.                     '</div>';
  66.    
  67.    
  68.     function createImage(file){
  69.  
  70.         var preview = $(template),
  71.             image = $('img', preview);
  72.            
  73.         var reader = new FileReader();
  74.        
  75.         image.width = 100;
  76.         image.height = 100;
  77.        
  78.         reader.onload = function(e){
  79.            
  80.             // e.target.result holds the DataURL which
  81.             // can be used as a source of the image:
  82.            
  83.             image.attr('src',e.target.result);
  84.         };
  85.        
  86.         // Reading the file as a DataURL. When finished,
  87.         // this will trigger the onload function above:
  88.         reader.readAsDataURL(file);
  89.        
  90.         message.hide();
  91.         preview.appendTo(dropbox);
  92.        
  93.         // Associating a preview container
  94.         // with the file, using jQuery's $.data():
  95.        
  96.         $.data(file,preview);
  97.     }
  98.  
  99.     function showMessage(msg){
  100.         message.html(msg);
  101.     }
  102.  
  103. });

post_file.php
Código PHP:
$demo_mode false;
$upload_dir '../fotos/';
$allowed_ext = array('jpg','jpeg','png','gif');

if(
strtolower($_SERVER['REQUEST_METHOD']) != 'post'){
    
exit_status('Error!');
}

if(
array_key_exists('pic',$_FILES) && $_FILES['pic']['error'] == ){

    
$pic $_FILES['pic'];
    
$exp explode("."$pic['name']);
    
$ext $exp[1];
    
$cadena '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_';
$tamano 12
$nvo 'foto-';
for (
$i=0$i <= $tamano$i++){
$rand rand(0strlen($cadena));
$nvo .= $cadena[$rand];

$nom $nvo.".".$ext;

    if(!
in_array(get_extension($pic['name']),$allowed_ext)){
        
exit_status('Only '.implode(',',$allowed_ext).' files are allowed!');
    }    

    
// Move the uploaded file from the temporary
    // directory to the uploads folder:

$conexion mysql_connect("localhost","xxxx","xxxx");
if(!
$conexion) {
die (
"Error al Intentar Conectar a la Base de Datos:"mysql_error());
}
$db_conexion mysql_select_db("xxxx"$conexion);
if(!
$db_conexion) {
die (
"Error al Intentar Seleccionar la Base de Datos:"mysql_error());
}
mysql_query ("SET NAMES 'utf8'");                    
mysql_query("INSERT INTO fotos VALUES ('', '1', '','http://xxxx.com/fotos/$nom', 'http://xxxxx.com/mn.php?src=fotos/$nom&w=230&h=172&zc=3', 'http://xxxx.com/mn.php?src=fotos/$nom&w=720&h=480&zc=3', '$album', '$seccion')");
    
    
    
    if(
move_uploaded_file($pic['tmp_name'], $upload_dir.$nom)){
        
exit_status('Las imagenes se han cargado correctamente.');
    }

}

exit_status('');

// Helper functions

function exit_status($str){
    echo 
json_encode(array('status'=>$str));
    exit;
}

function 
get_extension($file_name){
    
$ext explode('.'$file_name);
    
$ext array_pop($ext);
    return 
strtolower($ext);