PHP/Form/File Upload
Содержание
File Uploader
<html>
<head>
<title>File Uploader</title>
</head>
<body>
<h3>File Upload</h3>
Select a file to upload:<br>
<form action="uploader.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="45">
<br>
<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>
<h3>File upload succeeded...</h3>
<ul>
<li>Sent: <?php echo $_FILES["file"]["name"]; ?></li>
<li>Size: <?php echo $_FILES["file"]["size"]; ?> bytes</li>
<li>Type: <?php echo $_FILES["file"]["type"]; ?></li>
</ul>
<a href="<?php echo $_FILES["file"]["name"]; ?>">Click here to view file</a>
</body>
</html>
File Uploading Error Constants in PHP
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.
File Upload Script
<html>
<head>
<title>A File Upload Script</title>
</head>
<body>
<div>
<?php
if ( isset( $_FILES["fupload"] ) ) {
print "name: ". $_FILES["fupload"]["name"] ."<br />";
print "size: ". $_FILES["fupload"]["size"] ." bytes<br />";
print "temp name: ".$_FILES["fupload"]["tmp_name"] ."<br />";
print "type: ". $_FILES["fupload"]["type"] ."<br />";
print "error: ". $_FILES["fupload"]["error"] ."<br />";
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 );
$imgstr = "<p><img width=\"$size[0]\" height=\"$size[1]\" ";
$imgstr .= "src=\"$target\" alt=\"uploaded image\" /></p>";
print $imgstr;
}
}
?>
</div>
<form enctype="multipart/form-data"
action="<?php print $_SERVER["PHP_SELF"]?>" method="post">
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="102400" />
<input type="file" name="fupload" /><br/>
<input type="submit" value="upload!" />
</p>
</form>
</body>
</html>
Processing an uploaded file
<?php
$maxsize=28480;
if (!$HTTP_POST_VARS["submit"]) {
$error=" ";
}
if (!is_uploaded_file($HTTP_POST_FILES["upload_file"]["tmp_name"]) AND !isset($error)) {
$error = "<b>You must upload a file!</b><br /><br />";
unlink($HTTP_POST_FILES["upload_file"]["tmp_name"]);
}
if ($HTTP_POST_FILES["upload_file"]["size"] > $maxsize AND !isset($error)) {
$error = "<b>Error, file must be less than $maxsize bytes.</b><br /><br />";
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:<br />
<input type="file" name="upload_file" size="80">
<br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Simple File Upload Form
<html>
<head>
<title>A Simple File Upload Form</title>
</head>
<body>
<form enctype="multipart/form-data"
action="<?print $_SERVER["PHP_SELF"]?>" method="post">
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="102400" />
<input type="file" name="fupload" /><br/>
<input type="submit" value="upload!" />
</p>
</form>
</body>
</html>