Simple picture/file upload script


Here is a simple tutorial script that uploads a file to a folder on the server. In this example it is assumed that the file is an image and it will be outputted on screen in an img-tag. But the file uploading mechanics works for any file type.


<?php
if(isset($_POST['submit']))
{
  // get the temporary filename/path on the server
  $temp_name = $_FILES["file"]["tmp_name"]; 
  $name = $_FILES["file"]["name"]; // get the filename of the actual file

// print the array (for reference)
  print_r($_FILES);

  // Create uploads folder if it doesn't exist.
  if (!file_exists("uploads")) {
    mkdir("uploads", 0755);
    // Set read and write permissions of folder, needed on some servers
    chmod("uploads", 0755); 
  }

  // Move file from temp to uploads folder
  move_uploaded_file($temp_name, "uploads/$name");
  chmod("uploads/$name", 0644); // Set read and write permissions if file
}
?>

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="submit"/>
</form>

[/php]
PHP

Leave a Reply

Your email address will not be published. Required fields are marked *