2011-08-12 41 views
0

有誰能夠告訴我,什麼是錯的這個代碼,我收到以下錯誤POST問題 - 未定義指數

Notice: Undefined index: media_type in C:\wamp\www\VirtualLibrary\upload_file.php on line 10 

Notice: Undefined index: file in C:\wamp\www\VirtualLibrary\upload_file.php on line 14 

Notice: Undefined index: file in C:\wamp\www\VirtualLibrary\upload_file.php on line 20 

Notice: Undefined index: file in C:\wamp\www\VirtualLibrary\upload_file.php on line 22 

這裏是我的代碼

<html> 
    <head> 
     <title>Virtual Library</title> 
     <link href="css/login.css" rel="stylesheet"/> 
     <link href="css/style.css" rel="stylesheet"/> 
     <script type="text/javascript" src="scripts/ajax_search.js"></script> 
    </head> 
    <body> 
     <div id="wrapper"> 
      <div id="header" align="center"> 
       <ul align="center"> 
        <li><a href="index.php"><span>Home</span></a></li> 
        <li><a href="news.html"><span>Top 100 Downloads</span></a></li> 
        <?php if(isset($_SESSION['login_user'])) 
        { 
         $uname = $_SESSION['login_user']; 
         echo "<li><a href='upload.php'><span>Upload</span></a></li>"; 
        }?> 
        <?php if(isset($_SESSION['login_user'])) 
        { 
         $uname = $_SESSION['login_user']; 
         echo "<li><a href='profile.php'><span>$uname</span></a></li>"; 
         echo "<li><a href='logout.php'><span>LogOut</span></a></li>"; 
        } 
        else{ 
          echo "<li><a href='login.php'><span>Member Login</span></a></li>"; 
          echo "<li><a href='register.php'><span>Register</span></a></li>"; 
         } 
        ?> 
        <li><a href="downloads.html"><span>RSS <img src="images/rss.gif" id="rss"></span></a></li> 
       </ul> 
      </div> 
      <div class='frmdiv'> 
           <form action='upload_file.php' method='post' enctype='multipart/form-data'> 
           <table width='38%' border='0' align='center' cellpadding='0' cellspacing='0'> 
           <tr> 
            <td colspan='2'><div align='center'><font size='2' face='verdana'>Upload File</font></div></td> 
           </tr> 
           <tr> 
            <td>&nbsp;</td> 
            <td>&nbsp;</td> 
           </tr> 
           <tr> 
            <td colspan='2'><hr></td> 
           </tr> 
           <tr> 
            <td height='26'><font size='2' face='verdana'>FileName&nbsp;</font></td> 
            <td><font size='2' face='verdana'> 
            <input type='file' name='file' ></font> 
            </td> 
           </tr> 
           <tr> 
            <td>&nbsp;</td> 
            <td>&nbsp;</td> 
           </tr> 
           <tr> 
            <td></td> 
            <td><select name='media_type' > 
            <option value='' selected>Select a media type...</option> 
            <option value='pdf'>PDF</option> 
            <option value='chm'>CHM</option> 
            <option value='epub'>EPUB</option> 
            <option value='html'>HTML</option> 
            <option value='djvu'>DJVU</option> 
            </select></td> 
           </tr> 
           <tr> 
            <td>&nbsp;</td> 
            <td>&nbsp;</td> 
           </tr> 
           <tr> 
            <td>&nbsp;</td> 
            <td><font size='2' face='verdana'> 
            <input type='submit' name='Submit' value='Upload'></font> 
            </td> 
           </tr> 
           <tr> 
            <td colspan='2'><hr></td> 
           </tr> 
           <tr> 
            <td>&nbsp;</td> 
            <td>&nbsp;</td> 
           </tr> 
           </table> 
           </form> 
           <div align='center'> 
        } 


     </div> 
    </body> 
</html> 

這裏是upload_file.php

<?php 
    require('include/connection.php'); 
    session_start(); 
?> 
<?php 
foreach ($_POST as $var => $value) { 
    echo "$var = $value<br>n"; 
} 

$meditype = $_POST['media_type']; 



       if ($_FILES["file"]["error"] > 0) 
        { 
         echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; 
        } 
       else 
        { 
         if (file_exists($meditype . "/" . $_FILES["file"]["name"])) 
            { 
             echo $_FILES["file"]["name"] . " already exists. "; 
            } 
          else 
           { 
       move_uploaded_file($_FILES["file"]["tmp_name"], $meditype . "/" . $_FILES["file"]["name"]); 

           } 
           } 




?> 
+0

有10號線 – genesis

+0

@genesis沒有PHP代碼 - 這是因爲另一個文件中的代碼被張貼替代,upload_file.php的。 – RobB

+0

文檔中的任何php代碼片段都不會使用警告消息中指示的數組索引。這塊html/php不能生成這些警告。 –

回答

2

您不檢查是否存在發佈數據。使用isset來做到這一點。防爆

if (isset($_POST['media_type'])) { 
    // Media type field was posted 
}