SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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 Code:
    <?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!
    Steve Husting

  2. #2
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,267
    Mentioned
    116 Post(s)
    Tagged
    1 Thread(s)
    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.
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,151
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    To me it looks like this line:
    PHP Code:
    $error = !in_array($detected_type$allowed_types); 
    Should be in an IF statement
    PHP Code:
    if (!in_array($detected_type$allowed_types)) { 
    But that is just from a quick look

  4. #4
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DaveMaxwell View Post
    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.
    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:

    Code:
    var now = new Date(); 
          $new_image_name = now + ".jpg";
    Should be:
    Code:
    $timestamp = time();
    $new_image_name = $timestamp . ".jpg";
    Steve Husting

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    To me it looks like this line:
    PHP Code:
    $error = !in_array($detected_type$allowed_types); 
    Should be in an IF statement
    PHP Code:
    if (!in_array($detected_type$allowed_types)) { 
    But that is just from a quick look
    You are right! I found the original in my notes:

    Code:
    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 {
    Steve Husting

  6. #6
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,410
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    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.

  7. #7
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rubble View Post
    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:
    http://www.sitepoint.com/forums/show...d+image+script

    Thanks!
    Steve Husting

  8. #8
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,410
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    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.

  9. #9
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I am getting these errors in the server log:

    Code:
    [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:

    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("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";
      }
    ?>
    Steve Husting

  10. #10
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    My goof. I copied the OP code instead of the updated code:

    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);
    ?>
    Steve Husting

  11. #11
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Consider this closed. I think I got it resolved.
    Steve Husting


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •