2017-03-30 25 views
-2
私はフォームを使用してWebへのArduinoのからユニットIDを送信する必要が

は、送信ファイルウェブにGETとして送信されたデータを保存するためにPHPを必要

 http://somdomain.com/path/phpscript.php?ID=5 

Arduinoのコードはスラムダンクであるが、私は助けが必要サーバー側のデータを受け入れ、後で解析するためにid_file.txtという名前のテキストファイルにのみID番号を保存するPHPスクリプト。

there are 20 units and i need 1 ID number/line 

ファイルの構造にも迷っています。

できるだけお手伝いください。

+0

は、このすべては、私は両方の答えのハイブリッドで終わった – user1213320

答えて

0

あなたが

を動作するはず同じファイルに新しい行を書きたい場合はその後phpscript.phpこの

<?php 
    //first line just checks to see if ID is set and if so it'll run the script. This stops the script from running if ID isn't set and someone just randomly comes to the webpage 
    if (isset($_GET['ID'])) { 

    $id = $_GET['ID'] . "\n"; //I added \n to denote a new line so when you write to this file it'll just append the ID's there 
    $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); 
    fwrite($myfile, $id); //the second parameter is whatever you want to write to that file 
    fclose($myfile); 
    } 

のようになります。

http://somdomain.com/path/phpscript.php?ID=5 

のようなArduinoの呼び出しにページを作ります

+0

作業私を得た、ありがとうございました。 echo文は必要ありませんが、fopenをwriteからappendに変更する必要がありました。皆さん、ありがとうございました – user1213320

0

次のコードを使用してファイルに書き込むことができます。

$filename='myfilename.txt'; 
if(isset($_GET['ID'])){ 
    $id= $_GET['ID']; 
    if(is_numeric($id)){ 
     if(file_put_contents($filename,$id,FILE_APPEND)!== FALSE){ 
      echo "id successfully saved"; 
     }else{ 
      echo "there is a error saving your id"; 
     } 
    } 
} 

後に、ファイルから読み取ることができます。..

$filearray = file($filename,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 
foreach($filearray as $id){ 
    echo $id;// this print the id's 
} 
関連する問題