Hello I'm upgrading a script my website uses from php 5.4 to php 7.4 and I have some issues with it: I need to switch it from ereg_replace to preg_replace yet for some reason it won't work the desired way.
here is the original ereg_replace use in the function below:
function uploadFile($file,$file2,$oldFile,$fileName,$gameName,$redir) {
$dupFix = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz_1234567890-ABCDEFGHIJKLMNOPQRSTUVWXYNZ"),0,5);
$ext = preg_replace("/ ^.+\\.([^.]+)$ /", " \\1 ", $file2);
if ($ext != "swf" && $ext != "dcr" && $ext != "3gp" && $ext != "asf" && $ext != "asx" && $ext != "avi" && $ext != "mov" && $ext != "unity3d" && $ext != "mpg" && $ext != "qt" && $ext != "rm" && $ext != "wmv" && $ext != "bmp" && $ext != "gif" && $ext != "jpeg" && $ext != "jpg" && $ext != "png" && $ext != "JPG" && $ext != "BMP" && $ext != "GIF" && $ext != "JPEG" && $ext != "PNG") {
$_SESSION['status'] = "ERROR: The provided ".$fileName." file extension is not in the list of supported extensions. This is a security measure to help ensure malicious files don't find their way on your server.";
header("Location: $redir");
exit;
}
if ($fileName == "game file") {
$newName = seo_str_plain($gameName).".".$ext;
$sendTo = "../content/games/";
} elseif ($fileName == "icon1") {
$newName = seo_str_plain($gameName)."-icon-1.".$ext;
$sendTo = "../content/icons/";
} elseif ($fileName == "icon2") {
$newName = seo_str_plain($gameName)."-icon-2.".$ext;
$sendTo = "../content/icons/";
} elseif ($fileName == "icon3") {
$newName = seo_str_plain($gameName)."-icon-3.".$ext;
$sendTo = "../content/icons/";
}
if ($oldFile == $newName) {
} elseif (file_exists($sendTo.$newName)) {
$newName = $dupFix."_".$newName;
} elseif ($oldFile != "") {
$deleteOldFile = 1;
}
if (!move_uploaded_file($file, $sendTo.$newName)) {
$_SESSION['status'] = "Failed to copy ".$fileName." to server";
header("Location: $redir");
exit;
} else {
if ($deleteOldFile == 1) {
unlink($sendTo.$oldFile);
}
return $newName;
}
}
if I just replace the $ext = ereg_replace("^.+\\.([^.]+)$", "\\1", $file2);
part to $ext = preg_replace("/ ^.+\\.([^.]+)$ /", "\\1", $file2);
, it won't trigger a php error anymore, yet when completing the task of pressing the button to upload a file, a jpg picture to be more precise it will trigger the warning that the extension for the file I'm trying to upload is not in the list of supported extensions even though ofcourse it's a jpg and it is inside the list.