I upload videos and thumbnails from this video to the server, you need to save both files with the same name.

for both files to get the name approximately: 123.png and 123.mp4

But, necessarily a random and unique name.

<?php ini_set('memory_limit', '1000M'); header('Content-Type: application/json'); $video_thumb_name = uniqid('video_', true); if (isset($_REQUEST['saveCanvas'])) { $img = $_POST['imgBase64']; $img = str_replace('data:image/png;base64,', '', $img); $img = str_replace(' ', '+', $img); $data = base64_decode($img); $fileThumb = $video_thumb_name . '.png'; $success = file_put_contents('thumbnails/'.$fileThumb, $data); print $success ? $fileThumb : 'Unable to save the file.'; } $return['status'] = 0; if (isset($_FILES['file'])) { $name = $_FILES['file']['name']; $extension = explode('.', $name); $extension = end($extension); $type = $_FILES['file']['type']; $size = $_FILES['file']['size'] / 1024 / 1024; $tmp = $_FILES['file']['tmp_name']; $videoName = $video_thumb_name . '.' . $extension; if ($_FILES["file"]["type"] == "video/mp4") { move_uploaded_file($tmp, 'videos/' . $videoName); $return['videoName'] = $videoName; $return['thumbName'] = $fileThumb; $return['status'] = 1; } echo json_encode($return); } sleep(1); ?> 

Did so ...

No title does not match.

It is necessary to stop the generation of titles $ video_thumb_name = uniqid ('video_', true); at the first appeal, and then the time difference ...

Result on ftp: video_5baf1495b60328.59934882.mp4 and video_5baf149a72c682.53232850.png

---------------------------------------

Another question / problem:

 $return['thumbName'] = $fileThumb; 

I get null here


JS in index.php:

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.2/jquery.form.min.js" integrity="sha384-FzT3vTVGXqf7wRfy8k4BiyzvbNfeYjK+frTVqZeNDFl8woCbF0CYG6g2fMEFFo/i" crossorigin="anonymous"></script> <script> function getThumb(video, thumbnailname) { maxWidth = 700; maxHeight = 700; var w = video.videoWidth; var h = video.videoHeight; if (w >= h) { var ratio = 1 / (w / maxWidth); } else { var ratio = 1 / (h / maxHeight); } var canvas = document.getElementById('canvas'); canvas.width = w * ratio;; canvas.height = h * ratio;; var ctx = canvas.getContext('2d'); ctx.drawImage(video, 0, 0, w * ratio, h * ratio); $.ajax({ type: "POST", url: "upload_video.php?saveCanvas=1", data: { imgBase64: document.getElementById('canvas').toDataURL('image/png') } }).done(function(o) { console.log(0); }); } jQuery('#myForm').on('change', function(e) { e.preventDefault() var percent = jQuery('.percent'); jQuery(this).ajaxSubmit({ url: 'upload_video.php', beforeSend: function() { var percentVal = '0%'; percent.html(percentVal); }, uploadProgress: function(event, position, total, percentComplete) { jQuery("#myf1_upload_process, #divLoading").css("visibility", "visible"); jQuery("#file_browse_wrapper").css("visibility", "hidden"); var percentVal = 'Uploaded: ' + percentComplete + '%'; percent.html(percentVal); }, success: function(xhr) { console.log(xhr); //status.html(xhr.responseText); var loaded = false; var videos = xhr.videoName; var video = document.getElementById('video'); //video.src = 'videos/' + videos; video.src = videos; video.load(); video.addEventListener('canplay', function() { this.currentTime = this.duration / 15; }, false); saved = false; setInterval(function() { if (!saved) { var thumbnailname = xhr.thumbName; if (video.readyState >= 3) { saved = true; getThumb(video, thumbnailname); jQuery("#myeditor").val(videos + '\n' + thumbnailname + '.png'); jQuery("#myf1_upload_process, #divLoading").css("visibility", "hidden"); jQuery("#file_browse_wrapper").css("visibility", "visible"); jQuery("#myf1_upload_form").html('<br /> <input id="file_browse" type="file" name="file" accept="video/mp4" size="0" style="cursor:pointer" /><input type="submit" name="submitBtn" class="mysbtn" value="Upload Image" />'); } } }, 1000); } }); return true; }); </script> 
  • If the question is in a unique name, for example, use the value time (), respectively, the value will be unique + you can determine the recording time - Dmitriy
  • Thanks for the advice, I will also try. Added: uniqid ('video_', true); Main question: How to save both files with the same name. It doesn’t work out yet :( - Erekle Maziashvili Sep.
  • Due to the fact that the time () function in different periods of time is different, first assign its value to a variable, and after that assign the name of the video file - Dmitriy

1 answer 1

To generate a unique file name, rand () is not suitable, it is better to use uniqid () with the more_entropy parameter. And in order for the variable to be visible in both conditions, it must be brought out, for example, after header('Content-Type: application/json');

  • @Ivan Chaykin Corrected my first post, please see what's wrong there. - Erekle Maziashvili
  • Script execution result: {"status":1,"videoName":"video_5baea3bbbc90e4.42089451.mp4","thumbName":"video_5baea3bbbc90e4.42089451.png"} . Give your result. And I advise you to correct the method of generating a unique name by something like this: $video_thumb_name = uniqid('video_', true); - Ivan Chaykin
  • @Ivan Chaykin Corrected on your advice. Result: video_5baf1495b60328.59934882.mp4 and video_5baf149a72c682.53232850.png - Erekle Maziashvili
  • one
    Now everything is cleared up =) The getThumb function accepts the thumbnailname but not how it does not process it. Need to getThumb to fix $.ajax({ type: "POST", url: "upload_video.php?saveCanvas=1", data: { imgBase64: document.getElementById('canvas').toDataURL('image/png'), thumbnailName: thumbnailname } . And in upload_video.php, in the condition where you save the image, assign the $ fileThumb variable the value passed by the getThumb function $fileThumb = $_POST['thumbnailName'] . '.png'; - Ivan Chaykin
  • one
    Well, $return['thumbName'] = $fileThumb; change to $return['thumbName'] = $video_thumb_name; in the second condition - Ivan Chaykin