Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Upload - can't get it to work

    Hi!

    Can someone help me with this script. I went over it 100 times, but I can't seem to find what's wrong with it.

    Everytime I press Submit, the page refreshes, and nothing gets uploaded.

    PHP Code:
    <?php
    $maxsize
    =2621440;
    if (!
    $HTTP_POST_VARS['submit']) {
      
    $error=" ";

    }
    if (!
    is_uploaded_file($_FILES['upload_file']['tmp_name']) AND !isset($error)) {
      
    $error "<b>You must upload a file!</b><br /><br />";
      
    unlink($_FILES['upload_file']['tmp_name']);
    }
    if (
    $maxsize $_FILES['upload_file']['size'] AND !isset($error)) {
      
    $error "<b>Error, file must be less than $maxsize bytes.</b><br /><br />";
      
    unlink($_FILES['upload_file']['tmp_name']);
    }
    if(
    $_FILES['upload_file']['type'] != "image/gif" AND
    $_FILES['upload_file']['type'] != "image/png" AND
    $_FILES['upload_file']['type'] != "image/jpg" AND
    $_FILES['upload_file']['type'] !="image/jpeg" AND !isset($error)) {
      
    $error "<b>You may only upload .gif, .png, or .jpeg files.</b><br /><br />";
      
    unlink($_FILES['upload_file']['tmp_name']);
    }
    if (!isset(
    $error)) {
    move_uploaded_file($_FILES['upload_file']['tmp_name'],"uploads/".$_FILES['upload_file']['name']);
      print 
    "Thank you for your upload.";
      exit;
    }
    else
    {
      echo (
    "$error");
    }
    ?>

    <html>
    <head></head>
    <body>
    <form enctype="multipart/form-data" action="<?php echo( htmlspecialchars($_SERVER['PHP_SELF'])) ?>" method="POST">
    Choose a file to upload:<br />
    <input type="file" name="upload_file" size="80">
    <br />
    <input type="submit" name="submit" value="submit">
    </form>
    </body>
    </html>

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,043
    Thanks
    2
    Thanked 316 Times in 308 Posts
    $HTTP_POST_VARS are depreciated and are off by default. Check your web server log for errors or turn on full php error reporting to get php to help you out. If they are off, then your code sets $error = " "; and all the rest of the code skips their checks. Your code then echos a " " and displays the form again.

    Your code also does not bother to check the ['error'] element for any upload errors before it starts checking the other elements, such as ['tmp_name'], ['size'], ... So, anytime you get a error during the upload, the other tests will fail and give you an error that does not really have anything to do with what the real problem was - http://www.php.net/manual/en/feature...oad.errors.php
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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