Jump to content

Help with Parse error: syntax error, unexpected T_ELSE


Aaron4osu

Recommended Posts

I'm working on a script that take information from a form and enters it into a mysql database.  Everything was working except that the way it is written you must select an image to upload to your profile.  I wanted to make the image upload part optional so I added an if/else to the script.

 

If (user supplies image)

{

      process image

}

else {

 

    use missing image icon

}

 

  I'm getting the following error :Parse error: syntax error, unexpected T_ELSE ... on line 70 (which is where I have my else statement.  From reading the forums it sounds like I'm missing a curly brace, but there I think I have the braces right.

 

Here is the part of the code described.  Line 70 is :else {

 

<?php
// if user provided image
if (!empty($_FILES[$image_fieldname]))
{
    // Make sure we didn't have an error uploading the image
    ($_FILES[$image_fieldname]['error'] == 0)
      or handle_error("the server couldn't upload the image you selected.",
                      $php_errors[$_FILES[$image_fieldname]['error']]);

    // Is this file the result of a valid upload?
    // The @ supresses PHP's errorfor this and uses our custom version instead.
    @is_uploaded_file($_FILES[$image_fieldname]['tmp_name'])
      or handle_error("you were trying to do something naughty. Shame on you!",
                      "Uploaded request: file named '{$_FILES[$image_fieldname]['tmp_name']}'");

    // Is this actually an image?
    @getimagesize($_FILES[$image_fieldname]['tmp_name'])
      or handle_error("you selected a file for your picture that isn't an image.",
                      "{$_FILES[$image_fieldname]['tmp_name']} isn't a valid image file.");

    // Name the file uniquely
    $now = time();
    while (file_exists($upload_filename = $upload_dir . $now .
                                         '-' .
                                         $_FILES[$image_fieldname]['name'])) {
        $now++;

    // Finally, move the file to its permanent location
    @move_uploaded_file($_FILES[$image_fieldname]['tmp_name'], $upload_filename)
      or handle_error("we had a problem saving your image to its permanent location.",
                      "permissions or related error moving file to {$upload_filename}");

} // end if user provided image
else {
      // set file name equal to missing picture icon
      $upload_filename = "uploads/profile_pics/missing_user.png";
}

?>

 

I also inclused the entire file if you need to see that.

 

<?php

require_once 'scripts/app_config.php';
require_once 'scripts/database_connection.php';

$upload_dir = SITE_ROOT . "uploads/profile_pics/";
$image_fieldname = "user_pic";

// Potential PHP upload errors
$php_errors = array(1 => 'Maximum file size in php.ini exceeded',
                    2 => 'Maximum file size in HTML form exceeded',
                    3 => 'Only part of the file was uploaded',
                    4 => 'No file was selected to upload.');

$first_name = trim($_REQUEST['first_name']);
$last_name = trim($_REQUEST['last_name']);
$username = trim($_REQUEST['username']);
$password = trim($_REQUEST['password']);
$email = trim($_REQUEST['email']);
$bio = trim($_REQUEST['bio']);
$facebook_url = str_replace("facebook.org", "facebook.com", trim($_REQUEST['facebook_url']));
$position = strpos($facebook_url, "facebook.com");
if ($position === false) {
  $facebook_url = "http://www.facebook.com/" . $facebook_url;
}

$twitter_handle = trim($_REQUEST['twitter_handle']);
$twitter_url = "http://www.twitter.com/";
$position = strpos($twitter_handle, "@");
if ($position === false) {
  $twitter_url = $twitter_url . $twitter_handle;
} else {
  $twitter_url = $twitter_url . substr($twitter_handle, $position + 1);
}

  // if user provided image
if (!empty($_FILES[$image_fieldname]))
{
    // Make sure we didn't have an error uploading the image
    ($_FILES[$image_fieldname]['error'] == 0)
      or handle_error("the server couldn't upload the image you selected.",
                      $php_errors[$_FILES[$image_fieldname]['error']]);

    // Is this file the result of a valid upload?
    // The @ supresses PHP's errorfor this and uses our custom version instead.
    @is_uploaded_file($_FILES[$image_fieldname]['tmp_name'])
      or handle_error("you were trying to do something naughty. Shame on you!",
                      "Uploaded request: file named '{$_FILES[$image_fieldname]['tmp_name']}'");

    // Is this actually an image?
    @getimagesize($_FILES[$image_fieldname]['tmp_name'])
      or handle_error("you selected a file for your picture that isn't an image.",
                      "{$_FILES[$image_fieldname]['tmp_name']} isn't a valid image file.");

    // Name the file uniquely
    $now = time();
    while (file_exists($upload_filename = $upload_dir . $now .
                                         '-' .
                                         $_FILES[$image_fieldname]['name'])) {
        $now++;

    // Finally, move the file to its permanent location
    @move_uploaded_file($_FILES[$image_fieldname]['tmp_name'], $upload_filename)
      or handle_error("we had a problem saving your image to its permanent location.",
                      "permissions or related error moving file to {$upload_filename}");

} // end if user provided image
else {
      // set file name equal to missing picture icon
      $upload_filename = "uploads/profile_pics/missing_user.png";
}


$insert_sql = sprintf("INSERT INTO users " .
                             "(first_name, last_name, username, " .
                              "password, email, " .
                              "bio, facebook_url, twitter_handle, " .
                              "user_pic_path) " .
                      "VALUES ('%s', '%s', '%s', '%s', '%s', 
                               '%s', '%s', '%s', '%s');",
                      mysql_real_escape_string($first_name),
                      mysql_real_escape_string($last_name),
                      mysql_real_escape_string($username),
                      mysql_real_escape_string(crypt($password, $username)),
                      mysql_real_escape_string($email),
                      mysql_real_escape_string($bio),
                      mysql_real_escape_string($facebook_url),
                      mysql_real_escape_string($twitter_handle),
                      mysql_real_escape_string($upload_filename));

// Insert the user into the database
mysql_query($insert_sql) or handle_error(mysql_error());

// Redirect the user to the page that displays user information
header("Location: show_user.php?user_id=" . mysql_insert_id());
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.