-1

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!

Gastón Vidal
  • 127
  • 2
  • 11
  • Seems to be more likely a permission issue regarding creating files in the server's filesystem – Hiren Sep 18 '16 at 01:08
  • And is there a way to give permission in ubuntu server? – Gastón Vidal Sep 18 '16 at 01:24
  • Depends on the type of hosting which you have with your host (Cloudatcost). Some hosts offer a file manager to allow you to set permissions, some allow you to have restricted shell access, and so on. – Hiren Sep 18 '16 at 01:26

3 Answers3

0

1.Check if the row inserted to the database. if it didn't insert, you did something wrong. like wrong query (for example field name or,...) or connecting problem. if you have not database problem go to #2.

2.check if your folder that you are trying to upload has proper permission by:

ls -l

if it has not proper permission. read this: https://help.ubuntu.com/community/FilePermissions

3.If it has no problem with database and permission, it means you are trying to insert to the wrong folder. try it with dirname(__FILE__). it will get your current file directory. for example, change this line:

move_uploaded_file($imagen['tmp_name'], "../../img/journeys/".$ID.".jpg");

to the

move_uploaded_file($imagen['tmp_name'], dirname(__FILE__)."/../../img/journeys/".$ID.".jpg");

For Checking Log

go to this file

sudo nano /var/log/apache2/error.log

if you couldn't find log file there, go to this file to find where is your log file:

 sudo nano /etc/php_version/apache2/php.ini

*replace php_version with proper folder

ICE
  • 1,667
  • 2
  • 21
  • 43
  • The row is correctly being inserted, the only thing is that the images are not being uploaded. By ls -1 you mean to execute that command in the server console? – Gastón Vidal Sep 18 '16 at 20:29
  • I've checked the permissions using ls -l ../path/myproyect and my images folder returned drwxr-xr-x 7 root root 4096 and some date. – Gastón Vidal Sep 18 '16 at 20:52
  • 1
    Solved it! Thanks @james using the error.log file helped a lot! – Gastón Vidal Sep 18 '16 at 21:47
  • Glad it could help. You had Problem #2. If you want to know how to read permissions read this: http://linuxcommand.org/lts0070.php. – ICE Sep 18 '16 at 23:17
0

In order to retrieve the Last inserted ID as per the Question you have made you have made is is compulsory that you have run the

  1. Executed Statement
  2. Queried Statement

after Insertion in-order to get the last inserted ID in mysqli.* or in PDO top.

In your code you have to change of how to get the last inserted ID since you have not run the query after Execution.

Replace:

$ID = $mysqli->insert_id;

With:

$ID = $stmt->insert_id;

Note: Since you have executed the query using $stmt alone and not with the $mysqli you need to change it to as i have mentioned.

Below are the Example of how to get the last inserted ID based on Mysqli and PDO

Scenario 1: (Mysqli)

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if (mysqli_query($conn, $sql)) {
    $last_id = mysqli_insert_id($conn);
    echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);

Here you have run the query after the query is being executed.

Scenario 2: (PDO)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', 'john@example.com')";
    // use exec() because no results are returned
    $conn->exec($sql);
    $last_id = $conn->lastInsertId();
    echo "New record created successfully. Last inserted ID is: " . $last_id;
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }
$conn = null;
?> 
Naresh Kumar P
  • 4,127
  • 2
  • 16
  • 33
0

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).

Community
  • 1
  • 1
Gastón Vidal
  • 127
  • 2
  • 11