How can I change create_function to anonymous function?
I am in the lengthy process of modernizing a lot of code on my website to make it php 8.x compliant. I have never really gotten the hang of user defined functions, so the following code was not created by me.
I am trying to change create_function, which has been removed as of php 8.0, to an anonymous function.
I removed the security lines as they were irrelevant.
Any hints as to how I should proceed?
Code:
print_r($_FILES)
##produces
$_FILES is Array ( [pix] => Array ( [name] => christmas every day.jpg [type] => image/jpeg [tmp_name] => /tmp/phpWPefKh [error] => 0 [size] => 91284 ) )
Here is the outdated code:
Code:
<?php
$end=substr($_FILES['pix']['name'],-3,3);
$end=strtolower($end);
if($end=="jpg"||$end=="peg"||$end=="gif"||$end=="png") {}
else {echo"<script type=\"text/javascript\">
window.location = \"http://www.animeviews.com\"
</script>";
exit;
}
function or_f($a, $b) {
return $a || $b;
}
function file_has_extension($fn, $ext) {
if(is_array($ext))
return array_reduce(array_map(create_function('$a', 'return file_has_extension(\'' . $fn . '\', $a);'), $ext), 'or_f', false);
else
return stripos($fn, '.' . strtolower($ext)) === strlen($fn) - strlen($ext) + 1;
}
$image_extensions = array(
'png',
'jpg',
'jpeg',
'gif'
);
if(!isset($_POST['Upload']) and !isset($_POST['Overwrite']))
{
include("picform.php");
} # endif
else
{
if($_FILES['pix']['tmp_name'] == "none")
{
echo "<b>File did not successfully upload. Check the
file size. File must be less than 500K.<br>";
include("picform.php");
exit();
}
if(file_has_extension($_FILES['pix']['name'], $image_extensions))
{
echo "<b>File is not a picture. Please try another
file.</b><br>";
include("picform.php");
exit();
}
else
{
$destination = $_FILES['pix']['name'];
$destination = strtolower($destination);
$temp_file = $_FILES['pix']['tmp_name'];
if (file_exists($destination) and $_POST['Overwrite']!="Overwrite") {
echo "The file $destination already exists.";exit();
}
#########
move_uploaded_file($temp_file,$destination);
echo "<p><b>The file has successfully uploaded:</b>
{$destination}
({$_FILES['pix']['size']})</p>";
$img="http://www.animeviews.com/images/$destination";
$img = $destination;
$width = "180px";
$height = "";
// Get new dimensions
list($width1, $height1) = getimagesize($img);
if ($width =="") {$j=$height/$height1; $width = $width1 * $j;}
if ($height=="") {$j=$width/$width1; $height = $height1 * $j;}
$new_width = $width;
$new_height = $height;
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($img);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width1, $height1);
// Output
$img=preg_replace('/h.*\//','',$img);
$img=strtolower($img);
$dest="thumbs/$img";
imagejpeg($image_p,$dest);
}
}
?>