En el código que pego aquí debajo, podrán apreciar una página que lo que hace es mostrar un form donde cargamos una serie de datos que se guardan en una base de datos hcha en MySQL. Pero algúnos de esos datos son archivos a subir al servidor, por lo cual tiene un campo del tipo "file", y aca es donde está el problema.
Si yo al form le pongo enctype="multipart/form-data" me sube el archivo al server pero no me inserta el nombre en la base de datos. si yo al form le quito enctype="multipart/form-data" me inserta bien el nombre del archivo en la Db pero no me sube el archivo físicio al server.
Si alguien me puede dar una mano con esto porque realmente no encuentro cual pueda ser el error.
Código PHP:
<?php require_once('../Connections/cnx_granalladora.php'); ?>
<?php
// Subimos archivos.
$extension = explode(".",$thumbs_name);
$num = count($extension)-1;
if($extension[$num] == "jpg")
{
if($thumbs_size < 120000)
{
if(!copy($thumbs, "../img_noticias/thumbs/".$thumbs_name))
{
echo "<font color='#FF0000'>Error al copiar el archivo</font>";
}
else
{
echo "<b><font color='#000000'>Archivo subido con éxito</font></b>";
}
}
else
{
echo "<font color='#FF0000'>El archivo supera los 120kb pemitidos</font>";
}
}
else
{
echo "<font color='#333333'>Usted no subió ninguna imágen en esta casilla ó el formato de archivo no es valido, solo puede ser .jpg</font>";
}
?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO noticas (titulo, texto, thumbs, pics, visibilidad, pdf) VALUES (%s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['titulo'], "text"),
GetSQLValueString($_POST['texto'], "text"),
GetSQLValueString($_POST['thumbs'], "text"),
GetSQLValueString($_POST['pics'], "text"),
GetSQLValueString($_POST['visibilidad'], "text"),
GetSQLValueString($_POST['pdf'], "text"));
mysql_select_db($database_cnx_granalladora, $cnx_granalladora);
$Result1 = mysql_query($insertSQL, $cnx_granalladora) or die(mysql_error());
$insertGoTo = "noticias_a.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
mysql_select_db($database_cnx_granalladora, $cnx_granalladora);
$query_rs_noticia_modf = "SELECT * FROM noticas ORDER BY Id_noticia DESC";
$rs_noticia_modf = mysql_query($query_rs_noticia_modf, $cnx_granalladora) or die(mysql_error());
$row_rs_noticia_modf = mysql_fetch_assoc($rs_noticia_modf);
$totalRows_rs_noticia_modf = mysql_num_rows($rs_noticia_modf);
?>
Código HTML:
<form method="post" name="form1" enctype="multipart/form-data" action="<?php echo $editFormAction; ?>"> <table align="center"> <tr valign="baseline"> <td nowrap align="right">Titulo:</td> <td><input type="text" name="titulo" value="" size="32"></td> </tr> <tr valign="baseline"> <td nowrap align="right" valign="top">Texto:</td> <td><textarea name="texto" cols="50" rows="5"></textarea> </td> </tr> <tr valign="baseline"> <td nowrap align="right"><p>Foto Chica :</p> </td> <td><input type='file' name="thumbs" value="" size="32"></td> </tr> <tr valign="baseline"> <td nowrap align="right">Foto Grande :</td> <td><input type="file" name="pics" value="" size="32"></td> </tr> <tr valign="baseline"> <td nowrap align="right">Visibilidad:</td> <td><select name="visibilidad"> <option value="si" selected>Si</option> <option value="no">No</option> </select> </td> <tr> <tr valign="baseline"> <td nowrap align="right">PDF:</td> <td><input type="file" name="pdf" value="" size="32"></td> </tr> <tr valign="baseline"> <td nowrap align="right"> </td> <td><input type="submit" value="Insertar registro"></td> </tr> </table> <input type="hidden" name="MM_insert" value="form1"> </form>
Muchas gracias por la ayuda y el tiempo dedicado!!
![Aplauso](http://static.forosdelweb.com/fdwtheme/images/smilies/aplausos.gif)