简体   繁体   English

PHP上传无法正常工作

[英]PHP Upload Won't Work

I made a php upload script that would be executed after a form with the upload was submitted. 我制作了一个php上传脚本,该脚本将在提交带有上载的表单后执行。

$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(). 它总是输出Error,所以我检查了php.ini文件和phpinfo()。 my php.ini shows upload_tmp_dir with no value and so does the value in phpinfo(). 我的php.ini显示没有值的upload_tmp_dir,因此phpinfo()中的值也没有。 I changed the calue of the directory to /upload, and changed the open_basedir to have a value of upload_tmp_dir. 我将目录的大小更改为/ upload,并将open_basedir更改为具有upload_tmp_dir的值。 Yet, when I look into phpinfo(), it still shows upload_tmp_dir with a local and master value of no value . 但是,当我查看phpinfo()时,它仍然显示upload_tmp_dir,其local和master值为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. 我还创建了一个/ upload文件夹,并将其权限值设置为777。但是,此问题仍然存在。 I am not sure what is causing this problem. 我不确定是什么引起了这个问题。

Try this, you must use the $_FILE super global: 试试这个,你必须使用$ _FILE超级全局:

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.";
    }

}

if upload_tmp_dir is not specified, php will use system's default tmp directory. 如果未指定upload_tmp_dir,则php将使用系统的默认tmp目录。 You can use sys_get_temp_dir() function to identify temporary directory being used by PHP. 您可以使用sys_get_temp_dir()函数来识别PHP使用的临时目录。 Have you tried printing $game_file var? 您是否尝试过打印$ game_file var? If not, try print_r($game_file) or var_dump($game_file) to view if its actually being set. 如果不是,请尝试print_r($ game_file)或var_dump($ game_file)来查看是否实际设置了它。 You can also print $_FILE to see error message ( http://php.net/manual/en/features.file-upload.errors.php ) 您还可以打印$ _FILE以查看错误消息( http://php.net/manual/zh/features.file-upload.errors.php

move_uploaded_file could fail for many reason. move_uploaded_file可能由于多种原因而失败。 Also, confirm if the destination directory exists and is writable. 另外,请确认目标目录是否存在并且可写。

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. 这是一个未定义的常量,因此PHP将“礼貌地”将其视为未引用的字符串,而您将生成"/games/game_name"

never EVER do devel/debug work in PHP with display_errors and error_reporting turned off. 永远不要做PHP的devel /调试工作,并的display_errors的error_reporting关闭。 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. $ _FILES中有一个['error']参数是有原因的。 Check it FIRST , before you start fiddling with a file that may not even be there. 检查它首先 ,你开始与可能甚至不存在文件前摆弄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM