3

I haven't been able to find any solutions here so far, so I really need to ask about this.

I'm building a video-hosting site, using PHP and mySQL and where video tutorials (called "answers" in my site) are uploaded in response to requests for help in hobby-making activities.

I need my site to extract the metadata of newly uploaded files during the creation of a new video record (to be put into a answers table in mySQL).

So far, I got the basic uploading and storing of new files working perfectly and you can read the code for yourself here:

function insert_answer($answer) {
    global $db;

    $errors = validate_answer($answer);
    if(!empty($errors)) {
      return $errors;
    }

        $fileowner = $_SESSION['user_id'];
        $filename = $_FILES['videoUpload']['name'];
        $filename = $fileowner.'-'.$filename;
        $filetype = $_FILES['videoUpload']['type'];
        $filesize = $_FILES['videoUpload']['size'];
        $cname = str_replace(" ","_",$filename);
        $tmp_name = $_FILES['videoUpload']['tmp_name'];
        $target_path = "../private/media/";
        $target_file = $target_path . basename($cname);
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); 
        move_uploaded_file($_FILES['videoUpload']['tmp_name'], $target_file);


    $author = $_SESSION['user_id'];  
    $mysqldate = date('H:i d/m/Y');

    $sql = "INSERT INTO answers ";
    $sql .= "(user_id, request_id, title, content, tags, date, video, length) ";
    $sql .= "VALUES (";
    $sql .= "'" . db_escape($db, $author) . "',";
    $sql .= "'" . db_escape($db, $answer['request_id']) . "',";
    $sql .= "'" . db_escape($db, $answer['title']) . "',";
    $sql .= "'" . db_escape($db, $answer['content']) . "',";
    $sql .= "'" . db_escape($db, $answer['tags']) . "',";
    $sql .= "'" . db_escape($db, $mysqldate) . "',";
    $sql .= "'" . db_escape($db, $target_file) . "',";
    $sql .= "'" . db_escape($db, $answer['length']) . "' ";
    $sql .= ")";
    $result = mysqli_query($db, $sql);
    // For INSERT statements, $result is true/false
    if($result) {
      return true;
    } else {
      // INSERT failed
      echo mysqli_error($db);
      db_disconnect($db);
      exit;
    }
  }

Right now, I am missing the solution to record the length of the uploaded videos and, possibly, any other metadata I may not be aware could be useful. I previously tried exif_read_data to extract that metadata until I learned that it only works on image files.

To be crystal clear, when creating a new record for the answers table, I select the file to upload as well as fill out a form before clicking submit. Once I click submit, the site should then extract the metadata and record them into the new entry.

Please not that I am building this website in Plesk as part of a university assignment so I'm not sure if server-side tools would work and I am still rather new to web design.

Any help and advice will be appreciated.

  • Your script is wide open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Apr 11 '18 at 13:37
  • Sounds to me like you may be looking for [the PHP EXIF extension](http://php.net/manual/en/book.exif.php) – RiggsFolly Apr 11 '18 at 13:39
  • It would be a problem if you can't install php extensions. – Trinopoty Apr 11 '18 at 13:52
  • Have you tried opening the file and trying to get the stream meta data with `stream_get_meta_data()`? No idea if it works, but it might be worth trying. – jeroen Apr 11 '18 at 13:54
  • Have a look at this: http://ffmpeg-php.sourceforge.net/ though it'll require you to compile and install it on the server. – Trinopoty Apr 11 '18 at 13:57
  • Sorry, can you install it by copying and pasting a script line on the header - the same way you can with Bootstrap and PHP? –  Apr 13 '18 at 12:36
  • Okay, jeroen. I searched stream_get_meta_data() and tried out an example I found. But the results I found come out like this `Array ( [timed_out] => [blocked] => 1 [eof] => [wrapper_type] => plainfile [stream_type] => STDIO [mode] => r [unread_bytes] => 0 [seekable] => 1 [uri] => ../private/media/1-TestVideo.mp4 ) ` I isn't actually the kind of information I'm looking for. –  Apr 13 '18 at 12:53

0 Answers0