Foros del Web » Programando para Internet » PHP »

Agregarle insert a este codigo

Estas en el tema de Agregarle insert a este codigo en el foro de PHP en Foros del Web. Hola nuevamente, bueno tengo este codigo para subir archivos, lo que hace es que los sube a la carpeta del servidor todo bien, pero que ...
  #1 (permalink)  
Antiguo 21/04/2010, 12:23
 
Fecha de Ingreso: enero-2010
Mensajes: 198
Antigüedad: 14 años, 3 meses
Puntos: 1
Agregarle insert a este codigo

Hola nuevamente, bueno tengo este codigo para subir archivos, lo que hace es que los sube a la carpeta del servidor todo bien, pero que pasa yo quiero que ademas me inserte el nombre del archivo en la bd, por ejemplo 113434.jpg
Que deberia agregarle al codigo donde le tendria que colocar el insert into.

Les muestro el codigo uploadprocess.php:

Código PHP:
Ver original
  1. <?php
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. session_name('pLogin');
  4.  
  5. include("connect.php");
  6.  
  7. $id=$_SESSION['id'];
  8. //define a maxim size for the uploaded images in Kb
  9.  define ("MAX_SIZE","500");
  10.  
  11. //This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
  12.  function getExtension($str) {
  13.          $i = strrpos($str,".");
  14.          if (!$i) { return ""; }
  15.          $l = strlen($str) - $i;
  16.          $ext = substr($str,$i+1,$l);
  17.          return $ext;
  18.  }
  19.  
  20. //This variable is used as a flag. The value is initialized with 0 (meaning no error  found)  
  21. //and it will be changed to 1 if an errro occures.  
  22. //If the error occures the file will not be uploaded.
  23.  $errors=0;
  24. //checks if the form has been submitted
  25.  if(isset($_POST['Submit']))
  26.  {
  27.     //reads the name of the file the user submitted for uploading
  28.     $image=$_FILES['image']['name'];
  29.     //if it is not empty
  30.     if ($image)
  31.     {
  32.     //get the original name of the file from the clients machine
  33.         $filename = stripslashes($_FILES['image']['name']);
  34.     //get the extension of the file in a lower case format
  35.         $extension = getExtension($filename);
  36.         $extension = strtolower($extension);
  37.     //if it is not a known extension, we will suppose it is an error and will not  upload the file,  
  38.     //otherwise we will do more tests
  39.  if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
  40.         {
  41.         //print error message
  42.             echo '<h1>Extension desconocida</h1>';
  43.             $errors=1;
  44.         }
  45.         else
  46.         {
  47. //get the size of the image in bytes
  48.  //$_FILES['image']['tmp_name'] is the temporary filename of the file
  49.  //in which the uploaded file was stored on the server
  50.  $size=filesize($_FILES['image']['tmp_name']);
  51.  
  52. //compare the size with the maxim size we defined and print error if bigger
  53. if ($size > MAX_SIZE*1024)
  54. {
  55.     echo '<h1>Se ha excedido el limite de subida</h1>';
  56.     $errors=1;
  57. }
  58.  
  59. //we will give an unique name, for example the time in unix time format
  60. $image_name=time().'.'.$extension;
  61. //the new name will be containing the full path where will be stored (images folder)
  62. $newname="images/".$image_name;
  63. //we verify if the image has been uploaded, and print error instead
  64. $copied = copy($_FILES['image']['tmp_name'], $newname);
  65. if (!$copied)
  66. {
  67.     echo '<h1>No se ha podido subir el archivo</h1>';
  68.     $errors=1;
  69. }}}}
  70.  
  71. //If no errors registred, print the success message
  72.  if(isset($_POST['Submit']) && !$errors)
  73.  {
  74.     echo "<h1>Archivo subido correctamente</h1>";
  75.  }
  76.  
  77.  
  78.  ?>


y el form:

Código HTML:
Ver original
  1. <form name="newad" method="post" enctype="multipart/form-data"  action="uploadprocess.php">
  2.  <table>
  3.     <tr><td><input type="file" name="image"></td></tr>
  4.     <tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
  5.  </table>  
  6.  </form>
  #2 (permalink)  
