0

私のphpcurlアップロードスクリプトはローカルホストでは正常に実行されますが、サーバーでは実行されません。curlは単純な投稿では正常に機能しますが、ファイルの投稿では機能しません。投稿データにファイルを追加し始めると(ファイルパスの前に@を付けます)、ファイルがないと$ _POSTが入力されますが、サーバーには何も表示されません($_FILEと$_POSTの両方が未設定であることがわかります)。ローカルホストとサーバーでも次のスクリプトを使用しています。

$request_url = 'http://localhost/curl_upload/curl_upload_process.php';
$post_params['uploadfile'] = '@'.'D:\images\photo-b4.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
$result = curl_exec($ch);
curl_close($ch);

サーバー上のrequest_urlをcurl_upload_process.phpファイルに変更しました。単純な投稿とファイルのアップロードの両方でローカルホストで正常に動作しますが、ファイルのアップロードのみのサーバーでは正常に動作しません。サーバー上のスクリプトの問題の原因を教えてください。

4

2 に答える 2

1

こんにちは、よく調べた結果、SSH(Secure SHell)プロトコルを使用するscpコマンドを使用できることがわかりました。phpには、phpコードでそれを使用するためのライブラリがありますが、scp接続を許可するサーバーが必要です

<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
?>

詳細については、scp+phpをご覧ください。これがお役に立てば幸いです :) . 必要に応じて変更してください

于 2013-01-25T12:16:10.787 に答える
0

@'.'D:\images\photo-b4.jpg';問題は、サーバー内のコードが実行されているときに、マシンからではなく、ファイルをファイリングしていることだと思います

localhostの場合と同様に、マシンとサーバーは同じであるため、物理的に場所にあるファイルを見つけます@'.'D:\images\photo-b4.jpg';

ファイルをアップロードする場合は、実行時に Web フォーム全体のリクエストを作成し、それをサーバーに投稿する必要があります。

このようなことを試してください:-それに応じて少し変更する必要がある正確なコードではありません

    $requestparameters["title"] = $filetitle;

    $content = file_get_contents($_FILES['uploadingfile']['tmp_name']);
    $filefieldname = (array_keys($_FILES));
    $delimiter = '-------------' . uniqid();
    $filefields = array(
    'file1' => array(
    'name' => $_FILES['uploadingfile']['name'],
    'type' => $_FILES['uploadingfile']['type'],
    'content' => $content),
    );
    $data = '';
    foreach ($requestparameters as $name => $value) {
        $data .= "--" . $delimiter . "\r\n";
        $data .= 'Content-Disposition: form-data; name="' . $name . '";' . "\r\n\r\n";
        // note: double endline
        $data .= $value . "\r\n";
    }
    foreach ($filefields as $name => $file) {
        $data .= "--" . $delimiter . "\r\n";
        // "filename" attribute is not essential; server-side scripts may use it
        $data .= 'Content-Disposition: form-data; name="' . $filefieldname['0'] . '";' .
        ' filename="' . $file['name'] . '"' . "\r\n";
        // this is, again, informative only; good practice to include though
        $data .= 'Content-Type: ' . $file['type'] . "\r\n";
        // this endline must be here to indicate end of headers
        $data .= "\r\n";
        // the file itself (note: there's no encoding of any kind)
        $data .= $file['content'];
   }
   $data .= "\r\n"."--" . $delimiter . "--\r\n";
   $str = $data;
   // set up cURL
   $ch=curl_init($url);
    curl_setopt_array($ch, array(
       CURLOPT_HEADER => false,
       CURLOPT_RETURNTRANSFER => true,
       CURLOPT_POST => true,
       CURLOPT_HTTPHEADER => array( // we need to send these two headers
            'Content-Type: multipart/form-data; boundary='.$delimiter,
            'Content-Length: '.strlen($str)
       ),
       CURLOPT_POSTFIELDS => $data,
   ));
   $ress =   curl_exec($ch);
   curl_close($ch);
于 2013-01-25T06:34:25.420 に答える