0

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.

n00b007
  • 11
  • 1

1 Answers1

0

I have printed $ext variable in order to see what it stores and why is triggering the error and it seems that somehow it catches the whole name of the file that is uploaded as the extension instead of getting just the word after the "." for example if the uploaded file is named NoobvsPro1.jpg the $ext variable records the extension of the file to be "NoobvsPro1.jpg" instead of just ".jpg"

I have printed the info storred inside $ext right in the Error message using the following code:

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."s ".$ext." 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;

Error looks like this in the frontend:
ERROR: The provided icon1 NoobvsPro1.jpg 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.
$fileName - returns icon1
$ext - returns NoobvsPro1.jpg

Great help from Deadooshka the solution was to use this line of code:

$ext = preg_match('/\.(\w+)$/', $file2, $m) ? $m[1] : '';
n00b007
  • 11
  • 1