0

ファイルの形式でftpを介してあるサーバーから別のサーバーにphp配列を転送できるようにしたいと思います。

受信サーバーは、上記のファイルを開いてその内容を読み取り、提供された配列を使用できる必要があります。

サーバー1から配列のphpコードを使用してphpファイルを書き込み、次にこのファイルをサーバー2にロードするという、この2つの方法について考えました。ただし、配列の深さが不明な場合、このファイルの書き込みは難しくなります。 。

したがって、jsonでエンコードされたファイルに配列を書き込むことについては考えましたが、2番目のサーバーがどのように開いて上記のデータを読み取ることができるかわかりません。

簡単にできますか:

$jsonArray= json_encode($masterArray);
$fh = fopen('thefile.txt' , 'w');
fwrite($fh, $thePHPfile);
fclose($fh);

次に、もう一方のサーバーでデータを変数に開きます。

$data = json_decode( include('thefile.txt') );

誰かがこれまでにこれを経験したことがありますか?

4

4 に答える 4

3

最初のサーバーの場合、FTPで2番目のサーバーに接続し、そのファイルの内容をファイルに入れます

$jsonArray = json_encode($masterArray);
$stream    = stream_context_create(array('ftp' => array('overwrite' => true))); 
file_put_contents('ftp://user:pass@host/folder/thefile.txt', $jsonArray, 0, $stream);

file_get_contents()2番目のサーバーに使用:

$data = json_decode( file_get_contents('/path/to/folder/thefile.txt') );
于 2012-09-16T13:25:57.727 に答える
2

serialize()PHPを使用してファイルを読み取ることだけに興味がある場合は、とを使用することを考えましたunserialize()か?

http://php.net/manual/en/function.serialize.phpを参照してください

また、おそらくjson_encode()/よりも高速ですjson_decode()http://php.net/manual/en/function.serialize.php#103761を参照)。

于 2012-09-16T13:32:55.503 に答える
1

探しているPHP関数は次のとおりです:file_get_contents

$masterArray = array('Test','Test2','Test3');
$jsonArray= json_encode($masterArray);
$fh = fopen('thefile.txt' , 'w');
fwrite($fh, $jsonArray);
fclose($fh);

次に、他のサーバーで:

$masterArray = json_decode( file_get_contents('thefile.txt') );
var_dump($masterArray);
于 2012-09-16T13:36:24.353 に答える
1

json_encodeファイルを媒体として使用してサーバー間で配列を「転送」するには、とを使用して優れた解決策を見つけましたjson_decodeserializeand関数はunserialize、同じ目標をうまく実行します。

$my_array = array('contents', 'et cetera');

$serialized = serialize($my_array);
$json_encoded = json_encode($my_array);

// here you send the file to the other server, (you said you know how to do)
// for example:
file_put_contents($serialized_destination, $serialized);
file_put_contents($json_encoded_destination, $json_encoded);

受信サーバーでは、ファイルの内容を読み取り、対応する「解析」機能を適用するだけです。

$serialized = file_get_contents($serialized_destination);
$json_encoded = file_get_contents($json_encoded_destination);

$my_array1 = unserialize($serialized);
$my_array2 = json_decode($json_encoded);
于 2012-09-16T13:46:04.720 に答える