vendredi 17 avril 2020

Is there any way to upload a null value if the file field is empty and when its not empty, upload is value?

First time here.

I'm trying to upload a image in a PHP form and when its empty, the value must be null.

I tried this code and it results when the file field is null! but when i upload some imagem, the result is double, one row with null value and another one with the file field value. Can i have some help?

        <?php
    if (isset($_POST['save'])) {
    $target_dir = "Uploads/";
    $target_file = basename($_FILES["file"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo(PATHINFO_EXTENSION);
    $extensao = strtolower(substr($_FILES['file']['name'], -4));
    $novo_nome = $_POST['titulo'] . $extensao;
    $IsActive = $_POST['IsActive'];
    $titulo = $_POST['titulo'];
    $descricao = $_POST['editor'];

    if($imageFileType != "jpg" || $imageFileType != "png" || $imageFileType != "jpeg" || $imageFileType != "gif" || $imageFileType != "docx" || $imageFileType != "doc" || $imageFileType != "html" || $imageFileType != "rar" || $imageFileType != "zip" || $imageFileType != "pdf" || $imageFileType != "xls" || $imageFileType != "xlsx" || $imageFileType != "xml" || $imageFileType != "psd" || $imageFileType != "svg") {
    if(!empty($_FILES['file']))
    {
    $sqli = "INSERT INTO `noticias` (Titulo, Descricao, IsActive) VALUES ('$titulo', '$descricao', '$IsActive')";
    $result = mysqli_query($conn,$sqli);
    if ($result) {
    echo "<meta http-equiv='refresh' content='0;url=noticias.php'>";
    };
    }
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir.$novo_nome)) {
    $files = basename($_FILES["file"]["name"]);
    }else{
    exit;
    }
    }else{
    echo "File Not Supported";
    }
    $location = '../bo/uploads/' . $novo_nome;
    $sqli = "INSERT INTO `noticias` (Titulo, Descricao, Imagem, IsActive) VALUES ('$titulo', '$descricao', '$location', '$IsActive')";
    $result = mysqli_query($conn,$sqli);
    if ($result) {
    echo "<meta http-equiv='refresh' content='0;url=noticias.php'>";
    };

    }
    ?>

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire