Monday, January 11, 2010

File upload using PHP

<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>

4 comments:

souravkumarpaul said...

What types of error may occur while uploading a file in php?

kajal4ever said...

1.File path not found.
2.File too large.
Allowed memory size of 8388608 bytes exhausted (tried to
allocate 40 bytes) in Unknown on line X.

souravkumarpaul said...

What is $_SERVER['PHP_SELF'];?

kajal4ever said...

The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.