2012-11-01 46 views
0

任何人都可以幫助我將上傳單個文件的代碼更改爲多個文件上傳?我嘗試了很多例子,似乎無法讓它起作用。頌歌如下。更改單個文件上傳到多個文件上傳在php

<tr> 
     <td>Add file:</td> 
     <td><input type="file" name="cv" size="chars" class="Form" />    
    </td> 
    </tr> 

    <?php 
    include("lib/xmlrpc.inc"); 
    include("settings.php"); 
    include("header.php"); 


$post = $_POST; 


    $client = new xmlrpc_client($serverUri.'common'); 

    $msg = new xmlrpcmsg('login'); 
    $msg->addParam(new xmlrpcval($db, "string")); 
    $msg->addParam(new xmlrpcval($user, "string")); 
    $msg->addParam(new xmlrpcval($password, "string")); 


    $res = &$client->send($msg); 

    if(!$res->faultCode()){ 

     $val = $res->value(); 
     $id = $val->scalarval(); 

     if (empty($id)){ 

      echo "Connection error = "; 
      exit; 
     } 
     else 
     { 

      $client2 = new xmlrpc_client($serverUri.'object'); 

      $val = array ("name" => new xmlrpcval($post['subject'],"string"), 
          "email_from" => new xmlrpcval($post['email'], "string"), 
          "partner_name" => new xmlrpcval($post['name'], "string"), 
          "partner_phone" => new xmlrpcval($post['phone'], "string"), 
          "description" => new xmlrpcval($post['letter'], "string"), 
          "job_id" => new xmlrpcval($post['jobid'], "int") 
         ); 

      $msg = new xmlrpcmsg('execute'); 
      $msg->addParam(new xmlrpcval($db, "string")); 
      $msg->addParam(new xmlrpcval($id, "int")); 
      $msg->addParam(new xmlrpcval($password, "string")); 
      $msg->addParam(new xmlrpcval("hr.applicant", "string")); 
      $msg->addParam(new xmlrpcval("create", "string")); 
      $msg->addParam(new xmlrpcval($val, "struct")); 


      $res2 = &$client2->send($msg); 

      if(!$res2->faultCode()) 
      { 
       $val2 = $res2->value()->scalarval(); 

       $tmp_name = $_FILES['cv']['tmp_name']; 
     $file_name = $_FILES['cv']['name']; 

       if(file_exists($tmp_name)) 
       { 
        // open the file for a binary read 
        $file = fopen($tmp_name,'rb'); 
        // read the file content into a variable 
        $data = fread($file,filesize($tmp_name)); 
        // close the file 
        fclose($file); 

        // now we encode it and split it into acceptable length lines 
        $encodedFile = base64_encode($data); 

        $valFile = array ("name" => new xmlrpcval($file_name,"string"), 
            "datas" => new xmlrpcval($encodedFile, "string"), 
            "datas_fname" => new xmlrpcval($file_name, "string"), 
            "index_content" => new xmlrpcval("true", "string"), 
            "res_model" => new xmlrpcval("hr.applicant", "string"), 
            "res_id" => new xmlrpcval($val2, "int"), 
           ); 

        $msgFile = new xmlrpcmsg('execute'); 
        $msgFile->addParam(new xmlrpcval($db, "string")); 
        $msgFile->addParam(new xmlrpcval($id, "int")); 
        $msgFile->addParam(new xmlrpcval($password, "string")); 
        $msgFile->addParam(new xmlrpcval("ir.attachment", "string")); 
        $msgFile->addParam(new xmlrpcval("create", "string")); 
        $msgFile->addParam(new xmlrpcval($valFile, "struct"));          

        $res2File = &$client2->send($msgFile); 
       } 

       echo 'Thanks for applying, Files (name of file to be added) are uploaded.<br />'; 
       echo '<a href="index.php">Back to start</a><br />'; 

      } 
      else 
      { 
       echo "<br />Problem in message sending for create application"; 

      } 
     }   
    } 
    else 
    { 
     echo "<br />Connection not established"; 
    } 

include("footer.php"); 
?> 

<input name="cv" type="file" size="chars" class="Form" /> 
+0

你似乎忘了,包括你的問題,你的代碼。 – andrewsi

+0

是的,我現在只是添加它,但有點梨形。 – JoeB

+0

你有什麼試圖將其轉換?當你嘗試時什麼都不起作用? – andrewsi

回答

0

試試這個

<!-- IMPORTANT: FORM's enctype must be "multipart/form-data" --> 
<form action="upload-handler.php.php" method="post" enctype="multipart/form-data"> 
    Send these files:<br /> 
    <input name="userfile1" type="file" /><br /> 
    <input name="userfile2" type="file" /><br /> 
    <input type="submit" value="Send files" /> 
</form> 

<?php foreach ($_FILES as $file) { ... } 

或者,如果你想從一個單一的輸入提供多個文件。

<!-- IMPORTANT: FORM's enctype must be "multipart/form-data" --> 
<form method="post" action="upload-handler.php" enctype="multipart/form-data"> 
    <input name="userfiles[]" id="userfiles" type="file" multiple="" /> 
</form> 

<?php foreach ($_FILES as $file) { ... } 
+0

我遇到的問題是與foreach()函數。你有沒有工作的例子?我嘗試了你建議的兩種方式,但仍然只能加載一個文件。 – JoeB

+0

你可以運行'var_export($ _ FILES);死;'並張貼結果?你確定這是包裹在一個表單元素? – ehime

+0

我不知道如何運行var_export($ _ FILES); die;我在哪裏添加此代碼?該表單位於另一個文件中。我可以嘗試上傳它,如果你想 – JoeB

0
$handle = fopen('D:\wamp\www\splite\uploads\hard bounce 10.txt','r'); 
     $f = 1; //new file number 
     while(!feof($handle)) 
     { 
      $newfile = fopen('D:/wamp/www/splite/batches/'.$f.'txt','w'); //create new file to write to with file number 
      for($i = 1; $i <= 5000; $i++) //for 5000 lines 
      { 
       $import = fgets($handle); 
       print_r($import); 
       fwrite($newfile,$import); 
       if(feof($handle)) 
       {break;} //If file ends, break loop 
      } 
      fclose($newfile); 
      //MySQL newfile insertion stuff goes here 
      $f++; //Increment newfile number 
     } 
     fclose($handle);`enter code here`