Ver Mensaje Individual
  #1 (permalink)  
Antiguo 01/10/2010, 03:32
rafaxusirati
 
Fecha de Ingreso: julio-2010
Ubicación: Cerca Sitges (Barcelona)
Mensajes: 98
Antigüedad: 13 años, 9 meses
Puntos: 4
formulario subir ruta imagen a base datos Mysql

Me gustaría con el siguiente formulario, obtener la ruta de una imagen y guardarla en la base de datos, la imagen la subiría al servidor en la carpeta imagenes/
No consigo realizarlo, me da error:

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhost/anuncialoquequieras.es/home/html/imagenes/contactos.php:12) in /var/www/vhost/anuncialoquequieras.es/home/html/imagenes/upload2.php on line 79.

La ruta es almacenada en la BD Mysql como "498f2a_P1020074.jpg" y tendria que ser "imagenes/498f2a_P1020074.jpg" ya que la ruta del servidor es "carpeta imagenes/"

La imagen se almacena correctamente en el servidor, pero la dirección que da la BD es incorrecta, y no puede leer el archivo.

¿Alguna sugerencia?

¿Dónde podría obtener un código parecido??

Os adjunto código página.

Muchas Gracias.


Código PHP:
Ver original
  1. <?php require_once('contactos.php'); ?>
  2. <?php
  3. $prefijo = substr(md5(uniqid(rand())),0,6);
  4. $directorio = ''.$prefijo.'_';
  5. (move_uploaded_file($_FILES['foto1']['tmp_name'], $directorio . $_FILES['foto1']['name']));
  6. if (!function_exists("GetSQLValueString")) {
  7. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  8. {
  9. if (PHP_VERSION < 6) {
  10. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  11. }
  12.  
  13. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  14.  
  15. switch ($theType) {
  16. case "text":
  17. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  18. break;
  19. case "long":
  20. case "int":
  21. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  22. break;
  23. case "double":
  24. $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  25. break;
  26. case "date":
  27. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  28. break;
  29. case "defined":
  30. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  31. break;
  32. }
  33. return $theValue;
  34. }
  35. }
  36.  
  37. $editFormAction = $_SERVER['PHP_SELF'];
  38. if (isset($_SERVER['QUERY_STRING'])) {
  39. $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  40. }
  41.  
  42. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  43. $insertSQL = sprintf("INSERT INTO contactos (idempleado, titulo, contenido, foto1) VALUES (%s, %s, %s, %s)",
  44. GetSQLValueString($_POST['idempleado'], "int"),
  45. GetSQLValueString($_POST['titulo'], "text"),
  46. GetSQLValueString($_POST['contenido'], "text"),
  47. GetSQLValueString($directorio . $_FILES['foto1']['name'], "text"));
  48.  
  49. //Nombre de la carpeta donde se guardará el archivo.
  50. $carpeta = "../imagenes";
  51. //Nombre del archivo
  52. $archivo = $_FILES['seleccion']['name'];
  53. //Nombre tempoaral del archivo
  54. $temporal = $_FILES['seleccion']['tmp_name'];
  55. //Tamaño del archivo
  56. $tamano = $_FILES['seleccion']['size'];
  57. //Tipo de archivo
  58. $tipo = $_FILES['seleccion']['type'];
  59. //URL de la imagen.
  60. $foto1 = $carpeta . basename($archivo);
  61. //Subimos el archivo
  62. if(move_uploaded_file($temporal, $foto1)) {
  63. //Si todo salio bien
  64. $estado = "El archivo . . . . ". basename($archivo).". . . . Se ha subido con exito";
  65. //Si fallo
  66. } else {
  67. //Advertir el fallo
  68. $estado = "Hubo un error al subir el archivo.<br>Posiblemente es mayor a lo 200KB permitido.<br>Inténtelo de nuevo!";
  69. }
  70.  
  71. mysql_select_db($database_contactos, $contactos);
  72. $Result1 = mysql_query($insertSQL, $contactos) or die(mysql_error());
  73.  
  74. $insertGoTo = "imagenes.php";
  75. if (isset($_SERVER['QUERY_STRING'])) {
  76. $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
  77. $insertGoTo .= $_SERVER['QUERY_STRING'];
  78. }
  79. header(sprintf("Location: %s", $insertGoTo));
  80. }
  81. ?>
  82. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  83. <html xmlns="http://www.w3.org/1999/xhtml">
  84. <head>
  85. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  86. <title>Documento sin título</title>
  87. </head>
  88.  
  89. <body>
  90. <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1" enctype="multipart/form-data">
  91. <table align="center">
  92. <tr valign="baseline">
  93. <td nowrap="nowrap" align="right">Titulo:</td>
  94. <td><input type="text" name="titulo" value="" size="32" /></td>
  95. </tr>
  96. <tr valign="baseline">
  97. <td nowrap="nowrap" align="right" valign="top">Contenido:</td>
  98. <td><textarea name="contenido" cols="50" rows="5"></textarea></td>
  99. </tr>
  100. <tr valign="baseline">
  101. <td nowrap="nowrap" align="right">&nbsp;</td>
  102. <td>&nbsp;</td>
  103. </tr>
  104. <tr valign="baseline">
  105. <td nowrap="nowrap" align="right">&nbsp;</td>
  106. <td>
  107. <label>
  108. <input type="hidden" name="MAX_FILE_SIZE" id="subir" value="200000" />
  109. Elija un archivo para cargar :<br /><input type="file" name="foto1" id="foto1"><br />
  110. </label>
  111. </td>
  112. </tr>
  113. <tr valign="baseline">
  114. <td nowrap="nowrap" align="right">&nbsp;</td>
  115. <td>&nbsp;</td>
  116. </tr>
  117. <tr valign="baseline">
  118. <td nowrap="nowrap" align="right">Nombre del archivo:</td>
  119. <td><input type="text" name="foto1" value="" size="32" /></td>
  120. </tr>
  121. <tr valign="baseline">
  122. <td nowrap="nowrap" align="right">&nbsp;</td>
  123. <td><input type="submit" value="Insertar registro" /></td>
  124. </tr>
  125.  
  126. </table>
  127. <input type="hidden" name="id_noticia" value="" />
  128. <input type="hidden" name="MM_insert" value="form1" />
  129. </form>
  130. <p>&nbsp;</p>
  131. </body>
  132. </html>

Última edición por rafaxusirati; 01/10/2010 a las 03:45