How to Insert MYSQL if Upload or URL

Asked

Viewed 49 times

2

Hello, I would like to know how to make a mysql Insert, however in the way that I only used javascript to change the imput URL for File, that is, I’m trying to work on the same imput name, however I doubt what to do in the Internet if the person does not want to read the upload and just enter the url, or otherwise, below this code, it may not be correct however if someone can help me, since I thank you.

$("#pre").click(function() {
        var icon = $(this).find("i");
        $("#jc_preview").fadeOut("fast");
        if (icon.hasClass("fa-cloud-upload")) {
            $(this).fadeOut("fast", function() {
                icon.removeClass("fa-cloud-upload").addClass("fa-chain");
                $("#jc_preview").prop("type", "file").fadeIn("fast");
                $(this).fadeIn("fast");
            });
        } else {
            $(this).fadeOut("fast", function() {
                icon.removeClass("fa-chain").addClass("fa-cloud-upload");
                $("#jc_preview").prop("type", "url").fadeIn("fast");
                $(this).fadeIn("fast");
            });
        }
    });
<?php
   if (isset($_POST['create'])) {
   
       $subtitle = mysqli_real_escape_string($conn, $_POST['subtitle']);
       $preview  = mysqli_real_escape_string($conn, $_POST['preview']);
       $preview  = mysqli_real_escape_string($conn, $_FILES["preview"]["tmp_name"];);
       $title    = mysqli_real_escape_string($conn, $_POST['title']);
       $date     = mysqli_real_escape_string($conn, date('Y-m-d H:i:s'));
   
       $new_name = uniqid(); // Novo nome aleatório do arquivo
       $extension = strtolower(pathinfo($_FILES["preview"]["name"], PATHINFO_EXTENSION)); // Pega extensão de arquivo e converte em caracteres minúsculos.
       $folder = "imagens";
       move_uploaded_file($preview, $folder."/".$new_name.".".$extension);
   
       $subtitle = strlen($subtitle) > 0 ? "'$subtitle'" : "NULL";
       $preview  = strlen($preview) > 0 ? "'$preview'" : "NULL";
   
       $vsl = "INSERT INTO `files` (`id`, `subtitle`, `preview`, `title`, `date`)VALUES(NULL, $subtitle, $preview, '$title', '$date')";
       $rsl = mysqli_query($conn, $vsl);
   
       header("Location: ../admin/add.php");
       exit();
   
   }
   ?>
<form action="../actions/create_player.php" method="post" enctype="multipart/form-data">
   <label>Title: <i class="fa fa-info-circle" data-toggle="tooltip" title="" data-original-title="Insert Title For This Player"></i></label>
   <input name="title" class="form-control" type="text" required="required">
   <label for="preview" class="control-label">Custom Preview:</label> <i class="fa fa-info-circle text-muted" data-toggle="tooltip" title="Insert Custom Preview URL For This Video"></i><span class="pull-right"><span class="label label-info cp" id="pre"><i class="fa fa-cloud-upload"></i></span></span>
   <input type="url" class="form-control" id="jc_preview" name="preview" placeholder="Insert Custom Preview URL">
   <br>
   <label>Subtitle: <i class="fa fa-info-circle" data-toggle="tooltip" title="" data-original-title="Subtitle For Player"></i></label>
   <input name="subtitle" class="form-control" placeholder="Subtitles in VTT or Srt Formats" type="url">
   <button type="submit" name="create" class="btn btn-primary btn-block"><i class="fa fa-check"></i> Publish</button>
</form>

  • Do you mean file upoad on your server by a url? I have a php file, which copies between different servers. Works as a download using url. simply create a text field the user places the url.click upload(for example ) the file is already available in your hosting

  • However, if you want the user to have access to this file I suggest storing their name in a database. Then when you go to do the Voce display take only the name text. Adapting of course.

  • and almost that, I want to ultilizar a type "post" and a "file", the Post and to store any Image url added in the POST input in the database, or it has the option to upload any file and save, however I am not sure how to finalize the 2 requirements to add to the database if upload or just post

  • Actually, it doesn’t matter one way or the other. The difference is that the input field copies from the computer. And the schematic I’m talking about copies by the url. You will continue using the same code. Just add mine. And add an deletion of the file after Insert.

  • Later I’ll send you the schematic if no one has answered.

  • Okay, thanks for the collaboration!

Show 1 more comment

1 answer

0


here is a schema for copying files between servers

<?php
$url_download = $_POST['campourl'];
$salvar_em = 'arquivo.jpj';
//inciando download
copy($url_download, $salvar_em);
//download finalizado
//aqui você faz as adaptações nessesarias

//final do arquivo - apagando o arquivo criado.
unlink($salvar_em);
?>
  • managed to adapt?

Browser other questions tagged

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