Difference between revisions of "CSC220 uploadFile2.htm"
(Created page with '--~~~~ ---- __TOC__ <br /> =FileUpload1.htm= <code><pre> </pre></code> =FileUpload1.php= <code><pre> </pre></code> <br /> <br /> <br /> <br /> <br /> <br /> <br /> …') |
(→FileUpload2.php) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
<br /> | <br /> | ||
− | = | + | =FileUpload2.htm= |
+ | (This is the same as [[CSC220 uploadFile1.htm|uploadFile1.htm]].) | ||
+ | |||
<code><pre> | <code><pre> | ||
+ | <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> | ||
</pre></code> | </pre></code> | ||
+ | =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. | ||
− | |||
<code><pre> | <code><pre> | ||
+ | <?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(); | ||
+ | |||
+ | ?> | ||
+ | |||
Latest revision as of 10:18, 8 November 2010
--D. Thiebaut 15:08, 8 November 2010 (UTC)
Contents
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();
?>