I have a question regarding the use of php to retrieve files which have been stored on a server.
I have been storing database files from my NSB/Appstudio application to my server by using a php file on the server. This has been working quite well. Now, I want to retrieve these files back to my app using php and I don’t know how to do it.
To store the files, I use the following code in my app:
Function btnBackupToServer_onclick()
DBjson=SQLExport(DB,“myDatabase.db”,exportComplete)
End Function
Function exportComplete()
Password="***********"
backToString=JSON.stringify(DBjson)
backToString=sjcl.encrypt(Password, JSON.stringify(DBjson))
IniFileName="DBbackup02EncryptLocalTest2"
req = Ajax(“http://###.###.#.##/xampp/saveBackUp.php”, “POST”, IniFileName & “,” & backToString)
If req.status = 200 Then
LabelBackupStatus2.textContent="Complete"
Else
LabelBackupStatus2.textContent="Failed"
End If
End Function
The php file on the server is as follows:
<?php
header('Access-Control-Allow-Origin: *');
ini_set('display_errors','1');
$backup_dir = "c:\\xampp\DBBackup\\";
// Get the data from the client.
$myText = file_get_contents('php://input');
$tmp = explode(",",$myText);
$myFile = $tmp[0];
$myFile= $backup_dir . $myFile;
echo "Data received from Device: " . $myText;
echo "
Writing data to " . $myFile;
$f = fopen($myFile, 'w') or die("can't open file");
fwrite($f, $myText);
fclose($f);
?>
I would really appreciate help in the form of sample code to place in my app as well as a sample php file for the server.
Hi, well, the first thing you need is the php file to communicate with the server and the correct query (Select from *…) by GET or POST. Try to get the answer you need (you can try with Postman). Here is an example:
<?php
$host='localhost';
$username='';
$pwd='';
$db="";
$con=mysqli_connect($host,$username,$pwd,$db) or die ('Unable to connect');
if(mysqli_connect_error($con))
{
echo "Failed to Connect to Database ".mysqli_connect_error();
}
$sql="SELECT * FROM yourtable";
$result=mysqli_query($con,$sql);
if($result)
{
$rawdata = array();
$i=0;
while($row = mysqli_fetch_array($result))
{
$rawdata[$i] = $row;
$i++;
}
echo json_encode($rawdata);
$jsonencoded = json_encode($rawdata,JSON_UNESCAPED_UNICODE);
$fh = fopen('file.json', 'w'); /if you want a file in the server
fwrite($fh, $jsonencoded);
fclose($fh);
}else
{
echo('Error');
}
mysqli_close($con);
Once you get the correct result you need to call the php file with Ajax like this:
'use this sentence on a clic button for example
req=Ajax("http://yourwebdomain/file.php", done)
Function done()
If req.status = 200 Then 'success
url = "http://yourdomain/file.json" 'The file you get from php
GetJSON(url,obt_json) 'Read the Json File
end if
end function
Function obt_json(data)
For i = 0 To UBound(data) 'each row on json
Select1.addItem(data[i].avalidtablefield) 'the result into a select
Next i
end function
Tell us your result.....
Xaris_Velas solution may be overkill. From what I can tell, you’re just copying the database to the server as a big text string - not turning it back into an SQLite database on the server.
In that case, send to back as the same file it was sent as - no processing of the data is needed.
Thank you - Yes, I just need to store the database onto the server and then load it back to the app later. I do not need to do any processing of the database on the server.