Error saving image in directory with PHP

Asked

Viewed 337 times

2

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

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

I suspect it is a directory permission issue. 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 is 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 exactly like this, I fall into the following redirect: header('Location: ../admin_advogados.php?erro=3');, which means that the string has not been sent to the bank. However, if you remove this piece of code, I fall in the following exit: header('Location: ../admin_advogados.php?erro=6');, which gives me certainty that the error is in the image storage.

Any idea what to do?

1 answer

1


For the purpose of helping people who are having 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 well. I hope to help other people!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.