PHP Upload Form - PLEASE HELP - It doesn't work!?

Discussion in 'PHP' started by EGS, Dec 9, 2005.

  1. #1
    So I tried to code a PHP upload form to upload some images into a folder on my site, but for SOME flippin' reason, it doesn't work! :mad:

    Please, somebody...tell me what I'm doing wrong!
    <?php
    $path = "/home/bgamer/public_html/media";
    $url = "http://www.boldgamers.com/media";
    
    if (!$_POST['file']) {
    echo '<form method="post" action="upload.php" enctype="multipart/form-data"><input type="file" name="file"> <input type="submit" value="Upload!"></form>';
    }
    
    if ($_POST['file']) {
    $copy = copy ($_FILES["file"]["tmp_name"], $path."/".$_FILES["file"]["name"]);
    
    if ($copy == TRUE) {
    echo "<a href='".$url."/".$_POST["file"]."'>View your file!</a>";
    } else {
    echo "Nope didnt go through; go back and try again!";
    }
    }
    ?>
    
    Code (markup):
    Note: That IS upload.php also ;)
     
    EGS, Dec 9, 2005 IP
  2. HN Will

    HN Will Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use the move_uploaded_file function

    here's the code I normally use for uploading

    
    <?php
    $name = $_FILES['the_file']['name'];
    if (!$name) die('Sorry, an error has occured please try again');
    $name = preg_replace("/\s/","_",$name);//make sure there are no spaces
    $size = $_FILES['the_file']['size'];
    $tmp_name = $_FILES['the_file']['tmp_name'];
    if (move_uploaded_file($tmp_name, "files/$name"))
    {
    	echo "Upload Successful";
    }
    else
    {
    echo "Sorry, an error has occured please try again";
    
    }
    
    
    
    ?>
    
    Code (markup):
     
    HN Will, Dec 10, 2005 IP
  3. HN Will

    HN Will Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ps you can use the size to check for filesize - but as it stands it does nothing in the above code
     
    HN Will, Dec 10, 2005 IP
  4. anton-io!

    anton-io! Active Member

    Messages:
    540
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #4
    is your upload folder CHMOD'ed properly?

    Permissions set?
     
    anton-io!, Dec 10, 2005 IP
  5. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #5
    Well with my code no because for some reason the coding I'm using now causes an internal server error when I try it out with the directory CHMOD'ed.. :|

    I'm going be trying this guy's coding later tonight when I get off of work.
    Thanks!!! ^_^
     
    EGS, Dec 10, 2005 IP
  6. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #6
    Ok so I tried this code but it has no upload field...so it says Sorry, an error has occured please try again. :|

    Also, is there a way it can make a link to the image you just uploaded? Also, could it add numbers to the file name that it saves into the directory? Like it changes MarioBrosPic.jpeg to MariosBrosPic0101010101010101289273.jpeg?

    Thanks. ^_^
     
    EGS, Dec 10, 2005 IP
  7. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #7
    Success! I got it. Thanks all! ^_^
     
    EGS, Dec 10, 2005 IP