Basic File Upload System


Allow users to upload files to your website

The HTML Element of our file uploader

PHP Code
  1. <html>
  2. <body><form action="doupload.php" method="post"
  3. enctype="multipart/form-data">
  4. <label for="file">Upload File:</label>
  5. <input type="file" name="file" id="file" />
  6.  
  7. <input type="submit" name="submit" value="Submit" />
  8. </form></body>
  9. </html>


The PHP Element of our file uploader (doupload.php)

doupload.php
  1. <?php
  2. if ((($_FILES["file"]["type"] == "image/gif")
  3. || ($_FILES["file"]["type"] == "image/jpeg")
  4. || ($_FILES["file"]["type"] == "image/png"))
  5. && ($_FILES["file"]["size"] < 30000))
  6. {
  7. if ($_FILES["file"]["error"] > 0)
  8. {
  9. echo "Return Code: " . $_FILES["file"]["error"] . "";
  10. }
  11. else
  12. {
  13. echo "Upload: " . $_FILES["file"]["name"] . "";
  14. echo "Type: " . $_FILES["file"]["type"] . "";
  15. echo "Size (Kb): " . ($_FILES["file"]["size"] / 1024) . " ";
  16. echo "Temp file: " . $_FILES["file"]["tmp_name"] . ""; if (file_exists("upload/" . $_FILES["file"]["name"]))
  17. {
  18. echo $_FILES["file"]["name"] . " already exists. ";
  19. }
  20. else
  21. {
  22. move_uploaded_file($_FILES["file"]["tmp_name"],
  23. "uploads/" . $_FILES["file"]["name"]);
  24. echo "Saved to: " . "upload/" . $_FILES["file"]["name"];
  25. }
  26. }
  27. }
  28. else
  29. {
  30. echo "Invalid file";
  31. }
  32. ?>


Enjoy :)
Dava's Avatar
Author:
Views:
2,414
Rating:
Posted on Sunday 27th July 2008 at 05:01 PM
ilyas-shezad
ilyas-shezad's Avatar
I love it! Nice good work on this one.
I've worked with other upload scripts before but i like this one its nice and simple.