0

For a game that I am an admin on, the maps upload page will always throw the "There was an error with your upload. Please try again." error. I did not write the code, but am in a position to to fix some things (all of the devs are no where to be found to try to help fix the problem). If the code is not an issue, what should I be looking at to try and solve the issue? Thanks!

Code for the page:

<?php
if (!isset($_SESSION['User'])) // we don't want guests to upload maps
{
    header("Location: /account");
    exit(0);
}

    // Handle map file upload
    $errormessage = "";
    if (isset($_FILES['mapfile']) and $_FILES['mapfile']['name'] != "")
    {
        if ($_FILES['mapfile']['error'] != 0 or $_FILES['mapfile']['type'] != "application/octet-stream" or $_FILES['mapfile']['size'] < 1 or $_FILES['mapfile']['size'] > 64000)
        {
            $errormessage = "There was an error with your upload.  Please try again.";
        } else
        {
            $_FILES['mapfile']['name'] = filter_var($_FILES['mapfile']['name'], FILTER_SANITIZE_STRING);
            $_FILES['mapfile']['name'] = str_replace(" ", "_", $_FILES['mapfile']['name']);
            $_FILES['mapfile']['name'] = str_replace("(", "", $_FILES['mapfile']['name']);
            $_FILES['mapfile']['name'] = str_replace(")", "", $_FILES['mapfile']['name']);
            if (strpos($_FILES['mapfile']['name'], "ACG") !== false or preg_filter("/[\d|\w|\.|\-|\(|\)]/i", "", $_FILES['mapfile']['name']) != "" or substr(strtolower($_FILES['mapfile']['name']), -4, 4) != ".map")
            {
                $errormessage = "Invalid map filename.";
            } else
            // if (file_exists("public/maps/".$_FILES['mapfile']['name']))
            // {
            //     $errormessage = "Map filename already exists in filesystem.";
            // } else
            {
                try
                {
                    $map = new Map($_FILES['mapfile']['tmp_name']);
                } catch (Exception $e)
                {
                    $errormessage = "File is not a valid map: ".$e->getMessage();
                }
            }
            if ($errormessage == "")
            {
                $mapquery = DB::selectArray("SELECT * FROM maps WHERE Mapfile='{$_FILES['mapfile']['name']}' LIMIT 1");
                if (sizeof($mapquery) > 0) DB::query("DELETE FROM maps WHERE Mapfile='{$_FILES['mapfile']['name']}' LIMIT 1");

                $newmap = [];
                $newmap['Mapfile'] = $_FILES['mapfile']['name'];
                $newmap['Mapname'] = $map->mapinfo['name'];
                $newmap['Gamemode'] = $map->mapinfo['gamemode'];
                $newmap['DescriptionRaw'] = $map->mapinfo['description'];
                $newmap['Description'] = $map->generateDescription(str_replace(".map", "", $_FILES['mapfile']['name']));
                $newmap['Numteams'] = $map->mapinfo['numteams'];
                $newmap['Mapsize'] = $map->mapinfo['tilecount'];
                $newmap['Flagcount'] = $map->mapinfo['neutralflags'] + $map->mapinfo['greenflags'] + $map->mapinfo['redflags'] + $map->mapinfo['blueflags'] + $map->mapinfo['yellowflags'];
                $newmap['Switchcount'] = $map->mapinfo['switches'];
                $newmap['Turretcount'] = $map->mapinfo['turrets'];
                $newmap['Warpcount'] = $map->mapinfo['warps'];
                $newmap['Views'] = 0;
                $newmap['Downloads'] = 0;
                $newmap['Votes'] = 0;
                $newmap['LastPlayed'] = null;
                if (DB::saveArray("maps", $newmap))
                {
                    if (move_uploaded_file($_FILES['mapfile']['tmp_name'], "public/maps/".$_FILES['mapfile']['name']))
                    {
                        chmod("public/maps/".$_FILES['mapfile']['name'], 0755);
                        imagepng($map->createImage(), "public/maps/images/".str_ireplace(".map",".png",$_FILES['mapfile']['name']));
                        imagepng($map->createCroppedImage(), "public/maps/croppedimages/".str_ireplace(".map",".png",$_FILES['mapfile']['name']));
                        $errormessage = "Map successfully added to database.<br/><br/><a href='/maps/?map={$_FILES['mapfile']['name']}'>{$_FILES['mapfile']['name']}</a> ";

                        function getmaplist()
                        {
                            $maps = [];
                            $mapPaths = glob("public/maps/*.map");
                            foreach ($mapPaths as $map)
                            {
                                $map = basename($map);
                                $maps[$map] = substr($map, 0, -4);
                            }
                            sort($maps, SORT_NATURAL | SORT_FLAG_CASE);
                            return $maps;
                        };

                        Cache::put("list of maps", getmaplist());

                    } else
                    {
                        $errormessage = "Error uploading map file.";
                    }
                } else
                {
                    $errormessage = "Error adding map to database.";
                }
            }
        }
    }
?>


<?php require_once("pages/header.php"); ?>

<div id='mapuploadpage'>
    <form action="/mapupload" method="POST" enctype="multipart/form-data">
        <h1>Upload a Map</h1>
        <input type="hidden" name="MAX_FILE_SIZE" value="64000" />
        <input type="file" name="mapfile" id="mapfile" />
        <input type="submit" name="upload" id="uploadbutton" value="Upload" />
        <div id="uploadmessage"><?php echo $errormessage; ?></div>
    </form>
</div>

<?php require_once("pages/footer.php"); ?>
  • 1
    A good first port of call is to check out [**Where does PHP Store the error logs**](https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php5-apache-fastcgi-cpanel) and read the error logs and fix the issues accordingly – Martin May 22 '21 at 19:18
  • After you have found the error log file you should explore what version of PHP is running on the server. add the line `error_log("PHP Version: ".print_r(phpversion(),true));` to your page and reload the page before reviewing the error log file. – Martin May 22 '21 at 19:28

2 Answers2

1

As you can see in the code,

if ($_FILES['mapfile']['error'] != 0 or $_FILES['mapfile']['type'] != "application/octet-stream" or $_FILES['mapfile']['size'] < 1 or $_FILES['mapfile']['size'] > 64000)
{
    $errormessage = "There was an error with your upload.  Please try again.";
}

This is the condition which is throwing the error. So based on this condition, You can check step-by-step

  1. What is the value of $_FILES['mapfile']['error']
  2. Is the map file which is getting upload is of type application/octet-stream or not
  3. The uploading map file size

One of them is the reason for failing. If uploading file is a image file in png/jpeg format which image/png or image/jpeg rather then application/octet-stream, then it's better to add check for these two mime-types as well

Haridarshan
  • 1,898
  • 1
  • 23
  • 38
0

Check if server file upload size is more than you are trying to upload ! You can check these settings in php.ini file

sid
  • 11
  • 1
  • 3
  • Thanks for your reply, I will look into that. I must also mention that I was recently able to upload files to this page using an older version of Microsoft Edge but no other browsers will work. – John Albert May 22 '21 at 19:23
  • @JohnAlbert if there is a browser issue then it will not be a PHP issue. PHP does not run on the browser. – Martin May 22 '21 at 19:27
  • Try to hit the PHP file with postman and see what error it throws and make sure display_error of PHP is on – sid May 23 '21 at 15:26