lunes, 17 de septiembre de 2007

EDITAR subiendo un fichero

//********** UPDATE

// si hay imagen la sube
$fichero="";
$enquery="";

$dir = "../cliente/descargas/"; // directorio donde se sube el fichero POST
$nombrefichero="fichero"; // nombre de la variable del fichero POST
$tamanyoimagen=200; // tamaño a redimensionar
$lado="w"; // w HORIZONTAL, h VERTICAL
$redimensiona="no"; // si PARA SI, no PARA NO
$variableentabla="descfichero"; // nombre del fichero en la tabla
$variabledeborrado="borrarfichero"; // nombre de varible de BORRAR el fichero
$valorvariableborradosi="1"; // valor de la varibale POST de borrar
$valorvariableborradono="0"; // valor de la varibale POST de NO borrar


// mira si habia un fichero antes
$query="SELECT * from descargas where descid='".$_POST['id']."'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$ficheroantiguo=$row[$variableentabla];


if (is_uploaded_file($_FILES[$nombrefichero]['tmp_name']))
{

// si habia un fichero antiguo lo borra
if ($ficheroantiguo!="")
{
$borrar = $dir.$row[$variableentabla];
unlink($borrar);
}
//***

// sube el nuevo fichero con la ID delante del nombre
$nombre = $_FILES[$nombrefichero]['name'];
$fichero=$_POST['id'].$nombre;
$temp = $dir.$fichero;

move_uploaded_file($_FILES[$nombrefichero]['tmp_name'], $temp);

if ($redimensiona=="si")
redimensionar_ok($temp2, $temp2, $lado, $tamanyoimagen);

$enquery=",".$variableentabla."='".$fichero."'";
}
else // SI NO SUBE FICHERO
{

// si se quiere borrar el antiguo sin subir ninguno
if ($_POST[$variabledeborrado]==$valorvariableborradosi)
{

// si habia un fichero antiguo lo borra
if ($ficheroantiguo!="")
{
$borrar = $dir.$row[$variableentabla];
unlink($borrar);
}
//***

$fichero="";

$enquery=",".$variableentabla."=''";
}

// si NO se quiere borrar el antiguo sin subir ninguno
if ($_POST[$variabledeborrado]==$valorvariableborradono)
{
$enquery="";
}
}



$query="UPDATE descargas set `desctitulo_es`='".$_POST['tituloes']."',`desctitulo_en`='".$_POST['tituloen']."',`desctitulo_va`='".$_POST['titulova']."' ".$enquery." where `descid`='".$_POST['id']."'";
$result = mysql_query($query) or die(mysql_error());

No hay comentarios: