Need to bracket this correctly

I have the following script, which is the first part of a page that receives image uploads. However, it is missing the opening curly brace and I don’t know where to put it so it is correct syntax. (The closing curly brace ends just before “else.”) Where do I put the first curly brace?Evidently I’m not beginning the script correctly.

<?php
print("From PHP: This is an upload page.");
define("UPLOAD_DIR", $_SERVER['DOCUMENT_ROOT'] . "/uploads/");

print_r($_FILES);

$allowed_types = array(IMAGETYPE_JPEG);
$detected_type = exif_imagetype($_FILES['file']['name']);
$error = !in_array($detected_type, $allowed_types);
error_log("Wrong file type uploaded.");

  }
  else {

error_log("Upload: " . $_FILES["file"]["name"] . " ... Type: " . $_FILES["file"]["type"] . " ... Size: " . ($_FILES["file"]["size"] / 1024) . "kB ... Temp file: " . $_FILES["file"]["tmp_name"]);

    if (file_exists(UPLOAD_DIR . $_FILES["file"]["name"])) {
		error_log($_FILES["file"]["name"] . " already exists.");
    }
    else {
      var now = new Date();
      $new_image_name = now + ".jpg";
      move_uploaded_file($_FILES["file"]["tmp_name"], UPLOAD_DIR . $new_image_name);
    }
  }
}
else {
	echo "Invalid file";
}
?>

Thanks!

where’s the if that starts it? Do you have all the code for sure?

Is this file perhaps included and there’s another before it which may have the proper start of the if statement

though if there is, I personally would hunt the developer of that codebase down, tie him/her to a set of railroad tracks, and perform some sort of horrendous torture (wet noodle slapping?) for coming up with such a bad design.

To me it looks like this line:

$error = !in_array($detected_type, $allowed_types); 

Should be in an IF statement

if (!in_array($detected_type, $allowed_types)) {

But that is just from a quick look

This is all the code I have. I don’t remember where I got it from; it’s all a jumble of bits and pieces put together.

I see another problem. The following in JavaScript in a php file:

var now = new Date(); 
      $new_image_name = now + ".jpg"; 

Should be:

$timestamp = time();
$new_image_name = $timestamp . ".jpg";

You are right! I found the original in my notes:

define("UPLOAD_DIR", "/srv/www/uploads/");

print_r($_FILES);

$allowed_exts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowed_exts)) {
  if ($_FILES["file"]["error"] > 0) { ?>
document.getElementById('camera_status').innerHTML = "Return Code: "
<?php . $_FILES["file"]["error"] . ?>
document.getElementById('camera_status').innerHTML = "<br/>";
<?php
  }
  else {

This is all the code I have. I don’t remember where I got it from; it’s all a jumble of bits and pieces put together

If this is the case why not look for some better code? There are probably hundreds of working examples on the web and a few on this forum.

OK, I searched and will try this one. It’s pretty similar to mine:

Thanks!

OK, I searched and will try this one. It’s pretty similar to mine:

That’s the best way I think Steve; if you had to use that code then you could sort it out otherwise there is usually something else as good around.

I am getting these errors in the server log:

[04-Mar-2014 16:11:53 America/Denver] PHP Warning:  move_uploaded_file(uploads/1393974863673.jpg): failed to open stream: No such file or directory in /home2/stevehus/public_html/uploads/upload.php on line 35

[04-Mar-2014 16:11:53 America/Denver] PHP Warning:  move_uploaded_file(): Unable to move '/var/tmp/php0vy3Rj' to 'uploads/1393974863673.jpg' in /home2/stevehus/public_html/uploads/upload.php on line 35

The URLs are actually correct, so I don’t understand the problem; files and folders are as shown in the errors.

My php.ini changes:

  1. file_uploads must be set to on:
    file_uploads = On
  2. upload_tmp_dir value must be set to where uploads will be placed:
    upload_tmp_dir = var/tmp/
  3. upload_max_filesize dictates how large a file can be sent
    upload_max_filesize = 10M

File is currently:

<?php
// File types allowed on upload:
$allowedExts = array("jpg", "jpeg", "gif", "png");
 
$extension = end(explode(".", $_FILES["file"]["name"]));
// Examine if file is indeed an image:
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  { 
  if ($_FILES["file"]["error"] > 0) // if file is not an allowed image type, show error:
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else // if file is valid, upload to tmp folder and give information about the file:
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp filename: " . $_FILES["file"]["tmp_name"] . "<br>";
 
/*
CUSTOMIZE: THE NAME OF THE FOLDER THAT THIS PHP UPLOAD SCRIPT RESIDES IN.
*/
      if (file_exists("home2/stevehus/public_html/uploads/" . $_FILES["file"]["name"])) 
      { // If file exists with that name:
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else // if file name is unique, move from tmp folder to named folder
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);
/*
CUSTOMIZE: THE NAME OF THE FOLDER THAT THIS PHP UPLOAD SCRIPT RESIDES IN.
*/
      echo "Stored in: " . "home2/stevehus/public_html/uploads/" . $_FILES["file"]["name"];
      }
    }
  }
else
  { 
  echo "Invalid file";
  }
?>

My goof. I copied the OP code instead of the updated code:

<?php
// File types allowed on upload:
$allowedExts = array("jpg", "jpeg", "gif", "png");
 
$extension = end(explode(".", $_FILES["file"]["name"]));
// Examine if file is indeed an image:
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  { 
  if ($_FILES["file"]["error"] > 0) // if file is not an allowed image type, show error:
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else // if file is valid, upload to tmp folder and give information about the file:
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp filename: " . $_FILES["file"]["tmp_name"] . "<br>";
 
/*
CUSTOMIZE: THE NAME OF THE FOLDER THAT THIS PHP UPLOAD SCRIPT RESIDES IN.
*/
      if (file_exists("/uploads/" . $_FILES["file"]["name"])) 
      { // If file exists with that name:
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else // if file name is unique, move from tmp folder to named folder
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);
/*
CUSTOMIZE: THE NAME OF THE FOLDER THAT THIS PHP UPLOAD SCRIPT RESIDES IN.
home2/stevehus/public_html/uploads/
*/
      echo "Stored in: " . "/uploads/" . $_FILES["file"]["name"];
      }
    }
  }
else
  { 
  echo "Invalid file";
  }
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
		<title>Upload page</title>
		<style type="text/css">
		ul { margin:1em; }
		li { font-size: 1.2em; border:1px; height: 45px; }
		</style>
	</head>
<body>
	<h2>Contents of the image file upload folder.</h2> 
<?php
print_r($_FILES);
?>

Consider this closed. I think I got it resolved.