Posted By

jackoder on 06/25/10


Tagged

php image function upload


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

dantreacy
joacoleo
hoodlumpr
dubogii
HalfWinter
rmmjr


Image Upload with Php


 / Published in: PHP
 

You can use this script in your projects when you need an image upload function. It checks file type, file size and error report. I also want you to check giochi di spongebob and gratis mahjong too. I hope you will like this code line.

Have a nice work.

Best wishes.

  1. <!-- Form Area -->
  2. <form enctype="multipart/form-data" action="uploader.php" method="post">
  3. Select Image: <input type="file" name="userfile">
  4. <input type="submit" value="Upload!">
  5. </form>
  6. <!-- Form Area -->
  7.  
  8. <?php
  9.  
  10. # Variables
  11. $path = "images/";
  12. $max_size = "200000";
  13.  
  14. # File
  15. $filename = $_POST['userfile'];
  16.  
  17. # Control
  18. if (!isset($HTTP_POST_FILES['userfile'])) exit;
  19.  
  20. if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
  21.  
  22. if ($HTTP_POST_FILES['userfile']['size']>$max_size) {
  23. echo "The File is Too Big. The Max File Size is $max_size KB<br>n";
  24. }
  25.  
  26. # Type Control
  27. if (
  28. ($HTTP_POST_FILES['userfile']['type']=="image/gif") ||
  29. ($HTTP_POST_FILES['userfile']['type']=="image/jpg") ||
  30. ($HTTP_POST_FILES['userfile']['type']=="image/bmp") ||
  31. ($HTTP_POST_FILES['userfile']['type']=="image/png") ||
  32. ($HTTP_POST_FILES['userfile']['type']=="image/jpeg")
  33. )
  34. {
  35.  
  36. # If File Exist
  37. if (file_exists($path . $HTTP_POST_FILES['userfile']['name']))
  38. {
  39. echo "A File With That Name Already Exists!<br>";
  40. }
  41.  
  42. $res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
  43.  
  44. $HTTP_POST_FILES['userfile']['name']);
  45. if (!$res){
  46. echo "Upload Failed!<br>";
  47. }
  48. else{
  49. echo "Upload Sucessful!<br>";
  50. }
  51.  
  52. echo "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>";
  53. echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>";
  54. echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>";
  55. echo "<a href=$path".$HTTP_POST_FILES['userfile']['name'].">View Image</a>";
  56. }
  57. else
  58. {
  59. echo "Wrong File Type<br>";
  60. }
  61. }
  62.  
  63. ?>

Report this snippet  

You need to login to post a comment.