Guest User

Untitled

a guest
Sep 4th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.36 KB | None | 0 0
  1. <?php
  2. include ('mysql_connect.php');
  3.  
  4. //define a maxim size for the uploaded images in Kb
  5. define ("MAX_SIZE","1000");
  6.  
  7. //This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
  8. function getExtension($str) {
  9.     $i = strrpos($str,".");
  10.     if (!$i) { return ""; }
  11.     $l = strlen($str) - $i;
  12.     $ext = substr($str,$i+1,$l);
  13.     return $ext;
  14. }
  15.  
  16. //This variable is used as a flag. The value is initialized with 0 (meaning no error  found)  
  17. //and it will be changed to 1 if an errro occures.  
  18. //If the error occures the file will not be uploaded.
  19. $errors=0;
  20. //checks if the form has been submitted
  21. if(isset($_POST['Submit']))
  22. {
  23.     //reads the name of the file the user submitted for uploading
  24.     $image=$_FILES['image']['name'];
  25.     //if it is not empty
  26.     if ($image)
  27.     {
  28.     //get the original name of the file from the clients machine
  29.         $filename = stripslashes($_FILES['image']['name']);
  30.     //get the extension of the file in a lower case format
  31.         $extension = getExtension($filename);
  32.         $extension = strtolower($extension);
  33.     //if it is not a known extension, we will suppose it is an error and will not  upload the file,  
  34.     //otherwise we will do more tests
  35. if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
  36.     {
  37.     //print error message
  38.         echo '<h2>Unknown extension!</h2>';
  39.         $errors=1;
  40.     }
  41.     else
  42.     {
  43. //get the size of the image in bytes
  44. //$_FILES['image']['tmp_name'] is the temporary filename of the file
  45. //in which the uploaded file was stored on the server
  46. $size=filesize($_FILES['image']['tmp_name']);
  47.  
  48. //compare the size with the maxim size we defined and print error if bigger
  49. if ($size > MAX_SIZE*1024)
  50. {
  51.     echo '<h2>You have exceeded the size limit! Limit is 1000kb!</h2>';
  52.     $errors=1;
  53. }
  54.  
  55. //we will give an unique name, for example the time in unix time format
  56. $image_name=time().'.'.$extension;
  57. //the new name will be containing the full path where will be stored (images folder)
  58. $newname="images/".$image_name;
  59. //we verify if the image has been uploaded, and print error instead
  60. $copied = copy($_FILES['image']['tmp_name'], $newname);
  61. if (!$copied)
  62. {
  63.     echo '<h2>Copy unsuccessfull!</h2>';
  64.     $errors=1;
  65. }}}}
  66.  
  67. //If no errors registred, print the success message
  68. if(isset($_POST['Submit']) && !$errors)
  69. {
  70.     echo "<h2>File Uploaded Successfully!</h2>";
  71.    
  72.     //This gets all the other information from the form
  73.     $name=$_POST['strainname'];
  74.     $author=$_POST['author'];
  75.     $source=$_POST['source'];
  76.     $type=$_POST['type'];
  77.     $looks=$_POST['looks'];
  78.     $smell=$_POST['smell'];
  79.     $taste=$_POST['taste'];
  80.     $effect=$_POST['effect'];
  81.     $potency=$_POST['potency'];
  82.     $review=$_POST['review'];
  83.     $date=$_POST['date'];
  84.    
  85.     //Writes the information to the database
  86.     $query = "INSERT INTO strain_review (strainname, author, source, type, looks, smell, taste, effect, potency, imagelink, review, date) VALUES ('$strainname', '$author', '$source', '$type', '$looks', '$smell', '$taste', '$effect', '$potency', '$newname', '$review', NOW())";
  87.     $result = @mysql_query($query);
  88.  
  89.     //Writes the photo to the server
  90.     if(move_uploaded_file($_FILES['image']['newname'], "/images/"))
  91.     {
  92.  
  93.     //Tells you if its all ok
  94.     echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
  95.     }
  96.     else {
  97.  
  98.     //Gives and error if its not
  99.     echo "Sorry, there was a problem uploading your file.";
  100.     }
  101.  
  102. }
  103. ?>
Advertisement
Add Comment
Please, Sign In to add comment