How to create a PHP script that would create a file.txt on the local pc through an IP address, there is a given IP address and a port which I can pass through.
How could I achieve this?
Any help will be appreciated, thank so much.
You can either, FTP it to the local machine, or simply let a person download it as a file. The second solution is the easiest, but requires someone on the local machine to initiate the download. The first solution can be automated, but requires you to set up an FTP server on the local machine.
Header will help you output as a download for the client.
http://php.net/manual/en/function.header.php
FTP will help you upload through FTP.
http://php.net/manual/en/book.ftp.php
on linux:
cat >dooda.php <<EOF
#!/usr/bin/env php
<?php
file_put_contents('file.txt',file_get_contents("http://example.com/of_some_url.txt"));
EOF
php dooda.php
Related
Recently I have been trying to work with my ESP. I have set up a server using XAMPP on my laptop. I can access that server using 127.0.0.2. I know that the content viewed on this page is present in the htdocs folder in XAMPP.
So I have created a folder named TEST in htdocs. The ESP is connected to PSoC and is sending some data at regular intervals to the TEST folder on the server. My question is: what is the IP address that will be needed to connect to the server when I use the AT+CIPSTART command on the ESP side?
Is it 127.0.0.2 or some other IP from the router? I have tried sending data to ThingSpeak before and there they provide a ready-made GET request link to send data to the server. What will be the GET request link in my case if the server is created using XAMPP? Please help as I am new to networking. Thank you.
Ok. I wrote a php file which takes value in a variable SAP and writes into a text file which is stored in the ht docs folder. The php code is as follows
'
$content = "SAP ID :".$var1." is present for the lecture \r\n";
echo $content;
echo "<br >";
$status = file_put_contents('attendance_record.txt',$content,FILE_APPEND);
if ($status != false)
{
echo "Data is written to the file :p ";
}
else
{
echo "Data was not written into the file :( ";
}
?>'
After then I enable the Xampp server and access this file by using 'http://127.0.0.2/receiver.php?SAP=104' Until this point everything is working fine and the text file is being updated. Now the same thing is needed to be done via AT commands using the ESP. The following lines of code is running on the ESP side.
AT+CWJAP="SSID","PASSWORD"
AT+CIPSTART="TCP","192.168.0.104",80
AT+CIPSEND=35
GET /receiver.php?SAP=69 HTTP/1.1
Now i'm not getting any updates on the text file. Any help will be appreciated. Thank you.
172.0.0.2 is the same at calling 'localhost' so you would need your laptop's IP for the ESP to connect to. Depending on your OS you can find that opening your terminal and write ipconfig(windows) or ifconfig(linux) On a MAC go to System Preferences > Network and your IP is displayed under'Status:'
I want to send variable from android application to wamp server and retrieve data from server. Please suggest me and what is path of localhost for wamp server. I am using "localhost/TestAndroid/check.php". The error is occurred data is not parsed.
Please suggest me with code. How to accessing data from android application(JSON+Android+HTTPClient)?.
Thanks
Nitin
Use this localhost address in your code '10.0.2.2/TestAndroid/check.php'
For more Help:
http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
Find the below link, it may help full
WAMP server
Follow these steps.
Make sure your PC and Phone are in the same network (Connected to the same wi-fi)
Open a command prompt and run ipconfig and get the IP adress. 99% chances are it'll be something like 192.168.X.X
Use this IP instead of localhost. i.e. 192.168.X.X/TestAndroid/check.php
I am currently opening and writing a text file into my local server with the following:
$mypath="sms_file\\cbsms_";
$fp = fopen($file_name.'.txt', "w");
fwrite($fp, $value. "\r\n");
fclose($fp);
I want to now copy that file to a remote server like /home/project on 10.10.18.23 (home network)
Assuming that I have R/W access in that directory, what would be the best way of achieving this?
The remote server needs to know that there is a request coming in to store a file on it. There are several possibilities here, the easiest would be to run a FTP server.
Another option would be to use the exec() function call scp on the command line (provided you have exchanged ssh keys with the remote server).
Another option would be to create a PHP page on the remote server that accepts POST requests with files and stores them. You must provide your own security measures in this case.
If you can mount the remote host as a permanent volume (via NFS or CIFS), you can use the regular PHP copy() function.
You can try using exec() to run an SCP command:
exec('scp /path/to/file.txt user#homenetworkhost:/home/project/file.txt');
// Obviously, you'll have to set up your SSH permissions and for 'user#homenetworkhost' you'll want to change it to your home network's user and host names.
By the looks of your exmample, I would say your PHP server is on Windows (looking at the backslash in $mypath="sms_file\\cbsms_";), and your remote host is UNIX/LINUX (looking at forward slashes and location /home/project). I would suggest setting up SSH or FTP on the remote host and rather use those protocols than copying it to a network location. Your PHP server (Windows box) will then have to communicate via SSH/FTP and copy the file.
References:
http://php.net/manual/en/book.ftp.php
http://php.net/manual/en/function.ssh2-scp-send.php
If i have a local file, for example, c:/test.txt, what path do i need to type in the ftp_put function to make it work (string $local_file)? When i try with "c:/test.txt" i get an error.
Thanks
Its on a remote server. Am i using wrong php function? I want to upload a local file to a remote ftp ...
Yes, you're using the wrong function.. Remember - PHP executes on the SERVER, not in your browser and not on your local machine. Any FTP connection you establish in the PHP script will be relative to the server.
e.g. If you've got something like this:
(your machine) ----> (your website) ----> (other machine you ftp to)
The FTP connection will be between "your website" and "other machine you ftp to". Any "local" path you specify for a file will be local to "your website", not "your machine".
You'd first have to upload the file via regular HTTP file sending mechanisms via a form on your site, which gets the file from "your machine" to "your server". The PHP script which handles the upload can then use the FTP functions to transfer the file from "your website" to "other machine you ftp to".
I think that if you have to use ftp to pull a file off your machine (as opposed to using a html form) your best bet would be to set your local machine up as an ftp server. You would probably need an static ip address for this to be consistent. You could then have your script connect to your local machine and use ftp_get to grab test.txt.
I have a script that uses ftp_connect() among other FTP PHP functions for uploading a file.
ftp_connect() works when executed on my local development server for connecting to a remote FTP server. The same script, when executed on the remote server does not work for connecting to the exact same FTP server.
Could somebody please point me in the right direction?
Thanks!
Here is the code:
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$server = 'ftp.someserver.com';
$ftpConn = ftp_connect($server);
if(!$ftpConn)
echo 'failed';
else
echo 'success';
No errors are reported.
So if I understand it correctly then the script above is installed on the server that you're trying to access using FTP (ie. the script is opening a local FTP connection)? What's the use? FTP in PHP is only useful to transfer files between 2 servers, you cannot use it to transfer files from the client to the server (since the script is executed on the server).
edit
Something I didn't add in my original comment : you could use a Java FTP applet if you want to transfer files from the client to the server. But be aware of the security issues involved (because the user credentials can be sniffed :p).
Probably firewall issues. On top of that, FTP was not designed with NAT in mind.
Try to login to the production server and use a ftp client to do the same connection.
I do not know the things inside it very well but I want to give my little help.My server is ubuntu Linux with Apache、PHP and MySQL,and my develop env is MAMP on Mac.
I met the problem suddenly and cannot find what happened because it was worked yesterday,I searched many answers and can't solved it.The ftp_connect($ftp_server) only return bool(false),but I can use my FileZilla,interesting,is it?
So I try to connect the server from my command line,like ftp 111.22.333.44,It shows:
500 OOPS: cannot read user list file:/etc/vsftpd/vsftpd.user_list
I login in my ubuntu server, and didn't find the vsftpd directory,and the vsftpd.user_listis in the directory /etc/,still don't know what happened.
So I simply create the directory and copy the file vsftpd.user_list to it.Then I try ftp 111.22.333.44(your IP address) again and it works now.
hope it help someone else.