2

本质上,我要做的是获取一个 HTTP Post,并将文件保存在第一台服务器上,然后将帖子转发到第二台服务器,该服务器再次保存文件,然后生成一封电子邮件,并将文件作为附件。目前,我在将帖子发送到第二台服务器的地方工作,它会创建电子邮件并将其发送出去,所以这不是问题。

但是,当我尝试在那里添加第一台服务器时,我根本无法让它发送,或者打印出有意义的结果。这是发送/接收脚本的代码:

<?php
$uploaddir = '/home/www/myfirstsite.com/3d/';
$uploadname = basename($_FILES['file']['name']);
$uploadfile = $uploaddir . $uploadname;


if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
        echo "OK\n";
} else {
    echo "ERROR\n";
}
echo "<br>";

$url = 'http://mysecondsite.com:12345/receive_scan.php';
//I use port 12345 because most of the important ports are being used already. 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_FILES);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
$result='Result: '.curl_exec ($ch);

echo $result;
echo curlerrno($ch);
echo curlerror($ch);
curl_close ($ch);

目前,当我发送 HTTP 发布请求时,它会打印出以下内容:

OK
Result: 

我用来发送 HTTP 发布请求的代码是这样的:

<html><body>
<form enctype="multipart/form-data" action="http://myfirstsite.com/receive_scan.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="file" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

如果我只是更改站点,则接收到的第二台服务器上的代码与上面的代码一起使用,是这样的:

<?php
$uploaddir = './scans/';
$uploadname = basename($_FILES['file']['name']);
$uploadfile = $uploaddir . uploadname;

echo "File Received\n";
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
    echo "OK\n";
} else {
    echo "ERROR\n";
}
shell_exec("bash sendmail.bash ".$uploadfile." ".$uploadname);
?>

sendmail.bash 只是一个使用 msmtp 向我的电子邮件发送电子邮件的脚本。

4

1 回答 1

0

$_FILES 和 curl post 的结构不同-大约 3 年前我遇到了这个问题,花了我大约 3 天的时间来解决:

function arraysToBrackets($array, $prefix, $level=0){

    $resarr = array();
    foreach ($array as $field => $data){
        if (is_array($data)){
            $resarr = array_merge($resarr, arraysToBrackets($data,
                    ($prefix?$prefix.'['.$field.']':$field),$level+1));
        }else{
            if ($level > 0){
                $resarr[$prefix.'['.$field.']'] = $data;
            }else{
                $resarr[$prefix.$field] = $data;
            }
        }
    }
    return $resarr;
}

$postThrough = arraysToBrackets($_POST);

if ($_FILES){

    foreach ($_FILES as $file => $details){
        if (is_array($details['name'])){
            foreach ($_FILES[$file]['name'] as $file2 => $details2){
                if ($_FILES[$file]['tmp_name'][$file2]){
                    $postThrough[$file.'['.$file2.']'] = '@'.$_FILES[$file]['tmp_name'][$file2];
                }
            }
        }else{
            if ($details['tmp_file']){
                 $postThrough[$file] = '@'.$details['tmp_file'];
            }
        }
    }
}
于 2013-11-14T23:18:01.993 回答