jeffshead Posted April 13, 2023 Share Posted April 13, 2023 I have the following code which throws an error since updating to PHP 8.x: Code: if ($return_link!="") { $slfile=str_replace($FileLocations[$return_link],"",$directory."/".$this_file); $link = sl_siteloklink($slfile.":".$return_link,1); } else { $slfile=str_replace($FileLocation,"",$directory."/".$this_file); $link = sl_siteloklink($slfile,1); } Error: Quote Fatal error: Uncaught TypeError: Illegal offset type in /home/whatever/php_file_tree.php:71 Stack trace: #0 /home/whatever/php_file_tree.php(63): php_file_tree_dir() #1 /home/whatever/php_file_tree.php(24): php_file_tree_dir() #2 /home/whatever/index.php(143): php_file_tree() #3 {main} thrown in /home/whatever/php_file_tree.php on line 71 Line 71 referenced in the error is: // This is the second line in the code block above $slfile=str_replace($FileLocations[$return_link],"",$directory."/".$this_file); I have tried rewriting line 71 but I don't know what the heck I'm doing. Can someone suggest the correct code? Quote Link to comment https://forums.phpfreaks.com/topic/316130-errors-since-updating-to-php-8/ Share on other sites More sharing options...
kicken Posted April 13, 2023 Share Posted April 13, 2023 The error is due to $return_link being some non-scalar value, such as an array or an object. You need to find where $return_link is defined and why it's not a scalar value. There's not enough information in the code given to determine how it should be fixed. Quote Link to comment https://forums.phpfreaks.com/topic/316130-errors-since-updating-to-php-8/#findComment-1607278 Share on other sites More sharing options...
jeffshead Posted April 13, 2023 Author Share Posted April 13, 2023 Thank for taking the time! Below is all of the code in that PHP file: <?php /* == PHP FILE TREE == Let's call it...oh, say...version 1? == AUTHOR == Cory S.N. LaViska http://abeautifulsite.net/ == DOCUMENTATION == For documentation and updates, visit http://abeautifulsite.net/notebook.php?article=21 */ function php_file_tree($directory, $return_link, $extensions = array()) { // Generates a valid XHTML list of all directories, sub-directories, and files in $directory // Remove trailing slash if( substr($directory, -1) == "/" ) $directory = substr($directory, 0, strlen($directory) - 1); $code .= php_file_tree_dir($directory, $return_link, $extensions); return $code; } function php_file_tree_dir($directory, $return_link, $extensions = array(), $first_call = true) { // Mod for Sitelok global $FileLocation,$FileLocations; // End of mod // Recursive function called by php_file_tree() to list directories/files // Get and sort directories/files if( function_exists("scandir") ) $file = scandir($directory); else $file = php4_scandir($directory); natcasesort($file); // Make directories first $files = $dirs = array(); foreach($file as $this_file) { if( is_dir("$directory/$this_file" ) ) $dirs[] = $this_file; else $files[] = $this_file; } $file = array_merge($dirs, $files); // Filter unwanted extensions if( !empty($extensions) ) { foreach( array_keys($file) as $key ) { if( !is_dir("$directory/$file[$key]") ) { $ext = substr($file[$key], strrpos($file[$key], ".") + 1); if( !in_array($ext, $extensions) ) unset($file[$key]); } } } if( count($file) > 2 ) { // Use 2 instead of 0 to account for . and .. "directories" $php_file_tree = "<ul"; if( $first_call ) { $php_file_tree .= " class=\"php-file-tree\""; $first_call = false; } $php_file_tree .= ">"; foreach( $file as $this_file ) { if( $this_file != "." && $this_file != ".." ) { if( is_dir("$directory/$this_file") ) { // Directory $php_file_tree .= "<li class=\"pft-directory\"><a href=\"#\">" . htmlspecialchars($this_file) . "</a>"; $php_file_tree .= php_file_tree_dir("$directory/$this_file", $return_link ,$extensions, false); $php_file_tree .= "</li>"; } else { // File // Get extension (prepend 'ext-' to prevent invalid classes from extensions that begin with numbers) $ext = "ext-" . substr($this_file, strrpos($this_file, ".") + 1); // Mod for Sitelok if ($return_link!="") { $slfile=str_replace($FileLocations[$return_link],"",$directory."/".$this_file); $link = sl_siteloklink($slfile.":".$return_link,1); } else { $slfile=str_replace($FileLocation,"",$directory."/".$this_file); $link = sl_siteloklink($slfile,1); } // End of mod $php_file_tree .= "<li class=\"pft-file " . strtolower($ext) . "\"><a href=\"$link\">" . htmlspecialchars($this_file) . "</a></li>"; } } } $php_file_tree .= "</ul>"; } return $php_file_tree; } // For PHP4 compatibility function php4_scandir($dir) { $dh = opendir($dir); while( false !== ($filename = readdir($dh)) ) { $files[] = $filename; } sort($files); return($files); } Quote Link to comment https://forums.phpfreaks.com/topic/316130-errors-since-updating-to-php-8/#findComment-1607280 Share on other sites More sharing options...
ginerjm Posted April 13, 2023 Share Posted April 13, 2023 After taking your code and cleaning it up a bit I cannot find any place that defines that $return_link variable. I see it being used but never defined. And why the concern for php 4? You should not need that. Where the error occurs I would add an echo of that value to see what it is. Quote Link to comment https://forums.phpfreaks.com/topic/316130-errors-since-updating-to-php-8/#findComment-1607283 Share on other sites More sharing options...
kicken Posted April 13, 2023 Share Posted April 13, 2023 That it still not enough information. $return_link is a function parameter, so you need find where the function is being called and determine what value it is being called with and why. Quote Link to comment https://forums.phpfreaks.com/topic/316130-errors-since-updating-to-php-8/#findComment-1607284 Share on other sites More sharing options...
jeffshead Posted April 13, 2023 Author Share Posted April 13, 2023 (edited) I did not write the script. It is old so that's why the author had concern for PHP4. This is output of echo and of print_r : echo $return_link; Array print_r($return_link); Array ( [0] => gif [1] => jpg [2] => jpeg [3] => png [4] => pdf [5] => zip [6] => xlsx ) Looks like $return_link is somehow getting mixed up with $extensions since updating server from PHP 7 to PHP 8. If I revert back to PHP 7.3, the script works without error. Edited April 13, 2023 by jeffshead Quote Link to comment https://forums.phpfreaks.com/topic/316130-errors-since-updating-to-php-8/#findComment-1607314 Share on other sites More sharing options...
jeffshead Posted April 14, 2023 Author Share Posted April 14, 2023 This is output of echo and of print_r for PHP 7.3: echo $return_link; Array print_r($return_link); ArrayArrayArrayArrayArrayArrayArrayArray Quote Link to comment https://forums.phpfreaks.com/topic/316130-errors-since-updating-to-php-8/#findComment-1607315 Share on other sites More sharing options...
kicken Posted April 14, 2023 Share Posted April 14, 2023 1 hour ago, jeffshead said: This is output of echo and of print_r for PHP 7.3 Then the script was fundamentally broken on PHP 7.3 as well. The difference is just that PHP 7.3 didn't complain about this mistake as hard. In 7.3 the error would trigger a warning, but your configuration probably did not show those. PHP 8 treats it as a full error now, since the code is improper. So to fix it, you need to either figure out why $return_link is an array, or change the logic to match the previous behavior. The invalid array access would have resulted in a null value on 7.3, making the str_replace do nothing so you might just be able to replace the line with this: $slfile=$directory."/".$this_file; But that is assuming the function is always used incorrectly. If $return_link is valid sometimes, then you will need to do more debugging. Quote Link to comment https://forums.phpfreaks.com/topic/316130-errors-since-updating-to-php-8/#findComment-1607320 Share on other sites More sharing options...
jeffshead Posted April 14, 2023 Author Share Posted April 14, 2023 18 hours ago, kicken said: That it still not enough information. $return_link is a function parameter, so you need find where the function is being called and determine what value it is being called with and why. So this is the PHP code that is used to display the file tree on the web page: <?php echo php_file_tree($FileLocations["tracy"], array("gif", "jpg", "jpeg", "png","pdf", "zip", "xlsx")); ?> Looks like I need to add something for the $return_link parameter, for the php_file_tree() function. Sorry, I don't even know what $return_link is supposed to be or what it is used for. If I change that line to: <?php echo php_file_tree($FileLocations["tracy"], test, array("gif", "jpg", "jpeg", "png","pdf", "zip", "xlsx")); ?> I get this error: Quote Fatal error: Uncaught Error: Undefined constant "test" in /home/whatever/index.php:143 Stack trace: #0 {main} thrown in /home/whatever/index.php on line 143 If I change that line to: <?php echo php_file_tree($FileLocations["tracy"], $return_link, array("gif", "jpg", "jpeg", "png","pdf", "zip", "xlsx")); ?> It seems to work without any errors in PHP 8.1. Quote Link to comment https://forums.phpfreaks.com/topic/316130-errors-since-updating-to-php-8/#findComment-1607324 Share on other sites More sharing options...
kicken Posted April 14, 2023 Share Posted April 14, 2023 42 minutes ago, jeffshead said: It seems to work without any errors in PHP 8.1 Unless $return_link is defined somewhere above in that script, that code is still incorrect, but like before PHP doesn't complain too hard about undefined variables. If you have nothing to pass for that parameter, try either null or the empty string (''). <?php echo php_file_tree($FileLocations["tracy"], '', array("gif", "jpg", "jpeg", "png","pdf", "zip", "xlsx")); ?> Quote Link to comment https://forums.phpfreaks.com/topic/316130-errors-since-updating-to-php-8/#findComment-1607326 Share on other sites More sharing options...
ginerjm Posted April 14, 2023 Share Posted April 14, 2023 Are you not a regular php programmer? It would seem to be true since you don't recognize what was wrong with the use of 'test' as an argument to the function call. Your use of 'test' as an argument is incorrect because your were trying to pass a word that has no meaning to php as a calling argument. You must pass a value or a variable of which the letters 'test' is neither. If you had used 'test' in quotes then it would be a string value and would work as long as the function was expecting a string value for that argument. And if you had used $test that would be a php variable and would not present a problem as long as it was the proper value (again) for the function to use. Quote Link to comment https://forums.phpfreaks.com/topic/316130-errors-since-updating-to-php-8/#findComment-1607334 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.