user7133318
user7133318

Reputation: 73

Javascript Get video length

Hello I have the following code:

          <form action="postvideophp.php" method="post" enctype="multipart/form-data">       
         <input id="videoage" type="file" name="video" style="-webkit-appearance: none;-webkit-border-radius: 0;margin-left:-242px;margin-top:10px;opacity:0;">
         <label for="videoage" id="labelvideo">Choose Video...</label>

         <textarea id="postTitleStyle" onkeyup="countChars('postTitleStyle','titlecount');" onkeydown="countChars('postTitleStyle','titlecount');" onmouseout="countChars('postTitleStyle','titlecount');" name="title" rows="1" maxlength = "180"  placeholder="Title"><?php echo $title;?></textarea>
         <a id="titlecount" style="color:#F52025;Font-family:Arial Black;display:table;margin-top:-20px;margin-left:840px;">0</a>
         <textarea id="postTagStyle" onkeyup="countChars2('postTagStyle','descripcount');" onkeydown="countChars2('postTagStyle','descripcount');" onmouseout="countChars2('postTagStyle','descripcount');" name="desc" rows="2" maxlength = "1000" placeholder="Description"><?php echo $DESC;?></textarea>
         <a id="descripcount" style="color:#3FDA21;Font-family:Arial Black;display:table;margin-top:-20px;margin-left:840px;">0</a>      
       <center><input type="submit" class = "Post2" value="[&nbsp;&nbsp;Post&nbsp;&nbsp;]"></center>
     </form>

And here is some of my php code after it gets posted:

if(FileSize($_FILES["video"]["tmp_name"]) >= 120){
  $uploadable = false;
  header("Location:post-video");
  $_SESSION["FLAW"] = '10';

}

I want the browser to go to an error screen called post-video when if the filesize of the posted video is greater than 120 bytes(for example). The problem I currently have is that it posts the whole video which could take 10 minutes if the user uploads a really big video file. After the video is posted and it is bigger than 120bytes the error screen displays. Is there a way, possible using javascript to detect the size of the video early on? If so how can i do this quickly?

Upvotes: 2

Views: 317

Answers (2)

vbguyny
vbguyny

Reputation: 1172

You can add some JavaScript to check for this:

 var file = document.getElementById("videoage").files[0]; 
if (file.size > 120) { /* do something */ }

Upvotes: 1

Victory
Victory

Reputation: 5890

You can get the filesize via $_FILES super global

if ($_FILES["video"]['size'] >= $fileLimit) {
  // handle oversized file
}

You can this client side for quicker error handling in supported browsers using:

var fileInput = document.getElementById("videoage");
fileInput.addEventListener("change", function () {
   if (fileInput.files[0].size > 120) {
     alert("file too big");
   }
});

Upvotes: 1

Related Questions