Search code examples
phpmysqliubuntu-server

Insert_id not working on web uploaded in server


I've hired Cloudatcost, and I've configured an Ubuntu server, installed LAMP and uploaded my web page. I have a section where I upload some text fields and an an image, the problem is that the image is not being uploaded, but when I run my page locally it works.

My insert code goes like this:

    function insert($title, $intro, $body, $data ,$date, $someid, $Myimage, $somesection){

    $ID = null;
    $mysqli = openConnection(); <- starts connection
    $query = "INSERT INTO columnsa (title, intro, body, data, date, someid) VALUES (?, ?, ?, ?, ?, ?)"; 
    if ($stmt = $mysqli->prepare($query))
    {
        $stmt->bind_param(

'sssssi', $title, $intro, $body, $data, $date, $someid);        
        /* Execution*/
        $stmt->execute();

        $ID = $mysqli->insert_id;
        /* Close query */
        $stmt->close();
    }
    if($ID)
    {
        if ($image != null) {
            insertImg($image, $ID, $section);
            closeConnection($mysqli);   
            return true;
        }
    }
    else
    {
        closeConnection($mysqli);
        return false;
    }
}

And my Insert image is:

    function insertImg($image, $ID, $section)
    {
        switch ($seccion) {
            case "journey":
                move_uploaded_file($imagen['tmp_name'], "../../img/journeys/".$ID.".jpg");
                break;
            case "column":
                move_uploaded_file($imagen['tmp_name'], "../../img/bolumns/".$ID.".jpg");
                break;
        case "blog":
            move_uploaded_file($imagen['tmp_name'], "../../img/blogs/".$ID.".jpg");
            break;
    }
}

I'm guessing that maybe I forget to install an php5 module, because the lines

$ID = $mysqli->insert_id;
        /* Close query */
        $stmt->close();
    }
    if($ID)
    {
        if ($image != null) {
            insertImg($image, $ID, $section);
            closeConnection($mysqli);   
            return true;
        }
    }
    else
    {
        closeConnection($mysqli);
        return false;
    }

Don't seem to work. Any Idea which php5 module includes insert_id? Thanks!


Solution

  • I've solved it by assigning the proper permissions. As this questions says

    Make sure all files are owned by the Apache group and user. In Ubuntu it is the www-data group and user

    chown -R www-data:www-data /path/to/webserver/www
    

    Next enabled all members of the www-data group to read and write files

    chmod -R g+rw /path/to/webserver/www
    

    I gave read and write permissions to my Images folder outside my web page's folder (due to good security practices).