irwan

PHP - Image Upload Function

Nov 15th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.71 KB | None | 0 0
  1. FILE 1 : upload.php
  2.  
  3.  
  4. <html>
  5. <body>
  6.  
  7. <!-- Form Area -->
  8. <form enctype="multipart/form-data" action="pro_uploader.php" method="post">
  9. Select Image: <input type="file" name="userfile">
  10. <input type="submit" value="Upload!">
  11. </form>
  12. <!-- Form Area -->
  13.  
  14. </body>
  15. </html>
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. FILE 2 : pro_uploader.php
  24.  
  25.  
  26.  
  27. <?php
  28.  
  29. # Variables
  30. $path = "images/";
  31. $max_size = "200000";
  32.  
  33. # File
  34. $filename = $_POST['userfile'];
  35.  
  36. # Control
  37. if (!isset($HTTP_POST_FILES['userfile'])) exit;
  38.  
  39. if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
  40.  
  41. if ($HTTP_POST_FILES['userfile']['size']>$max_size) {
  42. echo "The File is Too Big. The Max File Size is $max_size KB<br>n";
  43. exit;
  44. }
  45.  
  46. # Type Control
  47. if (
  48.    ($HTTP_POST_FILES['userfile']['type']=="image/gif")   ||        
  49.    ($HTTP_POST_FILES['userfile']['type']=="image/jpg")   ||  
  50.    ($HTTP_POST_FILES['userfile']['type']=="image/bmp")   ||
  51.    ($HTTP_POST_FILES['userfile']['type']=="image/png")   ||
  52.    ($HTTP_POST_FILES['userfile']['type']=="image/jpeg")
  53. )
  54. {
  55.  
  56. # If File Exist
  57. if (file_exists($path . $HTTP_POST_FILES['userfile']['name']))
  58. {
  59. echo "A File With That Name Already Exists!<br>";
  60. exit;
  61. }
  62.  
  63. $res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
  64.  
  65. $HTTP_POST_FILES['userfile']['name']);
  66. if (!$res){
  67. echo "Upload Failed!<br>";
  68. exit;
  69. }
  70. else{
  71. echo "Upload Sucessful!<br>";
  72. }
  73.  
  74. echo "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>";
  75. echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>";
  76. echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>";
  77. echo "<a href=$path".$HTTP_POST_FILES['userfile']['name'].">View Image</a>";
  78. }
  79. else
  80. {
  81. echo "Wrong File Type<br>";
  82. exit;
  83. }
  84. }
  85.  
  86. ?>
  87.  
Advertisement
Add Comment
Please, Sign In to add comment