upload update

Asked

Viewed 61 times

1

I have a problem in the way to save the upload file. I have a php file that does the update that is in the process folder, and an upload folder to save the files that were saved.

$id = $_POST['id'];
$titulo = $_POST['titulo'];
$descricao = $_POST['descricao'];
$postador = $_POST['postador'];
$arquivo = $_FILES['arquivo']['name'];

//Substituindo os caracteres especiais
$original = 
'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜüÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿRr"!@#$%&*
()_-+={[}]/?;:,\\\'<>°ºª';

$substituir = 
'aaaaaaaceeeeiiiidnoooooouuuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr                                
';

$arquivo = strtr(utf8_decode($arquivo), utf8_decode($original), 
$substituir);

//Substitui os espaços em branco
$arquivo = str_replace(' ', '-', $arquivo);

//Substitui os Maiúsculos por Minúsculos
$arquivo = strtolower($arquivo);

$caminhofinal = "upload".'/'.$arquivo;

move_uploaded_file($_FILES['arquivo']['tmp_name'],$caminhofinal);

$sql_query = "UPDATE posts SET titulo = '$titulo', arquivo = 
'$caminhofinal', descricao = '$descricao', data = NOW(), hora = NOW(), 
postador = '$postador' WHERE id = '$id'";
$resultado = mysqli_query($mysqli, $sql_query);

if($resultado === TRUE){
    $_SESSION['msg'] = "<div class='alert alert-success alert-dismissable'>
                    <a href='#' class='close' data-dismiss='alert' aria-
    label='close'>&times;</a>
                    ID: $id Editado com Sucesso!</div>";

    header("Location: ../professor/lista_conteudo.php");
}  else {
     $_SESSION['msg'] = "<div class='alert alert-danger'>
                    <a href='#' class='close' data-dismiss='alert' aria-
     label='close'>&times;</a>
                    Erro ao Editar ID: $id</div>";
     header("Location: ../professor/editar_conteudo.php");
}

The point is that you are saving the url in the database but not the file in the folder. I have tried putting in the variable $camiofinal = ".. /upload". '/'. $file; but it didn’t work. Can anyone help me? Thanks!

  • It may be folder permission, try to put write permission in your final path folder

1 answer

-1

As Gabriel Santos commented, folder options are very important in this case, but for a good understanding of the error it is also important to isolate the possibilities.

When I do uploads, I isolate this way:

if(move_uploaded_file($arquivo,$destino)){
    //ok
}else{
    //não ok
}

This way you know whether the file was uploaded or not running the action within the if check itself.

Now to know what the problem was I suggest an approach with Try catch:

try {
    if (!move_uploaded_file( ... )) {
        throw new Exception('Arquivo não Enviado!');
    }
} catch (Exception $e) {
    die ('Motivo: ' . $e->getMessage());
}

Hugs!

Browser other questions tagged

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