Jump to content

how would i go about making a simple file upload system?


Noskiw

Recommended Posts

Here's the barebones for jpeg and pdf - modify to suit your filenames:

 

<?php
//file upload
if (array_key_exists('submit',$HTTP_POST_VARS)){
if ($HTTP_POST_VARS['submit']) {
  print_r($HTTP_POST_FILES);
  if (!is_uploaded_file($HTTP_POST_FILES['file']['tmp_name'])) {
    echo "You did not upload a file!";
    unlink($HTTP_POST_FILES['file']['tmp_name']);
    // assign error message, remove uploaded file, redisplay form.
  } else {
    //a file was uploaded
    $maxfilesize=2500000;

    if ($HTTP_POST_FILES['file']['size'] > $maxfilesize) {
      echo "file is too large";
      unlink($HTTP_POST_FILES['file']['tmp_name']);
      // assign error message, remove uploaded file, redisplay form.
    } else {
      if ($HTTP_POST_FILES['file']['type'] != "application/pdf" AND $HTTP_POST_FILES['file']['type'] != "image/pjpeg") {
        echo "This file type is not allowed";
        unlink($HTTP_POST_FILES['file']['tmp_name']);
        // assign error message, remove uploaded file, redisplay form.
      } else {
       //File has passed all validation, copy it to the final destination and remove the temporary file:
       copy($HTTP_POST_FILES['file']['tmp_name'],"****YOUR UPLOAD LOCATION****".$HTTP_POST_FILES['file']['name']);
       unlink($HTTP_POST_FILES['file']['tmp_name']);
       echo "File has been successfully uploaded!";
       exit;
     }
    }
  }
}
}
?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
   <p><label for="photo">Choose a file to upload:</label>
   <input type="file" size="30" name="photo" /></p>
   <p><label></label>
   <input type="submit" name="submit" value="Upload" /></p>
</form>	

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.