how to read and upload text file from server in mysql database - php

i've a text file on 000webhost server with url [http://file_server.comxa.com/remote_server/temp.txt][1]
i want to read first value till delimiter,here is the code
<?php
include 'dbconfig.php';
$file = fopen ("http://file_server.comxa.com/remote_server/temp.txt", "r");
if (!$file) {
echo "<p>Unable to open remote file.\n";
exit;
}
while (!feof ($file)) {
$line = fgets ($file, 1024);
if (preg_match ("#\<title\>(.*)\</title\>#i", $line, $out)) {
$title = $out[1];
break;
}
$sql = "INSERT INTO `temprature` (`id`, `temprature`) VALUES (NULL, $line)";
$result = mysqli_query($conn,$sql) or die(mysqli_error($conn));
if($result)
{
echo "temprature inserted";
}
else
{
echo "failure";
}
}
fclose($file);
?>
but this code is throwing error
Warning: fopen(http://file_server.comxa.com/remote_server/temp.txt): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in C:\xampp\htdocs\demo\server_read.php on line 3
Unable to open remote file.
Let me know where i'm wrong
P.S: its working fine when i'm changing path to my localhost file

Related

V2: add values to a file on server Receive sensor data, publish on webpage

I want to read a php file on my web server (data to be sendt from arduino in future)
Code idea I got here:
(http://www.glacialwanderer.com/hobbyrobotics/?p=15)
I use this URL http://192.168.0.5/xPortTest.php?value0=111&value1=222
on server I have this code file:
xPortTest.php
Responce:
Failed to open file
I'm not a programmer so I need a helping hand to get this code return my values 111 and 222
Code:
<?php
if (!isset($_GET["value0"]) || !isset($_GET["value1"])) {
echo "Error: You need to pass in valid _GET parameters. Script Terminated.";
exit;
}
$value0 = $_GET["value0"];
$value1 = $_GET["value1"];
$fp = fopen("./xportTest.txt", 'at');
if (!$fp) {
echo "Failed to open file";
exit;
}
$outputstring = "$value0 $value1\r\n";
fwrite($fp, $outputstring, strlen($outputstring));
echo "\0";
?>
All I have done is added this code to the original:
if (!isset($_GET["value0"]) || !isset($_GET["value1"])) {

How to get php/html file lines with php

I have a file manager and I want to add an option of editing files (html,php,css), but if I try with fgets() it displays the page and its graphic. How to get only lines from file and then send them as response to ajax request.
This is what I tried so far:
<?php
$handle = fopen('/location/', "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
echo $line;
}
fclose($handle);
} else {
// error opening the file.
}
?>
Use
show_source("/location/file.php"); to get the source code.
You can refer it from W3School - PHP show_source() Function
If the file is on the same server you can use
$content = #file_get_contents($filename);
if($content){
echo $content;
}else{
echo 'File:"'.$filename.'" couldn\'t be found.';
}

how to upload a base64_decode($data) using ftp?

Im trying to upload a mp3 file to my ftp server:
$data = substr($_POST['data'], strpos($_POST['data'], ",") + 1);
$decodedData = base64_decode($data);
$filename = urldecode($_POST['fname']);
$cid = ftp_connect("foo.com");
$result = ftp_login($cid, "rodrigo#foo.com","password");
if ((!$cid) || (!$result)) {
echo "connection failed"; die;
} else {
echo "connected";
}
ftp_pasv ($cid, true);
ftp_chdir($cid, "my_folder");
if (ftp_put($cid, $filename, $decodedData, FTP_BINARY)) {
//...
} else {
//...
}
I have this warning:
Warning: ftp_put(���) [function.ftp-put]: failed to open stream: Invalid argument in...
I cant find out how to send a valid argument
You will need to create a file that you can pass, the below example writes the file to memory instead of a file on your disk ... but you may want to write it to disk depending on the size.
$tmp = fopen('php://memory', 'r+');
fputs($tmp, $decodedData);
rewind($tmp);
if (ftp_fput($cid, $filename, $tmp, FTP_BINARY)) {
}

PHP: fopen error handling

I do fetch a file with
$fp = fopen('uploads/Team/img/'.$team_id.'.png', "rb");
$str = stream_get_contents($fp);
fclose($fp);
and then the method gives it back as image. But when fopen() fails, because the file did not exists, it throws an error:
[{"message":"Warning: fopen(uploads\/Team\/img\/1.png): failed to open stream: No such file or directory in C:\...
This is coming back as json, obviously.
The Question is now: How can i catch the error and prevent the method from throwing this error directly to the client?
You should first test the existence of a file by file_exists().
try
{
$fileName = 'uploads/Team/img/'.$team_id.'.png';
if ( !file_exists($fileName) ) {
throw new Exception('File not found.');
}
$fp = fopen($fileName, "rb");
if ( !$fp ) {
throw new Exception('File open failed.');
}
$str = stream_get_contents($fp);
fclose($fp);
// send success JSON
} catch ( Exception $e ) {
// send error message if you can
}
or simple solution without exceptions:
$fileName = 'uploads/Team/img/'.$team_id.'.png';
if ( file_exists($fileName) && ($fp = fopen($fileName, "rb"))!==false ) {
$str = stream_get_contents($fp);
fclose($fp);
// send success JSON
}
else
{
// send error message if you can
}
You can use the file_exists() function before calling fopen().
if(file_exists('uploads/Team/img/'.$team_id.'.png')
{
$fp = fopen('uploads/Team/img/'.$team_id.'.png', "rb");
$str = stream_get_contents($fp);
fclose($fp);
}
[{"message":"Warning: fopen(uploads\/Team\/img\/1.png): failed to open stream: No such file or directory in C:\...
the error is clear: you've put the wrong directory, you can try what you whant but it'll not work. you can make it work with this:
take your file and put it in the same folder of your php file
(you'll be able to move it after don't worry, it's about your error)
or on a folder "higher" of your script (just not outside of your www
folder)
change the fopen to ('./$team_id.'png',"rb");
rerun your script file
don't forget this : you can't access a file that is'nt in your "www" folder
(he doesn't found your file because he give you her name: the name come from the $team_id variable)
Generically - This is probably the best way to do file-io in php (as mentioned by #Cendak here)
$fileName = 'uploads/Team/img/'.$team_id.'.png';
if ( file_exists($fileName) && ($fp = fopen($fileName, "rb"))!==false ){
$str = stream_get_contents($fp);
fclose($fp);
// send success JSON
}else{
// send an error message if you can
}
But it does not work with PHP 7.3, these modifications do,
if(file_exists($filename) && ($fp = fopen($filename,"r") !== false)){
$fp = fopen($filename,"r");
$filedata = fread($fp,filesize($filename));
fclose($fp);
}else{
$filedata = "default-string";
}

File from SOAP, how to save?

I am working with a client on getting a gzip from their webservice. I am able to get a response with my following call:
$response = $client->call('branchzipdata', $param);
$filename = "test.gzip";
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $response) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
Now when I attempt to write that a file, such as 'test.gzip', I am unable to open it afterwards... most likely because I am doing something horrible wrong. Any insight would be appreciated.
EDIT:
For some reason I was saving the file as '.gzip' instead of '.gz'... So in order to have it work I now have:
$response = $client->call('call', $param);
$content = base64_decode($response);
$filename = "output_zip.gz";
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $content) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
echo system("gzip -d $filename");
(Edited based on the comments)
If the return value is base64-encoded, you need to base64-decode it before you write it to the file. Alternatively you could write it out to a file which you then base64-decode to another file before trying to open it, but that seems a bit pointless compared with just decoding it when you first get it.

Categories