1

I am migrating my project from PHP 5.X to PHP 8.0.8 but I have a problem with a script. The script allows users to export the selected clip's to a zip file, I've been testing it and it seems to work fine sometimes and sometimes it returns this error:

[Mon Aug 30 09:55:21.651635 2021] [php:warn] [pid 2772] [client 10.10.2.10:47942]PHP Warning: fopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /userdata/demo/action/download_clips.php on line 164, referer: http://demo.com/index.php

[Mon Aug 30 09:55:21.651635 2021] [php:warn] [pid 2772] [client 10.10.2.10:47942] PHP Warning: fopen(http://demo.com/ec/6d703f2dc2561d902fab564f70c4a508.mp4): Failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /userdata/demo/action/download_clips.php on line 164, referer: http://demo.com/index.php

[Mon Aug 30 09:55:21.651774 2021] [php:error] [pid 2772] [client 10.10.2.10:47942] PHP Fatal error: Uncaught TypeError: feof(): Argument #1 ($stream) must be of type resource, bool given in /userdata/demo/action/download_clips.php:167\nStack trace:\n#0 /userdata/demo/action/download_clips.php(167): feof(false)\n#1 {main}\n thrown in /userdata/demo/action/download_clips.php on line 167, referer: http://demo.com/index.php

$slides = json_decode($_POST['slides']);
$files_seek = json_decode(get_files_from_slides($slides));

if($files_seek->status !== 'success' || sizeof($files_seek->data->files) == 0){
    
    echo json_encode($files_seek);
    exit;
    
}

$files = $files_seek->data->files;
$files_zipped = 0;

if(sizeof($files) > 0){
        
    $tmp_file = tempnam($zip_folder, 'zp');
    chmod($tmp_file, 0775);
    
    $tmp_zip = property_is_defined($_POST['zip']) ? $zip_folder . $_POST['zip'] : tempnam($zip_folder, 'zp');
    chmod($tmp_zip, 0775);
    
    $zip = new ZipArchive();
    
    foreach($files as $file){
        
        if($file->src){
            set_time_limit(400);
            
            $zip->open($tmp_zip, ZipArchive::CREATE);       
            
            $handle = fopen($tmp_file, "w");        
            $f = fopen($file->src, 'r');

            while (!feof($f)) {
                fwrite($handle, fread($f, 256));
            }

            fclose($handle);    
            fclose($f); 
        
           $filename = mb_ereg_replace("([^\w\s\d\-_\[\]\(\).])", '_', $file->name);
           $filename = mb_ereg_replace("([\.]{2,})", '', $filename);

           $zip->addFile($tmp_file, $filename);
           $files_zipped++;

           $progress = ceil($files_zipped * 100 / sizeof($files));
           $p = fopen($tmp_zip.".progress", "w+");
           fwrite($p, $progress);
           fclose($p);
           $zip->close();
        }
    }

    if(file_exists($tmp_file)) unlink($tmp_file);
    
    $zip_name = date("YmdHis") . "_" . $_SESSION['user']['id'] . ".zip";
    rename($tmp_zip, $zip_folder . $zip_name);

I have tested the links and it works, I have done ping and nslookup to the domain and it works.

What am I doing wrong ?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
cplaiuu
  • 163
  • 4
  • 18
  • Separate issue: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Barmar Sep 03 '21 at 15:55
  • Hi, could you try to [edit] the code down to a [mcve]? If the URLs you're using aren't sensitive, it might be useful to know what URL it is failing on. – IMSoP Sep 03 '21 at 15:56
  • 2
    The problem isn't in `feof()`, the problem is that `fopen()` is failing. You should always check that `fopen()` successfully opened the file before trying to use it. – Barmar Sep 03 '21 at 15:58
  • @Barmar I will try, thanks. – cplaiuu Sep 06 '21 at 07:17
  • @Barmar Return values without a fixed type are really php's worst joke... – The incredible Jan Sep 01 '22 at 09:15

0 Answers0