CSC220 uploadFile2.htm

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 15:08, 8 November 2010 (UTC)



FileUpload2.htm

(This is the same as uploadFile1.htm.)


<html>
<body>

<form enctype ="multipart/form-data"
      action  ="uploadFile2.php"
      method  ="POST">

File name: <input name="uploaded" type="file" />
   
<input type="submit" value="Upload" />

</form>

</body>
</html>

FileUpload2.php

  • This php program will store the uploaded file in the subdirectory call uploads located in the same directory where it resides. The uploads directory must be readable and writable by all.
<?php

//--------------------------------------------------
function printHtmlHead() {
?>
<html>
<body>

<?php
}

//--------------------------------------------------
function printHtmlTail() {
?>

</body>
</html>
<?php
}

//--------------------------------------------------
function displayTable( $caption, $table ) {
  printf( "<h3>%s</h3>\n", $caption );
  echo "<pre>\n";
  print_r( $table );
  echo "</pre>\n";
}

//--------------------------------------------------
function storeFile( ) {
  $originalName = $_FILES['uploaded']['name'];
  $tmp_name     = $_FILES['uploaded']['tmp_name'];
  $destination  = "uploads/" . $originalName;

  print "originalName = $originalName <br />";
  print "tmp_name = $tmp_name <br />";
  print "destination = $destination <br />";

  if ( ! move_uploaded_file( $tmp_name, $destination ) )
    echo "<font color=\"red\"><P>Upload unsuccessful<P></font>\n";
  else
    echo "<P>Upload successful<P>\n";
}

//--------------------------------------------------
//                      M A I N
//--------------------------------------------------
printHtmlHead();
storeFile();
printHtmlTail();

?>