Error saving directory image with PHP

2

I made a page that administers a website that is responsible for including new lawyers in a 'bootstrap carousel'. On my machine it works perfectly, but on the server it is impossible to save the image that is loaded.

I will explain: The image is not saved to the database with BLOB or anything like it. The image is saved to the directory inside the server, and the form data is stored in the database. The reference that is saved in the bank is the physical path of the image, such as "../../ images-lawyers / image_001.jpg".

I suspect it is a directory permission problem. I have a droplet on DigitalOcean, which uses Linux Ubuntu 16.04.3 LTS. I run PHP on an apache2 server and the database is MySql.

Below, the code I use for the form data and return the success / error message:

session_start();
if (!$_SESSION['USER']) {
    header('Location: index.php?erro=3');
    $id = session_id();
}

require_once('../classes/DAO.php');
require_once("../config.php");

$name = $_POST['name'];
$age = $_POST['age'];
$description = $_POST['description'];
$atuation = $_POST['atuation'];
$experience = $_POST['experience'];
$graduacao = $_POST['graduacao'];
$language = $_POST['language'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$cel = $_POST['cel'];

$upload_foto = false;

if (isset($_FILES['image']['name']) && $_FILES['image']['error'] == 0) {
    $arquivo_tmp = $_FILES['image']['tmp_name'];
    $nome = $_FILES['image']['name'];

    $extensao = pathinfo($nome, PATHINFO_EXTENSION);

    if (strstr('.jpg;.jpeg;.gif;.png', $extensao)) {
        $novoNome = uniqid(time()) . '.' . $extensao;
        $destino = '../../imagens-advogados/' . $novoNome;

        if (@move_uploaded_file($arquivo_tmp, $destino)) {
            $upload_foto = true;
        } else {
            header('Location: ../admin_advogados.php?erro=6');
        }
    } else {
        header('Location: ../admin_advogados.php?erro=5');
    }
} else {
    header('Location: ../admin_advogados.php?erro=4');
}

if ($upload_foto) {

    $DAO = new DAO();

    try {
        $result = $DAO->query("INSERT INTO TB_ADVOGADOS (TXT_NAME, NM_AGE, TXT_DESCRIPTION, TXT_ATUATION, TXT_EXPERIENCE, TXT_GRADUATION, TXT_LANGUAGE, IMG_PATH, TXT_EMAIL, TXT_TEL, TXT_CEL) VALUES (:NAME, :AGE, :DESCRIPTION, :ATUATION, :EXPERIENCE, :GRADUATION, :LANGUAGE, :IMG_PATH, :EMAIL, :TEL, :CEL)", array(":NAME" => $name, ":AGE" => $age, ":DESCRIPTION" => $description, ":ATUATION" => $atuation, ":EXPERIENCE" => $experience, ":GRADUATION" => $graduacao, ":LANGUAGE" => $language, ":IMG_PATH" => $destino, ":EMAIL" => $email, ":TEL" => $tel, ":CEL" => $cel));
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}

if ($result) {
    header('Location: ../admin_advogados.php?success=1');
} else {
    header('Location: ../admin_advogados.php?erro=3');
}

If I run the code just like this, I fall into the following redirect: header('Location: ../admin_advogados.php?erro=3'); , which means that the string was not sent to the bank. But if I remove this piece of code, I fall into the following output: header('Location: ../admin_advogados.php?erro=6'); , which makes me sure the error is in the image storage.

Any idea what to do?

    
asked by anonymous 21.09.2017 / 21:00

1 answer

1

In order to help the guys with the same problem, I'll leave the solution I found here:

I navigated to the directory where my system is located and used the following command:

chmod -R 777 /var/www/caminho/para/o/diretorio/

It worked fine. I hope to help other people!

    
19.10.2017 / 20:27