2
votes

I made a php upload script that would be executed after a form with the upload was submitted.

$game_tmp = $game_file['tmp_name'];
$game_name = $game_file['name'];
$game_dest = "/games/" . game_name;

if (move_uploaded_file($game_tmp, $game_dest)) {
    echo "<b>SUCCESS:</b><br />";
}
else {
    echo "Error.";
}

It always output Error, so I checked my php.ini file and phpinfo(). my php.ini shows upload_tmp_dir with no value and so does the value in phpinfo(). I changed the calue of the directory to /upload, and changed the open_basedir to have a value of upload_tmp_dir. Yet, when I look into phpinfo(), it still shows upload_tmp_dir with a local and master value of no value. I believe this is the problem that is stopping my upload script from working. I created a /upload folder also and gave its permission value 777. Yet, this problem persists. I am not sure what is causing this problem.

3
and tmp_name for the file gives me /tmp/php4A6B2o. It gives me /tmp/php and a random combination of numbers and letters afterwards. /tmp/php4A6B2o, in this case. - TheGodProject
Try investigate if file uploaded with errors. php.net/manual/en/features.file-upload.errors.php - Vladimir Gilevich

3 Answers

0
votes

Try this, you must use the $_FILE super global:

if($_FILES["game_file"]["error"] == UPLOAD_ERR_OK) {
    $game_tmp = $_FILES['game_file']['tmp_name'];
    $game_name = $_FILES['game_file']['name'];
    $game_dest = "/games/" . $game_name;

    if (move_uploaded_file($game_tmp, $game_dest)) {
        echo "<b>SUCCESS:</b><br />";
    }
    else {
        echo "Error.";
    }

}
0
votes

if upload_tmp_dir is not specified, php will use system's default tmp directory. You can use sys_get_temp_dir() function to identify temporary directory being used by PHP. Have you tried printing $game_file var? If not, try print_r($game_file) or var_dump($game_file) to view if its actually being set. You can also print $_FILE to see error message (http://php.net/manual/en/features.file-upload.errors.php)

move_uploaded_file could fail for many reason. Also, confirm if the destination directory exists and is writable.

0
votes

Typos:

$game_dest = "/games/" . game_name;
                         ^---you forgot a $ here

That's an undefined constant, so PHP will "politely" treat it as an unquoted string, and you're generating "/games/game_name" instead.

never EVER do devel/debug work in PHP with display_errors and error_reporting turned off. It's the equivalent of going "lalalalala can't hear you" with your fingers stuffed in your ears.

And you're also simply assuming your upload succeeded. There's a ['error'] parameter in $_FILES for a reason. Check it FIRST, before you start fiddling with a file that may not even be there.