2
I’m setting up a news system and in the Adm panel for registration today, the code works as follows: Register the title/text/image/ and make it active/inactive in the situation: In this case the inactive uses as draft. All right, it works perfectly. The detail is that with the code I have to upload images, only makes the registration of the news with image upload. I would like to insert a check, that if there is no image loaded, it passes the same and makes the registration of the news without image.
Follows the code:
<?php
    include_once("../../conexao/conexao.php");
    $nome = mysqli_real_escape_string($conn, $_POST['nome']);
    $texto = mysqli_real_escape_string($conn, $_POST['texto']);
    $situacao_noticia_id = mysqli_real_escape_string($conn, $_POST['situacao_noticia_id']);
    $arquivo = $_FILES["arquivo"]["name"];
    $caminho = "../../../imagens/noticia/";
    $_UP['pasta'] = $caminho;
    //Tamanho máximo do arquivo em Bytes
    $_UP['tamanho'] = 1024*1024*5; //5mb
    //Array com os tipos de erros de upload do PHP
    $_UP['erros'][0] = 'Não houve erro';
    $_UP['erros'][1] = 'O arquivo no upload é maior que o limite';
    $_UP['erros'][2] = 'O arquivo ultrapassa o limite de tamanho especificado';
    $_UP['erros'][3] = 'O upload do arquivo foi feito parcialmente';
    $_UP['erros'][4] = 'Não foi feito o upload do arquivo';
    //Verfica se houve algum erro com o upload. Se sim, exibe mensagem de erro
    if($_FILES['arquivo']['error'] != 0){
        die("Não foi possivel fazer o upload, erro: <br>". $_UP['erros'][$_FILES['arquivo']['error']]);
        exit; //para a execução do script
    }
    //Fazer a verificação do tamanho do arquivo
    if($_UP['tamanho'] < $_FILES['arquivo']['size']){   
        echo "
            <META HTTP-EQUIV=REFRESH CONTENT = '0;URL=$dominioadm/administrativo.php?link=31'>
            <script type=\"text/javascript\">
                alert(\"ERRO! Não foi possivel cadastrar a notícia tente novamente!\");
            </script>
        ";  
    }
else {
// Codigo de inserção
$sql = "INSERT INTO noticias (nome, texto, situacao_noticia_id, imagem, created) VALUES ('{$nome}', '{$texto}', '{$situacao_noticia_id}', '{$arquivo}', NOW())";
// Converte e Executa a query
$inserir = mysqli_query($conn,$sql); 
I’d appreciate it if someone could help.
Did you write the code? What was the difficulty of doing what you need?
– Woss
I’m not sure what to do, if I perform a check in the file field, or if I insert an if to check?
– Marcelo Rossi
What’s the difference between "check in" or "insert an if"?
– Woss
check the input, type check if you have any file loaded in the form, or an if in the processing.
– Marcelo Rossi
easier to check if the image has been loaded if yes, still continue. I will correct your code for you
– user148170