I am trying to use the form below to submit 4 images to a MySQL database. When I click the "Upload Image" button, an error saying syntax error, unexpected $end in ...postsubmit.php on line 235
appears. Line 235 has nothing on it; it's just the last line on postsubmit.php. Any idea why I'm getting this error?
Thanks in advance,
John
The form:
echo '<form method="post" action="postsubmit.php" enctype="multipart/form-data">
<input type="hidden" value="'.$_SESSION['loginid'].'" name="uid">
<div class="imagetitle"><label for="image">Image 1:</label></div>
<div class="imagefield"><input type="file" name="image" /></div>
<div class="imagetitle2"><label for="image2">Image 2:</label></div>
<div class="imagefield2"><input type="file" name="image2" /></div>
<div class="imagetitle3"><label for="image3">Image 3:</label></div>
<div class="imagefield3"><input type="file" name="image3" /></div>
<div class="imagetitle4"><label for="image4">Image 4:</label></div>
<div class="imagefield4"><input type="file" name="image4" /></div>
<div class="submissionbutton"><input type="submit" value="Upload Image" /></div>
</form>';
On postsubmit.php:
function assertValidUpload($code)
{
if ($code == UPLOAD_ERR_OK) {
return;
}
switch ($code) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$msg = 'Image is too large';
break;
case UPLOAD_ERR_PARTIAL:
$msg = 'Image was only partially uploaded';
break;
case UPLOAD_ERR_NO_FILE:
$msg = 'No image was uploaded';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$msg = 'Upload folder not found';
break;
case UPLOAD_ERR_CANT_WRITE:
$msg = 'Unable to write uploaded file';
break;
case UPLOAD_ERR_EXTENSION:
$msg = 'Upload failed due to extension';
break;
default:
$msg = 'Unknown error';
}
throw new Exception($msg);
}
$errors = array();
try {
if (!array_key_exists('image', $_FILES)) {
throw new Exception('Image not found in uploaded data');
}
$image = $_FILES['image'];
// ensure the file was successfully uploaded
assertValidUpload($image['error']);
if (!is_uploaded_file($image['tmp_name'])) {
throw new Exception('File is not an uploaded file');
}
$info = getImageSize($image['tmp_name']);
if (!$info) {
throw new Exception('File is not an image');
}
}
catch (Exception $ex) {
$errors[] = $ex->getMessage();
}
if (count($errors) == 0) {
// no errors, so insert the image
$query = sprintf(
"insert into images (filename, mime_type, file_size, file_data)
values ('%s', '%s', %d, '%s')",
mysql_real_escape_string($image['name']),
mysql_real_escape_string($info['mime']),
$image['size'],
mysql_real_escape_string(
file_get_contents($image['tmp_name'])
)
);
mysql_query($query);
try {
if (!array_key_exists('image2', $_FILES)) {
throw new Exception('Image not found in uploaded data');
}
$image2 = $_FILES['image2'];
// ensure the file was successfully uploaded
assertValidUpload($image2['error']);
if (!is_uploaded_file($image2['tmp_name'])) {
throw new Exception('File is not an uploaded file');
}
$info2 = getImageSize($image2['tmp_name']);
if (!$info2) {
throw new Exception('File is not an image');
}
}
catch (Exception $ex2) {
$errors2[] = $ex2->getMessage();
}
if (count($errors2) == 0) {
// no errors, so insert the image
$query2 = sprintf(
"insert into images (filename, mime_type, file_size, file_data)
values ('%s', '%s', %d, '%s')",
mysql_real_escape_string($image2['name']),
mysql_real_escape_string($info2['mime']),
$image2['size'],
mysql_re开发者_如何学运维al_escape_string(
file_get_contents($image2['tmp_name'])
)
);
mysql_query($query2);
try {
if (!array_key_exists('image3', $_FILES)) {
throw new Exception('Image not found in uploaded data');
}
$image3 = $_FILES['image3'];
// ensure the file was successfully uploaded
assertValidUpload($image3['error']);
if (!is_uploaded_file($image3['tmp_name'])) {
throw new Exception('File is not an uploaded file');
}
$info3 = getImageSize($image3['tmp_name']);
if (!$info3) {
throw new Exception('File is not an image');
}
}
catch (Exception $ex3) {
$errors3[] = $ex3->getMessage();
}
if (count($errors3) == 0) {
// no errors, so insert the image
$query3 = sprintf(
"insert into images (filename, mime_type, file_size, file_data)
values ('%s', '%s', %d, '%s')",
mysql_real_escape_string($image3['name']),
mysql_real_escape_string($info3['mime']),
$image3['size'],
mysql_real_escape_string(
file_get_contents($image3['tmp_name'])
)
);
mysql_query($query3);
try {
if (!array_key_exists('image4', $_FILES)) {
throw new Exception('Image not found in uploaded data');
}
$image4 = $_FILES['image4'];
// ensure the file was successfully uploaded
assertValidUpload($image4['error']);
if (!is_uploaded_file($image4['tmp_name'])) {
throw new Exception('File is not an uploaded file');
}
$info4 = getImageSize($image4['tmp_name']);
if (!$info4) {
throw new Exception('File is not an image');
}
}
catch (Exception $ex4) {
$errors4[] = $ex4->getMessage();
}
if (count($errors4) == 0) {
// no errors, so insert the image
$query4 = sprintf(
"insert into images (filename, mime_type, file_size, file_data)
values ('%s', '%s', %d, '%s')",
mysql_real_escape_string($image4['name']),
mysql_real_escape_string($info4['mime']),
$image4['size'],
mysql_real_escape_string(
file_get_contents($image4['tmp_name'])
)
);
mysql_query($query4);
header("Location: http://www...com/.../");
exit;
}
Typically that error is from the PHP parser reaching the end of the script before expected syntax is found. More than likely an unclosed curly brace }
.
add one more closing brace }
at the very end of your file.
you never closed your assertValidUpload()
function.
you generally should avoid writing such lengthy functions. try breaking it up into multiple functions. --as a general rule, a function should accomplish only one task.
also, your code style is inconsistent ( and it might just be the formatting on this site ):
at the beginning of your assertValidUpload()
function you put the opening brace on the next line, you should move this to the same line as the function definition declaration. these sorts of things help inconsistencies in your code stand out--consequently helping you notice errors like this.
精彩评论