将单个文件上传到php中的多个文件上传

时间:2012-11-01 16:17:02

标签: php post xml-rpc openerp

任何人都可以帮我改变我的代码,将单个文件上传到多个文件上传吗?我尝试了很多例子,似乎无法让它发挥作用。颂歌。

    <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" />

2 个答案:

答案 0 :(得分: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) { ... }

答案 1 :(得分: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`