2012-03-17 16 views
0

私はかなり単純なファイルサーバを作成しています.1つのファイルをアップロードしてサーバ上のフォルダに移動し、その情報をデータベースに保存するとうまくいきました。今では、1つの入力フィールドから複数のファイルを受け入れるように修正しようとしたとき、エラーの最初のテストを過ぎて進んでいくことはできません。ここ複数のファイルをサーバにアップロードする際の問題

<body> 
     <img src="style/images/sitename.gif" alt="sitename" align="absmiddle" class="displayed" /> 
     <div id="sidediv"> 
      <ul> 
       <li>Multiple files uploaded at once will return a link to a zip archive of those files. 
      </ul> 
     </div><!--close the sidediv--> 
     <div id="container">   
      <div id="content">     
        <!--form starts here--> 
        <form action="upload.php" id="group" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" > 
         <p id="f1_upload_process">Loading...<br/><img src="loader.gif" /><br/></p> 
         <p id="f1_upload_form" align="center"><br/> 
          <label>File: 
            <input name="myfile[]" type="file" size="30" multiple="multiple" /> 
          </label> 
          <label> 
           <input type="submit" name="submitBtn" class="sbtn" value="Upload" multiple="multiple" /> 
          </label> 
         </p> 

         <iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe> 
        </form> 
        <!--form ends here--> 
      </div> 
      <!--<div id="footer"><a href="" target="_blank">sitename</a></div>--> 
     </div> 
     <div id="link"></div>    
</body> 

そして、私のupload.php:

は、これはここに私のindex.phpである

<?php 


    //database 
    $username=""; 
    $password=""; 
    $database=""; 
    mysql_connect(localhost,$username,$password); 
    @mysql_select_db($database) or die("Unable to select database"); 

    $message = array(); 
    $result = array(); 
    $fileName = array(); 
    $ext = array(); 
    $tmpName = array(); 
    $path = array(); 
    $target_path = array(); 

    $count = count($_FILES['myfile']['name']); 
    for($i=0;$i<$count;$i++) 
    { 
     //file info 
     $fileName[$count] = $_FILES['myfile']['name'][$count]; // Get the name of the file (including file extension). 
     $ext[$count] = pathinfo($fileName[$count], PATHINFO_EXTENSION); // Get the extension from the filename. 
     $tmpName[$count] = $_FILES['myfile']['tmp_name'][$count]; 
     $fileSize[$count] = $_FILES['myfile']['size'][$count]; 
     $fileType[$count] = $_FILES['myfile']['type'][$count]; 

     //file info 
/*  $fileName = $myfile['name']; // Get the name of the file (including file extension). 
     $ext = pathinfo($fileName, PATHINFO_EXTENSION); // Get the extension from the filename. 
     $tmpName = $myfile['tmp_name']; 
     $fileSize = $myfile['size']; 
     $fileType = $myfile['type'];*/ 

     // Edit upload location here 
     $destination_path = './files/'; 
     $allowed_filetypes = array('idx','sub','txt','srt'); 
     $max_filesize = 5242880; //bytes 

     $prefix = substr(md5(time()),0,7); //new name of the file 
     $target_path[$count] = $destination_path . $prefix .".".$ext[$count]; 

     // Check if the filetype is allowed, if not DIE and inform the user. 
     if(!in_array($ext[$count],$allowed_filetypes)){ 
      $result[$count] = 2; 
      $message[$count] = "The file you attempted to upload is not allowed.".$fileName[$count];} 

     // Now check the filesize, if it is too large then DIE and inform the user. 
     else if(filesize($_FILES['myfile']['tmp_name'][$count]) > $max_filesize){ 
      $result[$count] = 3; 
      $message[$count] = "The file you attempted to upload is too large.";} 

     else if(!file_exists($destination_path)){ 
      $result[$count] = 4; 
      $message[$count] = "The upload path does not exist";} 

     // Check if we can upload to the specified path, if not DIE and inform the user. 
     else if(!is_writable($destination_path)){ 
      $result[$count] = 5; 
      $message[$count] = "You cannot upload to the specified directory, please CHMOD it to 777.";} 

     else 
     {  
      @move_uploaded_file($tmpName[$count], $target_path[$count]); 

      $file_info = pathinfo($fileName[$count]); 
      $sql = "INSERT INTO Files SET 
         uploader_ip = '".$_SERVER['REMOTE_ADDR']."', 
         File_Name = '".$fileName[$count]."', 
         File_Type = '".$fileType[$count]."', 
         File_Size = '".$fileSize[$count]."', 
         File_Hash = '".$prefix.".".$ext[$count]."', 
         File_Extension = '".$file_info['extension']."'"; 

      $sqlresult = mysql_query($sql);  
      // If the query was successful, give success message 
      if(!$sqlresult){ 
       $result[$count] = 6; 
       $message[$count] = "Could not add this file.";//not actually displayed 
       exit; 
      } 
      else{ 
       $message[$count] = "New file successfully added.";//not actually displayed 
       $result[$count] = 1; 
       $path[$count] = 'Your file upload was successful, view the file <a href="' . $target_path[$count] . '" title="Your File">here</a>'; 
      } 

     }//closes last else (all the writing to the db) 
    } 
    sleep(1); 
?> 

<script language="javascript" type="text/javascript">window.top.window.stopUpload(
<?php echo json_encode($result[$count]); ?>, 
<?php echo json_encode($message[$count]); ?>, 
<?php echo json_encode($path[$count]); ?>, 
<?php echo json_encode($count); ?>, 
<?php echo json_encode($fileName[$count]); ?>, 
<?php echo json_encode($ext[$count]); ?>); 
</script> 

たびに、私はエラーを取得しています「アップロードしたファイルは許可されていません」ときそのテストに合格するはずです。どんな助けでも大歓迎です。

答えて

0

forループ内の配列に$countを使用しているところなら、$countの代わりに$iを使用する必要があります。

$countは常に同じであり(アレイの境界外にあります)

これ以上お試しください。

+0

私の主よ、私は自分自身を蹴る準備ができています。私はあなたが正しいと確信しています。私はそれを試して、あなたに戻ってきます。 –

+0

さて、私は行って、$ iを使いました。 $ countを使うのは間違っていましたが、今や変数のどれも初期化されていないようです。 1つのファイルだけを入力してsubmitを押すと、upload.phpがフォームに送信しているレスポンス: ' ' –

+0

複数のファイルが存在する可能性があるので、' stopUpload' javascript呼び出しをループ内に置く必要があるかもしれません。その部分を '$ count'のままにしたり、' $ i'に変更しましたか?ループの外側では、 '$ i'はco​​untに等しくなります。 – drew010

関連する問題