Ver Mensaje Individual
  #7 (permalink)  
Antiguo 10/03/2012, 09:40
Avatar de Etherman
Etherman
 
Fecha de Ingreso: abril-2011
Mensajes: 93
Antigüedad: 13 años
Puntos: 2
Respuesta: subida de archivos php

Hola, no te sé responder, pero probablemente puede darte una referencia para saber si funciona, el formulario en el que estoy trabajando. Es un php muy sencillo para subir una imagen, y en el mismo directorio donde se encuentre, hay que tener una carpeta llamada images (donde aparecerá la imagen que estas subiendo.)

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