How to Show result after uploaded file in PHP

Viewed 177

I have a script that uploads the video to a server, everything is correct but there is a problem, after the upload of the video to the server is completed it shows all the uploaded files in the (uploads) folder as array!

I only want the result of the file I just uploaded, and it doesn't show me the previous files! I need ffmpeg to improve video quality

index.php

<?php
//index.php

?>
<!DOCTYPE html>
<html>
 <head>
  <title>Personal Video Uploader</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>        
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/dropzone.css" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/dropzone.js"></script>
  <link rel="stylesheet" href="css/main.css">
 </head>
 <body class="bg-body">
  <div class="container">

   <form action="upload.php" class="dropzone font" id="dropzoneFrom">

   </form>
   
    
   </form>
   <br />

   <div id="preview"></div>

   <br />
  </div>
 </body>
</html>

<script>

$(document).ready(function(){
 
 Dropzone.options.dropzoneFrom = {
  autoProcessQueue: true,
  timeout: 300000,
  acceptedFiles:"video/*",
  init: function(){
   var submitButton = document.querySelector('#submit-all');
   myDropzone = this;
   submitButton.addEventListener("click", function(){
    myDropzone.processQueue();
   });
   this.on("complete", function(){
    if(this.getQueuedFiles().length == 0 && this.getUploadingFiles().length == 0)
    {
     var _this = this;
     _this.removeAllFiles();
    }
    list_image();
   });
  },
 };

 list_image();

 function list_image()
 {
  $.ajax({
   url:"upload.php",
   success:function(data){
    $("#preview").html(data);
   }
  });
 }

 $(document).on('click', '.remove_image', function(){
  var name = $(this).attr('id');
  $.ajax({
   url:"upload.php",
   method:"POST",
   data:{name:name},
   success:function(data)
   {
    list_image();
   }
  })
 });
 
});
</script>

upload.php

    <?php

//upload.php

$folder_name = 'upload/';
$tumb_name = 'thumb/';
$imageext = '.png';

if(!empty($_FILES))
{

 $temp_file = $_FILES['file']['tmp_name'];
 $location = $folder_name . $_FILES['file']['name'];
 move_uploaded_file($temp_file, $location);
 $upload = $_FILES['file']['name'];
 $uploadStr = str_replace(" ", "\ ",$upload);
 $locationStr = str_replace(" ","\ ",$location);
 $cmd  = "ffmpeg -y -i {$locationStr} -ss 00:00:15 -vframes 1 thumb/{$uploadStr}.png 2>&1";
 echo shell_exec($cmd);
}

if(isset($_POST["name"]))
{
 $filename = $folder_name.$_POST["name"];
 $imagename = $thumb_name.$_POST["name"].$imageext;
 unlink($filename);
 unlink($imagename);
}

$result = array();

$files = scandir('upload');

$output = '<div class="row">';

if(false !== $files)
{
 foreach($files as $file)
 {
  if('.' !=  $file && '..' != $file)
  {
   $output .= '
   <a href="/view.php?file='.$file.'" target="_blank"> <img src="thumb/'.$file.'.png" class="img-thumbnail" width="246px" height="138px" style="height:138px;" /></a>
    <button type="button" class="btn btn-link remove_image" id="'.$file.'">Remove</button>
   ';
  }
 }
}
$output .= '</div>';
echo $output;

?>

EDIT: I put the example on an Array, I don't want it, I just want it to show the downloaded video I just uploaded as a result.

EDIT 2 : There are some who say type $location and it displays the downloaded file, but this does not work!!! I just tried more than once and with several uses, there is no display where the text is empty

This is an example of that

<?php

//upload.php

$folder_name = 'upload/';
$tumb_name = 'thumb/';
$imageext = '.png';

if(!empty($_FILES))
{

 $temp_file = $_FILES['file']['tmp_name'];
 $location = $folder_name . $_FILES['file']['name'];
 move_uploaded_file($temp_file, $location);
 $upload = $_FILES['file']['name'];
 $uploadStr = str_replace(" ", "\ ",$upload);
 $locationStr = str_replace(" ","\ ",$location);
 $cmd  = "ffmpeg -y -i {$locationStr} -ss 00:00:15 -vframes 1 thumb/{$uploadStr}.png 2>&1";
 echo shell_exec($cmd);
}

if(isset($_POST["name"]))
{
 $filename = $folder_name.$_POST["name"];
 $imagename = $thumb_name.$_POST["name"].$imageext;
 unlink($filename);
 unlink($imagename);
}


$output .= 'Successfly file is "'.$location.'"';
echo $output;

?>

Result : Successfly file is "" no name file :(

EDIT 3 :

this code upload.php functions not working

<?php

//upload.php

$folder_name = 'upload/';
$tumb_name = 'thumb/';
$imageext = '.png';

if(!empty($_FILES))
{

 $temp_file = $_FILES['file']['tmp_name'];
 $location = $folder_name . $_FILES['file']['name'];
 move_uploaded_file($temp_file, $location);
 $upload = $_FILES['file']['name'];
 $uploadStr = str_replace(" ", "\ ",$upload);
 $locationStr = str_replace(" ","\ ",$location);
 $cmd  = "ffmpeg -y -i {$locationStr} -ss 00:00:15 -vframes 1 thumb/{$uploadStr}.png 2>&1";
 echo shell_exec($cmd);
}


echo "The file " . $location . " has been uploaded";
// not working
echo "<br>";
echo "The file " . $upload . " has been uploaded";
// not working
echo "<br>";
echo "The file " . $uploadStr . " has been uploaded";
// not working
echo "<br>";
echo "The file " . $locationStr . " has been uploaded";
// not working

?>

Result upload.php in EDIT 3

this error: [23-Apr-2022 12:31:56 Asia/Riyadh] PHP Notice: Undefined variable: location in /home/prdix/public_html/test/upload.php on line 38

line 38: echo $location;

About the developer solution Markus AO

I did the experiment and it is quite good, but dropzone is still missing, because I will upload a large video and the normal upload compresses the video before uploading, here is a picture from my mobile while uploading, but this does not happen with dropzone enter image description here

I want to implement this in dropzone as well, because this library does not compress the video, but upload it in full size.

Thank you bro.

0 Answers
Related