Upload PHP Script Trouble
Posted by: soul searching
Posted on: 2005-06-07 09:09:00
Trying to create a form to allow users to upload images. However, I get this error:
Warning: Unexpected character in input: '' (ASCII=12) state=1 in /usr/local/dh/cgi-system/php.cgi on line 2987
Parse error: parse error, unexpected T_STRING in /usr/local/dh/cgi-system/php.cgi on line 2987
The form itself:
<html>
<head>
<title>web.blazonry : PHP : Upload an Image</title>
<meta name="author" content="Marcus Kazmierczak, marcus@mkaz.com">
<meta name="copyright" content="(c) 2000 mkaz.com">
<meta name="license" content="http://blazonry.com/mklicense.php">
<meta name="origin" content="May 2000">
<?
if ($REQUEST_METHOD == "POST")
{
/* SUBMITTED INFORMATION - use what you need
* temporary filename (pointer): $imgfile
* original filename : $imgfile_name
* size of uploaded file : $imgfile_size
* mime-type of uploaded file : $imgfile_type
*/
/*== upload directory where the file will be stored
relative to where script is run ==*/
$uploaddir = "/images";
/*== get file extension (fn at bottom of script) ==*/
/*== checks to see if image file, if not do not allow upload ==*/
$pext = getFileExtension($imgfile_name);
$pext = strtolower($pext);
if (($pext != "jpg") && ($pext != "jpeg"))
{
print "<h1>ERROR</h1>Image Extension Unknown.
";
print "<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY
";
print "The file you uploaded had the following extension: $pext</p>n";
/*== delete uploaded file ==*/
unlink($imgfile);
exit();
}
/*== setup final file location and name ==*/
/*== change spaces to underscores in filename ==*/
$final_filename = str_replace(" ", "_", $imgfile_name);
$newfile = $uploaddir . "/$final_filename";
/*== do extra security check to prevent malicious abuse==*/
if (is_uploaded_file($imgfile))
{
/*== move file to proper directory ==*/
if (!copy($imgfile,"$newfile"))
{
/*== if an error occurs the file could not
be written, read or possibly does not exist ==*/
print "Error Uploading File.";
exit();
}
}
/*== delete the temporary uploaded file ==*/
unlink($imgfile);
print("<img src="$final_filename">");
/*== DO WHATEVER ELSE YOU WANT
SUCH AS INSERT DATA INTO A DATABASE ==*/
}
?>
</head>
<body bgcolor="#FFFFFF">
<h2>Upload and Resize an Image</H2>
<form action="<?=$SCRIPT_NAME; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="150000">
<p>Upload Image: <input type="file" name="imgfile">
<font size="1">Click browse to upload a local file</font>
<input type="submit" value="Upload Image">
</form>
</body>
</html>
<?
/*== FUNCTIONS ==*/
function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
?>