Ver Mensaje Individual
  #3 (permalink)  
Antiguo 21/04/2010, 12:56
den_22
 
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?