簡體   English   中英

無法使php文件上傳器正常工作

[英]Having trouble getting php file uploader to work

我無法讓一個簡單的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");
        }
}

發生了什么事,我在瀏覽器中收到消息“ Thanks”,但沒有文件。 我已經檢查了權限,檢查了我的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文件夾保存在哪里? 僅在文件$uploadDir="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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM