Monday 24 December 2012

PHP file uploader script

1. cat fileupload.php

<html>
<body>

<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Upload" />
</form>

</body>
</html>


2.  cat upload.php
<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "File uploaded successfully. Here are the details... ". "<br />";
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
 
   move_uploaded_file($_FILES["file"]["tmp_name"],
   "upload/" . $_FILES["file"]["name"]);
  }
?>



NOTE: Create a directory "upload" on the same location [ accessible by apache ], and later give full access to that directory and protect that directory:

 # some directories must be protected
<Directory /path/to/dir/upload>
        Options -FollowSymLinks
        AllowOverride None
</Directory>


No comments:

Post a Comment