How to handle an uploaded file without using separate script file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LELE7
    New Member
    • Jan 2010
    • 14

    How to handle an uploaded file without using separate script file

    I'm new to PHP, but I usually program Windows Apps in .NET(c#, VB). So please excuse my unfamiliarity with code and HTML combined.

    It seems pretty simple to upload a file to a website using the following html:

    Code:
    <form method="POST" enctype="multipart/form-data" name="image_upload_form" action="">
    <p><input type="file" name="image_file" size="20"></p>
    <p><input type="submit" value="Upload Image" name="action"></p>
    </form>
    I'm dealing with a website that the standard is not to use separate script files, but to code in the same file as the html. Most examples on the web for this use separate script files. I found a few examples that do not work. What am I missing in my code to somehow connect the uploaded file in the html to my code on top?

    My code with html:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/template.dwt.php" codeOutsideHTMLIsLocked="false" -->
    
    <head>
    <script type="text/JavaScript">
    
    <? php
    
    define ('MAX_FILE_SIZE', 1024 * 50);
    
    if (array_key_exists('upload', $_POST)) {
      // replace any spaces in original filename with underscores
      $file = str_replace(' ', '_', $_FILES['image']['name']);
      // create an array of permitted MIME types
      $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg',
    'image/png');
      
      // upload if file is OK
      if (in_array($_FILES['image']['type'], $permitted)
          && $_FILES['image']['size'] > 0 
          && $_FILES['image']['size'] <= MAX_FILE_SIZE) 
        {
    	  $result= "$file file uploaded!";
        }
      else {
        $result = "$file is either too big or not an image.";
      }
      
      
    }
    ?>
    
    
    
    </script>
    </head>
    <body>
    
    <?php
    // if the form has been submitted, display result
    if (isset($result)) {
      echo "<p><strong>$result</strong></p>";
    }
    ?>
    
    <form method="POST" enctype="multipart/form-data" name="image_upload_form" action="">
    <p><input type="file" name="image_file" size="20"></p>
    <p><input type="submit" value="Upload Image" name="action"></p>
    </form>
    
    </body>
    </html>

    Thanks!
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    I think what you are trying to do is get the same page to submit itself.
    You do this with
    Code:
    action="$_SERVER['PHP_SELF']"

    Comment

    • LELE7
      New Member
      • Jan 2010
      • 14

      #3
      I'm getting the following error:
      Firefox can't find the file at
      /C:/WebSites/STUNNI~1/$_SERVER['PHP_SELF'], the root directory being the root of my page. Why does it think it's a separate file? Am I missing something important in my code on top?

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        I think your HTML is wrong. I would need to see it.
        But $_SERVER['PHP_SELF'] returns
        The filename of the currently executing script, relative to the document root.
        For instance, $_SERVER['PHP_SELF'] in a script at the address
        http://example.com/test.php/foo.bar would be /test.php/foo.bar.

        Comment

        • HaLo2FrEeEk
          Contributor
          • Feb 2007
          • 404

          #5
          If it's in HTML, then he needs to do:

          action="<?php echo $_SERVER['PHP_SELF']; ?>"

          That should work.

          Comment

          • LELE7
            New Member
            • Jan 2010
            • 14

            #6
            In the end, I think the problem was that I wasn't running it from the proper place- from my localhost (or my WAMP/www directory. I was just running it from my regular file directory. Not sure if this makes sense, but I'm not getting the error anymore. Thanks for your help!

            Comment

            Working...