无法让php文件上传器工作

时间:2014-08-20 08:22:08

标签: php upload

我无法使用简单的php文件上传脚本。发生的事情是我在浏览器上收到了感谢信息,但该文件无处可见。

我一直在观看上传目录和/ tmp with inotify以查看是否有任何内容被创建,但没有任何内容。这是允许选择文件的html div:

<div>
     <p> Select a file </p>
          <form id="support" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                   <label for="file">Filename:</label>
                   <input type="file" name="logfile" id="file"><br/><br/>
                   <input type="submit" name="submit" value="Upload">
           </form>
</div>

然后我尝试上传文件:

$uploadDir = "/tmp/";


if(isset($_FILES['logfile']['name'])){

        $uploadFile = $uploadDir . basename($_FILES['logfile']['name']);

        print "<p>{$_FILES['logfile']['name']}</p>\n";
        print "<p>{$uploadFile}</p>\n";

        # Delete if already exists
        if (file_exists($uploadFile)) {
                unlink($uploadFile);
        }

        // Recieve the file
        if (move_uploaded_file($_FILES['logfile']['tmp_name'], $uploadFile)) {
                displayMessage("Thanks");
        }
        else {
                # Couldnt move the file
                displayMessage("Error moving file");
        }
}

我收到的消息是什么?#34;谢谢&#34;在我的浏览器但没有文件。我检查了权限,检查了我的nginx日志 - 完全没有错误。看起来它是成功的但没有被复制?

编辑:

即使失败了:

$myfile = fopen("/tmp/testfile.txt", "w") or die("Unable to open file!");
$current = "John Smith\n";
fwrite($myfile, $current);
fclose($myfile);

基本上我出于某种原因无法写入/ tmp,即使nginx没有报告任何错误。我也在apache下尝试了相同的脚本,结果相同。

drwxrwxrwt 18 root root 460 Aug 20 10:56 /tmp/

1 个答案:

答案 0 :(得分:0)

它对我来说很好。你能查一下上传目录吗?你在哪里保存了你的tmp文件夹?只需使用文件$uploadDir="tmp/";

的相同位置
<?php
$uploadDir = "";
if(isset($_FILES['logfile']['name'])){

    $uploadFile = $uploadDir . basename($_FILES['logfile']['name']);

    print "<p>{$_FILES['logfile']['name']}</p>\n";
    print "<p>{$uploadFile}</p>\n";

    # Delete if already exists
    if (file_exists($uploadFile)) {
       unlink($uploadFile);
    }
    // Recieve the file
    if (move_uploaded_file($_FILES['logfile']['tmp_name'], $uploadFile)) {
            echo "Thanks";
    }
    else {
            # Couldnt move the file
            echo "Error moving file";
    }
}
?>
<div>
 <p> Select a file </p>
      <form id="support" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
               <label for="file">Filename:</label>
               <input type="file" name="logfile" id="file"><br/><br/>
               <input type="submit" name="submit" value="Upload">
       </form>
</div>