javascript - PHP And AJAX Download of a few MB file freezes website -
hello ive searched everywhere find answer none of solutions ive tried helped
what building site connects youtube allow users search , download videos mp3 files. have built site search etc having problem download part (ive worked out how youtube audio file). format audio audio/mp4 need convert mp3 first need file on server
so on download page ive made script sends ajax request server start downloading file. sends request different page every few seconds find out progress , update on page user viewing.
however problem while video downloading whole website freezes (all pages dont load until file downloaded) , when script tries find out progress cant until done.
the file downloads:
<?php session_start(); if (isset($_get['yt_vid']) && isset($_get['yrt'])) { set_time_limit(0); // prevent script stopping execution include "assets/functions.php"; define('chunk', (1024 * 8 * 1024)); if ($_get['yrt'] == "gphj") { $vid = $_get['yt_vid']; $mdvid = md5($vid); if (!file_exists("assets/videos/" . $mdvid . ".mp4")) { // check if file exists, if not proceed downloading $url = urlscraper($vid); // urlscraper function function audio file, sends simple curl request , takes less second complete if (!isset($_session[$mdvid])) { $_session[$mdvid] = array(time(), 0, retrieve_remote_file_size($url)); } $file = fopen($url, "rb"); $localfile_name = "assets/videos/" . $mdvid . ".mp4"; // file stored on server doesnt have downloaded every time $localfile = fopen($localfile_name, "w"); $time = time(); while (!feof($file)) { $_session[$mdvid][1] = (int)$_session[$mdvid][1] + 1; file_put_contents($localfile_name, fread($file, chunk), file_append); } echo "execution time: " . (time() - $time); fclose($file); fclose($localfile); $result = curl_result($url, "body"); } else { echo "failed."; } } } ?>
i had problem in past, reason not work because session can once open writing. need modify download script , use session_write_close()
each time directly after writing session.
like:
session_start(); if (!isset($_session[$mdvid])) { $_session[$mdvid] = array(time(), 0, retrieve_remote_file_size($url)); } session_write_close();
and in while
while (!feof($file)) { session_start(); $_session[$mdvid][1] = (int)$_session[$mdvid][1] + 1; session_write_close(); file_put_contents($localfile_name, fread($file, chunk), file_append); }
Comments
Post a Comment