Tuesday, August 18, 2009

PHP upload script

SkyHi @ Tuesday, August 18, 2009
[root@home ]# cat index.php

<html>
<form action="testupload.php" enctype="multipart/form-data" method="POST">
Please choose a file: 


</form>
<html>




[root@home]# cat testupload.php
<?php
//#mkdir UploadDirectory on the same directory as "testindex.php" 
//and #chmod to 777 if it is not running suphp

//Upload directory
$target = "UploadDirectory/";
$target = $target.basename ($_FILES['uploaded']['name']);
$ok = 1;
$time_start = microtime (true);

// Sleep for a while
usleep (100);


//This is our size condition
if ($uploaded_size > 500000)
  {
    echo "Your file is over 50MB.  ";
    $ok = 0;
  }

//This is our limit file type condition
if ($uploaded_type == "text/php")
  {
    echo "No PHP files";
    $ok = 0;
  }

//Here we check that $ok was not set to 0 by an error
if ($ok == 0)
  {
    Echo "Sorry your file was not uploaded";
  }

//If everything is ok we try to upload it
else
  {
    if (move_uploaded_file ($_FILES['uploaded']['tmp_name'], $target))
      {
        echo "The file ".basename ($_FILES['uploadedfile']['name']).
          " has been uploaded";
      }
    else
      {
        echo "Sorry, there was a problem uploading your file.";
      }
  }

echo '
'; $time_end = microtime (true); $time = $time_end - $time_start; echo "Did it in $time seconds"; ?>