2014-07-12 36 views
-5

好的,所以我創建了這個網站,允許用戶直接上傳文件到服務器而無需登錄。一切都很好(迄今爲止),除了一個小問題。當我登錄IP時,我希望它能打印他們上傳的用戶IP和文件名。除了文件名稱之外,一切工作。我已經檢查了每個可能的問題,並在數小時後搜索了幾小時它所能做的一切都沒有成功。請看看我有什麼,並讓我知道,如果你發現這個問題。謝謝!

如果你好奇,該網站是http://box.endurehosting.com/

<html> 

<head> 

<title>EndureBox | Upload</title> 

<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" /> 

<style type="text/css"> 

@import url("webfonts/Muli/fontstylesheet.css"); 

/*<![CDATA[*/ 

     .TitleText 
      { 
       font-family: Muli; 
       font-size: 58px; 
       font-style: normal; 
       line-height: normal; 
       font-weight: normal; 
       font-variant: normal; 
       color: #FFF; 
       text-shadow: 0px 0px 10px #707070; 
      } 

     .NormalText 
      { 
       font-family: Muli; 
       font-size: 18px; 
       font-style: normal; 
       line-height: normal; 
       font-weight: normal; 
       font-variant: normal; 
       color: #FFF; 
       text-shadow: 0px 0px 6px #999; 
      } 

     .SuccessText 
      { 
       font-family: Muli; 
       font-size: 30px; 
       font-style: normal; 
       line-height: normal; 
       font-weight: normal; 
       font-variant: normal; 
       color: #83F52C; 
       text-shadow: 0px 0px 6px #83F52C; 
      } 

     .FailedText  
      { 
       font-family: Muli; 
       font-size: 30px; 
       font-style: normal; 
       line-height: normal; 
       font-weight: normal; 
       font-variant: normal; 
       color: #FF0000; 
       text-shadow: 0px 0px 6px #FF0000; 
      } 

     .FileText 
      { 
       font-family: Muli; 
       font-size: 14px; 
       font-style: normal; 
       line-height: normal; 
       font-weight: normal; 
       font-variant: normal; 
       color: #FFF; 
       text-shadow: 0px 0px 6px #999; 
      } 

     .BlackText 
      { 
       font-family: Muli; 
       font-size: 19px; 
       font-style: normal; 
       line-height: normal; 
       font-weight: normal; 
       font-variant: normal; 
       color: #000000; 
       text-shadow: 0px 0px 3px #999999; 
      } 

    /*]]>*/ 

body {background-image:url("images/background.png");} 

</style> 

</head> 

<body> 

<div align="center"> 

</br><h1 class="TitleText">Endure Box</h1> 

<?php 
if ($_POST) 
    { 
     $folder = "box/"; 
     $redirect = "index.php?complete"; 

     move_uploaded_file($_FILES["file"]["tmp_name"], "$folder" . $_FILES["file"]["name"]); 

     header('Location: '.$redirect); 
    } 

if (isset($_GET['complete'])) 
    { 
     if ($_FILES["file"]["error"] > 0) 
      { 
       echo "<span class='notice'><p class='SuccessText'>Upload Failed!</p></span>"; 
       echo "<span class='notice'><p class='NormalText'>" . $_FILES["file"]["error"] . "</p></span>"; 
      } 
     else 
      { 
       echo "<span class='notice'><p class='SuccessText'>Upload Successful!</p></span>"; 

       $ipLog="log.txt"; 
       $friendly_name = $_FILES["file"]["name"]; 
       $register_globals = (bool) ini_get('register_gobals'); 

       if ($register_globals) 
       { 
        $ip = getenv(REMOTE_ADDR); 
       } 
       else 
       { 
        $ip = $_SERVER['REMOTE_ADDR']; 
       }   

       $date=date ("l dS of F Y h:i:s A"); 
       $log=fopen("$ipLog", "a+"); 

       if (preg_match("/\bhtm\b/i", $ipLog) || preg_match("/\bhtml\b/i", $ipLog)) 
        { 
         fputs($log, "File: $friendly_name | IP Address: $ip | Date Uploaded: $date <br>"); 
        } 
        else 
        { 
         fputs($log, "File: $friendly_name | IP Address: $ip | Date Uploaded: $date \n"); 
        } 

       fclose($log); 
     } 
    } 
?> 

<form action="index.php" method="post" enctype="multipart/form-data"> 

<p class="NormalText">Choose a file to upload</p></br> 
<input type="file" name="file" id="file" class="FileText"/></br></br></br> 
<input type="submit" name="submit" class="BlackText" value="Submit"/></br></br></br> 

</form> 

<a href="contents.php"> <img border="0" src="/images/box.png" alt="Open Box" width="200" height="200"></a></br></br></br> 

<a href="contents.php" class="NormalText">Open Box</a> 

</div> 

</body> 

</html> 
+0

它在哪裏不行? 'POST'或'GET'之後? 'POST'應該給你這個文件,'GET'不會。 – putvande

回答

0

在你的情況下,問題是以下行:

header('Location: '.$redirect); 

當你第一次運行move_uploaded_file,然後進行使用重定向header功能,$_FILES數組變空,所以在下一行簡單你不能再檢查$_FILES

另外我沒有看到任何一點使這redirectrion。當你move_uploaded_file它簡單的返回TRUE成功,因此您可以更改下面的代碼:

<?php 
if ($_POST) 
    { 
     $folder = "box/"; 
     $redirect = "index.php?complete"; 

     move_uploaded_file($_FILES["file"]["tmp_name"], "$folder" . $_FILES["file"]["name"]); 

     header('Location: '.$redirect); 
    } 

if (isset($_GET['complete'])) 

<?php 

$uploadStatus = false; 
if ($_POST) 
    { 
     $folder = "box/"; 

     $uploadStatus = move_uploaded_file($_FILES["file"]["tmp_name"], "$folder" . $_FILES["file"]["name"]); 

    } 

if ($uploadStatus) 
+0

非常感謝!你真棒!我一直在試圖弄清楚這個問題的最長時間! – Syllith