#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    2

    [PHP FORM] File Upload


    I am having problems uploading a picture to my site.

    My HTML Form:
    Code:
    <form action="inc/doAddServer.inc.php" enctype="multipart/form-data" method="post">
                            
                            <div class="control-group">
                                <label class="control-label" for="serverImg">Server Image Banner</label>
                                    <div class="controls">
                                        <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
                                        <input name="serverImg" type="file">
                                    </div>
                            </div>
                        </form>
    And if I try to use the other fields (I only included the one for the upload above) I use a is not set function for the serverImg, because then I don't want to use the upload. The problem I am having even though the upload isn't set, it still uses the code that when there is a file there it uses.

    I find it hard to explain.
    PHP Code:

    if(!isset($serverImg)){
        
    $sql=mysql_query("INSERT INTO servers (serverName, serverIP, serverOwner, serverWebsite, serverDesc, serverUptime, serverPlayersOnline, serverPlayersTotal, serverDateAdded) VALUES ('$serverName', '$serverIP', '$serverOwner', '$serverWebsite', '$serverDesc', '$serverUptime', '$serverPlayersOnline', '$serverPlayersTotal', '$today')");
        
    header('Location: ../addServer.php?s='.urlencode('Thank you for adding your server to our database!')); exit;  
    } else {

        
    // Your file name you are uploading 
        
    $file_name $HTTP_POST_FILES['serverImg']['name'];

        
    // random 4 digit to add to our file name 
        // some people use date and time in stead of random digit 
        
    $random_digit=$nextId;

        
    //combine random digit to you file name to create new file name
        //use dot (.) to combile these two variables

        
    $new_file_name=$random_digit;

        
    //set where you want to store files
        //in this example we keep file in folder upload 
        //$new_file_name = new upload file name
        //for example upload file name cartoon.gif . $path will be upload/cartoon.gif
        
    $path"../banners/".$new_file_name;
        if(
    $serverImg != none) {
            if(
    copy($HTTP_POST_FILES['serverImg']['tmp_name'], $path)) {
                
    $sql=mysql_query("INSERT INTO servers (serverName, serverIP, serverOwner, serverWebsite, serverDesc, serverUptime, serverPlayersOnline, serverPlayersTotal, serverDateAdded) VALUES ('$serverName', '$serverIP', '$serverOwner','$serverWebsite', '$serverDesc', '$serverUptime', '$serverPlayersOnline', '$serverPlayersTotal', '$today')");
                
    header('Location: ../addServer.php?s='.urlencode('Thank you for adding your server to our database!')); exit;  
            } else {
                echo 
    "Error";
            }
        } else {
            
    $sql=mysql_query("INSERT INTO servers (serverName, serverIP, serverOwner, serverWebsite, serverDesc, serverUptime, serverPlayersOnline, serverPlayersTotal, serverDateAdded) VALUES ('$serverName', '$serverIP', '$serverOwner', '$serverWebsite', '$serverDesc', '$serverUptime', '$serverPlayersOnline', '$serverPlayersTotal', '$today')");
        
    header('Location: ../addServer.php?s='.urlencode('Thank you for adding your server to our database!')); exit;  
        }

        } 
    Any help would be greatly appreciated!

    Thanks,
    TJ
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    1. It will always be set.
    2. Unless you have an assignment for $serverImg somewhere you haven't shown, the variable does not exist and you need to use the $_FILES array...
    3. $HTTP_POST_FILES died a long time ago. It's now called $_FILES. Same structure, different name.
    4. You aren't using a "is not set function". You are trying to compare it to the constant "none" which doesn't exist.

    To check what happened with the file upload look at the ['error']. It will be one of these constants. UPLOAD_ERR_OK means the image was uploaded file and anything else means the upload didn't happen correctly. Specifically, UPLOAD_ERR_NO_FILE means there wasn't a file to begin with so you might not want to treat that like an actual error.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Burb of Detroit, Michigan
    Posts
    90
    Rep Power
    77
    Here's some snippets of code from my upload picture section, maybe it will help?


    PHP Code:
    if (isset($_POST['submit'])) { 

           
    $allowedExts = array("jpg""jpeg""gif""png");
           
    $basenameAndExtension explode('.'$_FILES["photo"]["name"]);
           
    $extension end($basenameAndExtension);
           
           if (((
    $_FILES["photo"]["type"] == "image/gif")
           || (
    $_FILES["photo"]["type"] == "image/jpeg")
           || (
    $_FILES["photo"]["type"] == "image/png")
           || (
    $_FILES["photo"]["type"] == "image/pjpeg"))
           && (
    $_FILES["photo"]["size"] < 800 1024)
           && 
    in_array($extension$allowedExts))
            {
            if (
    $_FILES["photo"]["error"] > 0
    PHP Code:
     if (file_exists("upload/" $_FILES["photo"]["name"]))
                {
                echo 
    $_FILES["photo"]["name"] . " already exists. ";
                }
              else
                {
                
    move_uploaded_file($_FILES["photo"]["tmp_name"],
                
    "upload/" $_FILES["photo"]["name"]);
                echo 
    "Stored in: " "upload/" $_FILES["photo"]["name"];
                
    $image_name $_FILES["photo"]["name"];
                echo 
    "<br />";
                echo 
    "<img src=\"http://localhost/php_test/greencay_php/upload/" $image_name "\" alt=\"There ya go\" />";
                echo 
    "<br />";
                } 
    Just wanted to add I didn't sanitize my code yet
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    2
    Originally Posted by requinix
    1. It will always be set.
    2. Unless you have an assignment for $serverImg somewhere you haven't shown, the variable does not exist and you need to use the $_FILES array...
    3. $HTTP_POST_FILES died a long time ago. It's now called $_FILES. Same structure, different name.
    4. You aren't using a "is not set function". You are trying to compare it to the constant "none" which doesn't exist.

    To check what happened with the file upload look at the ['error']. It will be one of these constants. UPLOAD_ERR_OK means the image was uploaded file and anything else means the upload didn't happen correctly. Specifically, UPLOAD_ERR_NO_FILE means there wasn't a file to begin with so you might not want to treat that like an actual error.
    Okay so, how would I use the UPLOAD_ERR_NO_FILE here?
    PHP Code:
    if($serverImg != none) { 
            if(
    copy($HTTP_POST_FILES['serverImg']['tmp_name'], $path)) { 
                
    $sql=mysql_query("INSERT INTO servers (serverName, serverIP, serverOwner, serverWebsite, serverDesc, serverUptime, serverPlayersOnline, serverPlayersTotal, serverDateAdded) VALUES ('$serverName', '$serverIP', '$serverOwner','$serverWebsite', '$serverDesc', '$serverUptime', '$serverPlayersOnline', '$serverPlayersTotal', '$today')"); 
                
    header('Location: ../addServer.php?s='.urlencode('Thank you for adding your server to our database!')); exit;   
            } else { 
                echo 
    "Error"
            } 
        } else { 
            
    $sql=mysql_query("INSERT INTO servers (serverName, serverIP, serverOwner, serverWebsite, serverDesc, serverUptime, serverPlayersOnline, serverPlayersTotal, serverDateAdded) VALUES ('$serverName', '$serverIP', '$serverOwner', '$serverWebsite', '$serverDesc', '$serverUptime', '$serverPlayersOnline', '$serverPlayersTotal', '$today')"); 
        
    header('Location: ../addServer.php?s='.urlencode('Thank you for adding your server to our database!')); exit;   
        } 
    Would it be this first line?
    PHP Code:
    if($serverImg != none) { 
    So like this?
    PHP Code:
    if(UPLOAD_ERR_NO_FILE !== 4){ // Because -UPLOAD_ERR_NO_FILE - Value: 4; No file was uploaded. 
    Thanks.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    2
    Originally Posted by Strider64
    Here's some snippets of code from my upload picture section, maybe it will help?


    PHP Code:
    if (isset($_POST['submit'])) { 

           
    $allowedExts = array("jpg""jpeg""gif""png");
           
    $basenameAndExtension explode('.'$_FILES["photo"]["name"]);
           
    $extension end($basenameAndExtension);
           
           if (((
    $_FILES["photo"]["type"] == "image/gif")
           || (
    $_FILES["photo"]["type"] == "image/jpeg")
           || (
    $_FILES["photo"]["type"] == "image/png")
           || (
    $_FILES["photo"]["type"] == "image/pjpeg"))
           && (
    $_FILES["photo"]["size"] < 800 1024)
           && 
    in_array($extension$allowedExts))
            {
            if (
    $_FILES["photo"]["error"] > 0
    PHP Code:
     if (file_exists("upload/" $_FILES["photo"]["name"]))
                {
                echo 
    $_FILES["photo"]["name"] . " already exists. ";
                }
              else
                {
                
    move_uploaded_file($_FILES["photo"]["tmp_name"],
                
    "upload/" $_FILES["photo"]["name"]);
                echo 
    "Stored in: " "upload/" $_FILES["photo"]["name"];
                
    $image_name $_FILES["photo"]["name"];
                echo 
    "<br />";
                echo 
    "<img src=\"http://localhost/php_test/greencay_php/upload/" $image_name "\" alt=\"There ya go\" />";
                echo 
    "<br />";
                } 
    Just wanted to add I didn't sanitize my code yet
    Thanks, I will look at this after I learn mine... I'm trying to learn PHP. But if I can't get it, I will use this!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    2
    @ Strider64

    Thanks. I ended up using your code! It works like a charm. Thanks

IMN logo majestic logo threadwatch logo seochat tools logo