script.js
Código Javascript
:
Ver original$(function(){
var dropbox = $('#dropbox'),
message = $('.message', dropbox);
dropbox.filedrop({
// The name of the $_FILES entry:
paramname:'pic',
data: {"album": "$album", "seccion":"$seccion"},
maxfiles: 10,
maxfilesize: 3,
url: 'post_file.php',
uploadFinished:function(i,file,response){
$.data(file).addClass('done');
// response is the JSON object that post_file.php returns
},
error: function(err, file) {
switch(err) {
case 'BrowserNotSupported':
showMessage('No se pueden subir imagenes debido a que tu navegador no es compatible con HTML5.');
break;
case 'TooManyFiles':
alert('Solo puedes subir 10 imagenes simultaneamente para agilizar la carga.');
break;
case 'FileTooLarge':
alert(file.name+' Esta imagen es muy pesada, se admiten 3mb como maximo.');
break;
default:
break;
}
},
// Called before each upload is started
beforeEach: function(file){
if(!file.type.match(/^image\//)){
alert('Only images are allowed!');
// Returning false will cause the
// file to be rejected
return false;
}
},
uploadStarted:function(i, file, len){
createImage(file);
},
progressUpdated: function(i, file, progress) {
$.data(file).find('.progress').width(progress);
}
});
var template = '<div class="preview">'+
'<span class="imageHolder">'+
'<img />'+
'<span class="uploaded"></span>'+
'</span>'+
'<div class="progressHolder">'+
'<div class="progress"></div>'+
'</div>'+
'</div>';
function createImage(file){
var preview = $(template),
image = $('img', preview);
var reader = new FileReader();
image.width = 100;
image.height = 100;
reader.onload = function(e){
// e.target.result holds the DataURL which
// can be used as a source of the image:
image.attr('src',e.target.result);
};
// Reading the file as a DataURL. When finished,
// this will trigger the onload function above:
reader.readAsDataURL(file);
message.hide();
preview.appendTo(dropbox);
// Associating a preview container
// with the file, using jQuery's $.data():
$.data(file,preview);
}
function showMessage(msg){
message.html(msg);
}
});
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'] == 0 ){
$pic = $_FILES['pic'];
$exp = explode(".", $pic['name']);
$ext = $exp[1];
$cadena = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_';
$tamano = 12;
$nvo = 'foto-';
for ($i=0; $i <= $tamano; $i++){
$rand = rand(0, strlen($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);
}