PHP/Form/File Upload
Содержание
File Uploader
<source lang="html4strict">
<html>
<head> <title>File Uploader</title> </head> <body>
File Upload
Select a file to upload:
<form action="uploader.php" method="post" enctype="multipart/form-data"> <input type="file" name="file" size="45">
<input type="submit" value="Upload File"> </form> </body>
</html>
File: uploader.php
<?php
if( $_FILES["file"]["name"] != "" ) { copy ( $_FILES["file"]["tmp_name"], "C:/Apache/htdocs/" . $_FILES["file"]["name"] ) or die( "Could not copy file" ); } else { die( "No file specified" ); }
?> <html>
<head> <title>Upload complete</title> </head> <body>
File upload succeeded...
- Sent: <?php echo $_FILES["file"]["name"]; ?>
- Size: <?php echo $_FILES["file"]["size"]; ?> bytes
- Type: <?php echo $_FILES["file"]["type"]; ?>
<a href="<?php echo $_FILES["file"]["name"]; ?>">Click here to view file</a> </body>
</html>
</source>
File Uploading Error Constants in PHP
<source lang="html4strict">
UPLOAD_ERR_OK No error occurred.
UPLOAD_ERR_INI_SIZE The uploaded file exceeds the maximum value specified in the php.ini file.
UPLOAD_ERR_FORM_SIZE The uploaded file exceeds the maximum value specified by the MAX_FILE_SIZE hidden widget.
UPLOAD_ERR_PARTIAL The file upload was canceled and only part of the file was uploaded.
UPLOAD_ERR_NOFILE No file was uploaded.
</source>
File Upload Script
<source lang="html4strict">
<html>
<head> <title>A File Upload Script</title> </head> <body>
<?php if ( isset( $_FILES["fupload"] ) ) { print "name: ". $_FILES["fupload"]["name"] ."$imgstr = "
"; print "size: ". $_FILES["fupload"]["size"] ." bytes
"; print "temp name: ".$_FILES["fupload"]["tmp_name"] ."
"; print "type: ". $_FILES["fupload"]["type"] ."
"; print "error: ". $_FILES["fupload"]["error"] ."
"; if ( $_FILES["fupload"]["type"] == "image/gif" ) { $source = $_FILES["fupload"]["tmp_name"]; $target = "upload/".$_FILES["fupload"]["name"]; move_uploaded_file( $source, $target );// or die ("Couldn"t copy"); $size = getImageSize( $target );
<img width=\"$size[0]\" height=\"$size[1]\" "; $imgstr .= "src=\"$target\" alt=\"uploaded image\" />
";print $imgstr; } } ?>
<form enctype="multipart/form-data" action="<?php print $_SERVER["PHP_SELF"]?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="102400" />
<input type="file" name="fupload" />
<input type="submit" value="upload!" />
</form> </body> </html> </source>
Processing an uploaded file
<source lang="html4strict">
<?php $maxsize=28480; if (!$HTTP_POST_VARS["submit"]) {
$error=" ";
} if (!is_uploaded_file($HTTP_POST_FILES["upload_file"]["tmp_name"]) AND !isset($error)) {
$error = "You must upload a file!
"; unlink($HTTP_POST_FILES["upload_file"]["tmp_name"]);
} if ($HTTP_POST_FILES["upload_file"]["size"] > $maxsize AND !isset($error)) {
$error = "Error, file must be less than $maxsize bytes.
"; unlink($HTTP_POST_FILES["upload_file"]["tmp_name"]);
} if (!isset($error)) {
move_uploaded_file($HTTP_POST_FILES["upload_file"]["tmp_name"], "uploads/".$HTTP_POST_FILES["upload_file"]["name"]); print "Thank you for your upload."; exit;
} else {
echo ("$error");
} ?> <html> <head></head> <body> <form action="<?php echo(htmlspecialchars($_SERVER["PHP_SELF"]))?>" method="post" enctype="multipart/form-data">
Choose a file to upload:
<input type="file" name="upload_file" size="80">
<input type="submit" name="submit" value="submit">
</form> </body> </html>
</source>
Simple File Upload Form
<source lang="html4strict">
<html> <head> <title>A Simple File Upload Form</title> </head> <body> <form enctype="multipart/form-data"
action="<?print $_SERVER["PHP_SELF"]?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="102400" />
<input type="file" name="fupload" />
<input type="submit" value="upload!" />
</form> </body> </html>
</source>