Hazem Taha
Hazem Taha

Reputation: 1184

PHP Website not responding while streaming a video

My website is developed using pure PHP with MySQL while streaming a video "Please try this link" Here

If you click on any button called "مشاهدة", the website will not respond to any other request like navigating to any other page.

Here is the code of my Stream.php file

<?php

session_start();
require("includes/connect.php");

set_time_limit(0);
if(isset($_GET["file"])){
    if(!isset($_GET["start"])) $_GET["start"] = 0;
    $seekPos = $_GET["start"];
    $file = htmlspecialchars($_GET["file"]);
    $fileName = basename($file);
    $file = $config["videoFilesPath"]."/".$fileName;
    if(!file_exists($file)) die("file does not exist: $file");
    $fh = fopen($file, "rb") or die("failed to open file");
    fseek($fh, 0, SEEK_END); $seek_end = ftell($fh);
    fseek($fh, $seekPos); $seek_start = ftell($fh);
    $fileSize =  $seek_end - $seek_start;
    session_cache_limiter('nocache');
    header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
    header("Pragma: no-cache");
    header("Content-Type: video/x-flv");
    //header("Content-Disposition: attachment; filename=\"" . $fileName . "\"");
    header('Content-Length: ' . $fileSize);
    if($seekPos != 0){
        print("FLV");
        print(pack('C', 1 ));
        print(pack('C', 1 ));
        print(pack('N', 9 ));
        print(pack('N', 9 ));
    }
    fseek($fh, $seekPos);
    $i=1;
    while (true){
        $var = fread($fh, 1024);
        if($var==false) break;
        echo $var;
        if(++$i%(1<<20)) ob_flush();
    }
    fclose($fh);
}

Upvotes: 0

Views: 73

Answers (1)

Marek
Marek

Reputation: 7433

session_start() opens and locks the session file, therefore you must call session_write_close() before any long running process:

session_start();
require("includes/connect.php");
session_write_close();

Upvotes: 1

Related Questions