Moving from php 5 to php 7 problems

I have some code that loops through image files on a website and determines how old they are and displays them on the page if they are newer than 1 day.
This used to work.
It no longer works after upgrading my website to php 7.

Is there a way I can check this code for compatibility?

Can you expand on “no longer works” - do you get error messages, does it get part-way through the code and not behave as it should (or used to), or does it do nothing at all?

This no longer displays the images, just a blank page.

    $dir =        $_SERVER['DOCUMENT_ROOT'] . '/MyDir';
    $file_display = array('jpg', 'jpeg', 'png', 'gif');
    $seconds_old = 86400;

    if (file_exists($dir) == false) 
      echo 'Directory \'', $dir, '\' not found!';
    } else 
      $dir_contents = scandir($dir);

      foreach ($dir_contents as $file) {
        $file_type = strtolower(end(explode('.', $file)));

        if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true)     
    	    IF( @FILEMTIME($dir. '/'. $file) > (TIME()-$seconds_old) )
            //echo '<img src="'. $dir. '/'. $file. '" alt="'. $file. '" />';
            echo "\n";
            echo "Time: " . date ("F d Y H:i:s.", filectime($dir. '/'. $file));
            echo "\n";
            echo '<img src="'. ''. $file. '" alt="'. $file. '" />';

Do you have error reporting on?
If not, do.
What errors do you get?

1 Like

It was showing a blank page instead of showing the images but I think it is working again after upgrading to php 7. Does anything in there look like it was deprecated by the newer version?
What’s a good way to check PHP code when new versions come out?

I’ll take that as a “no”.

Error reporting, like I asked about.
If you use anything that is deprecated, or just some random coding typo or whatever, it tells you about it and where to find it in your script so you can track it down and fix it quickly.

Not using error suppression might help. i.e.

@FILEMTIME($dir. '/'. $file) > (TIME()-$seconds_old) )

Currently the “@” error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use “@” to suppress errors from a certain function and either it isn’t available or has been mistyped, the script will die right there with no indication as to why.

1 Like

In order to gain PHP 7 increased performance it is necessary to specifically declare variable types and function return values. Using the following script enables this increased level of error checking and eliminates sloppy variable declarations. I think it is well worth inserting at the beginning of every PHP file.

<?php # implementing  strict_types:

/** must be the  FIRST declaration 
 *    and is ONLY file wide, Sad I would prefer a global setting
declare(strict_types=1);  # accepts 0 as a toggle-off

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.