I am writing a parser to get a video. But I can’t save the resulting video to the folder I need. How to fix here code.

foreach ($href as $key => $link) { $doc = file_get_html('https://example.com/'.$link); foreach($doc->find("#video source") as $el) { $video[]="https:".$el->src; //с помощью simple html dom получаю ссылки на файл } } //в итоге получаю /* array( "http:example/video1.mp4", "http:example/video2.mp4", "http:example/video3.mp4") */ $dirSubtitles=$_SERVER['DOCUMENT_ROOT'].'/video/'; foreach ($video as $address) { $url = $address; $path = $dirSubtitles; $fp = fopen($path."video", 'w'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_FILE, $fp); $data = curl_exec($ch); curl_close($ch); fclose($fp); } 

    3 answers 3

     $dirSubtitles=$_SERVER['DOCUMENT_ROOT'].'/video/'; foreach ($video as $address) { copy($address,$dirSubtitles); } 

    Regarding the time limits (and memory). You need to increase the memory_limit if there is a heavy * process. Regarding the work time, you can safely set up more if there is only one such user on the system.

     ini_set('max_execution_time', 9000); ini_set('memory_limit', '200M'); 
     $dirSubtitles = __DIR__ . '/video/'; foreach ($video as $address) { $url = $address; $path = $dirSubtitles; $name = pathinfo($address, PATHINFO_BASENAME); $fp = fopen($path . time() . $name, 'w'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_FILE, $fp); curl_exec($ch); curl_close($ch); fclose($fp); } 
    • Fatal error: Maximum execution time of 30 seconds exceeded - G_test_00
    • Well, how could it be. If you download 100,500 movies. Run through the CLI and remove the time limit - Ninazu
    • if you do not mind write how it will look like - G_test_00
    • one
     <?php set_time_limit(0); //This is the file where we save the information $fp = fopen(dirname(__FILE__) . '/localfile.tmp', 'w+'); //Here is the file we are downloading, replace spaces with %20 $ch = curl_init(str_replace(" ", "%20", $url)); curl_setopt($ch, CURLOPT_TIMEOUT, 50); // write curl response to file curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // get curl response curl_exec($ch); curl_close($ch); 

    Can look here