Antiguo 21/04/2010, 12:26
Avatar de darkasecas  
Fecha de Ingreso: marzo-2005
Ubicación: SantaCata, NL, Mexico
Mensajes: 1.553
Antigüedad: 19 años, 1 mes
Puntos: 77
Respuesta: Agregarle insert a este codigo

Pues supongo que el codigo para insertar iria en este caso en donde imprimes que se subio el archivo, y es una simple query insert (o update, depende como estes trabajando la bd), con mysql_query()
  #3 (permalink)  
Antiguo 21/04/2010, 12:56
 
Fecha de Ingreso: enero-2010
Mensajes: 198
Antigüedad: 14 años, 3 meses
Puntos: 1
Respuesta: Agregarle insert a este codigo

Hola, bueno hice lo que me dijiste, el codigo quedó asi y funciona.

Código PHP:
Ver original
  1. <?php
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. session_name('pLogin');
  4.  
  5. include("connect.php");
  6.  
  7. $id=$_SESSION['id'];
  8. //define a maxim size for the uploaded images in Kb
  9.  define ("MAX_SIZE","500");
  10.  
  11. //This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
  12.  function getExtension($str) {
  13.          $i = strrpos($str,".");
  14.          if (!$i) { return ""; }
  15.          $l = strlen($str) - $i;
  16.          $ext = substr($str,$i+1,$l);
  17.          return $ext;
  18.  }
  19.  
  20. //This variable is used as a flag. The value is initialized with 0 (meaning no error  found)  
  21. //and it will be changed to 1 if an errro occures.  
  22. //If the error occures the file will not be uploaded.
  23.  $errors=0;
  24. //checks if the form has been submitted
  25.  if(isset($_POST['Submit']))
  26.  {
  27.     //reads the name of the file the user submitted for uploading
  28.     $image=$_FILES['image']['name'];
  29.     //if it is not empty
  30.     if ($image)
  31.     {
  32.     //get the original name of the file from the clients machine
  33.         $filename = stripslashes($_FILES['image']['name']);
  34.     //get the extension of the file in a lower case format
  35.         $extension = getExtension($filename);
  36.         $extension = strtolower($extension);
  37.     //if it is not a known extension, we will suppose it is an error and will not  upload the file,  
  38.     //otherwise we will do more tests
  39.  if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
  40.         {
  41.         //print error message
  42.             echo '<h1>Unknown extension!</h1>';
  43.             $errors=1;
  44.         }
  45.         else
  46.         {
  47. //get the size of the image in bytes
  48.  //$_FILES['image']['tmp_name'] is the temporary filename of the file
  49.  //in which the uploaded file was stored on the server
  50.  $size=filesize($_FILES['image']['tmp_name']);
  51.  
  52. //compare the size with the maxim size we defined and print error if bigger
  53. if ($size > MAX_SIZE*1024)
  54. {
  55.     echo '<h1>You have exceeded the size limit!</h1>';
  56.     $errors=1;
  57. }
  58.  
  59. //we will give an unique name, for example the time in unix time format
  60. $image_name=time().'.'.$extension;
  61. //the new name will be containing the full path where will be stored (images folder)
  62. $newname="images/".$image_name;
  63. //we verify if the image has been uploaded, and print error instead
  64. $copied = copy($_FILES['image']['tmp_name'], $newname);
  65. if (!$copied)
  66. {
  67.     echo '<h1>Copy unsuccessfull!</h1>';
  68.     $errors=1;
  69. }}}}
  70.  
  71. //If no errors registred, print the success message
  72.  if(isset($_POST['Submit']) && !$errors)
  73.  {
  74.     echo "<h1>File Uploaded Successfully! Try again!</h1>";
  75.     mysql_query("
  76. UPDATE prueba
  77. SET picture='$image_name'
  78. WHERE id='$_SESSION[id]'  ");
  79.  }
  80.  
  81.  
  82.  ?>

Ahora como hago si quiero que el usuario suba mas archivos, creo por ejemplo tres campos mas en la bd con los nombres picture1 picture2, etc, que le agrego al codigo para que los suba ? el mismo update?

Etiquetas: insert
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:27.