【问题标题】:Having trouble getting php file uploader to work无法让 php 文件上传器正常工作
【发布时间】:2014-08-20 08:22:07
【问题描述】:

我无法让一个简单的 php 文件上传脚本正常工作。发生的情况是我在浏览器上收到了感谢消息,但该文件在任何地方都看不到。

我一直在使用 inotify 观察上传目录和 /tmp 以查看是否创建了任何内容,但没有创建任何内容。这是允许选择文件的 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");
        }
}

我在浏览器中收到“谢谢”消息,但没有文件。我检查了权限,检查了我的 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/

【问题讨论】:

  • /tmp/ 文件夹是否存在于您的 php 文件所在的同一位置?

标签: php upload


【解决方案1】:

它对我来说很好用。你能检查上传目录吗?您将 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多