Access file created by NodeJS module using PHP or browser - php

I have installed a NodeJS module on a Digital Ocean Droplet. It generates a csv file that I store in the directory root/csv_file/my_file.csv.
However, I cannot access it in the browser by simply visiting ip_address/csv_file/my_file.csv.
I read this question which asks me to install http-server so I installed it. After that I ran the following command:
http-server csv_file
and
http-server root
However, none of the allowed me to access the file by visiting root/csv_file/my_file.csv.
Using the file_get_contents() function in PHP gives me the following error:
No connection could be made because the target machine actively refused it.
How can I access this file preferably in PHP or in browser?
Thanks.

http-server csv_file will serve in port 8080 by default.
Did you try http://your_ip_address:8080
Please make sure: the port is already opened/allowed (https://askubuntu.com/questions/911765/open-port-on-ubuntu-16-04)

Related

local PHP & local node.js (react-native)

I have a local Apache server using XAMPP which runs on port 80 and uses a local PHP file.
In the browser I can open this file from localhost/test/test.php and localhost:80/test/test.php.
I also have a react-native app, and it starts an NPM server on port 8081.
In this app I want to make a request to the local PHP file, but when I make the request it returns a 404 error.
I understand that it happens because the NPM server doesn't know about the local Apache server so I want to understand how to combine them.
Thanks.
don't write 'localhost/test/test.php'
You have to write with your ip, like this => '192.16x.x.x/test.php/'

file_get_contents and curl not working via php but working on CLI

When i use file_get_contents with a url (http://example.com or other), it will return false with different context, user_agent, etc:
file_get_contents(http://example.com):failed to open stream: operation
failed
However when we used file_get_contents() for a local file, it works. But it shows the above error when we try to access any file through http.
I tried the same using cURL and got the following error:
cURL Error (7): couldn't connect to host
According to some solutions on stackoverflow, we changed this setting in php.ini file:
ini_set('allow_url_fopen', 1);
I have also used snoopy library after including snoopy class.php
require "Snoopy/Snoopy.class.php";
$snoopy = new $Snoopy;
fetchtext("http://example.com");
$text = $snoopy->results;
it is also giving an empty response.
If I cURL or wget the same URL using terminal, it works perfectly. But it doesn't work in the PHP code when the file is not local (through HTTP).
Server details:
Debian GNU/Linux 7.6 (wheezy)
Apache/2.2.22 (Debian)
PHP 5.4.45-0+deb7u7
In Debian, there will be a group aid_inet. Apache should be a member of that group to allow to access network.
Run the following command to get the apache user. In my machine its www-data.
cat /etc/passwd
Try this command to add apache user in aid_inet group.
usermod -a -G aid_inet www-data
[Note: if you have different apache user then use your own]
Now, run your php code again to test. It should work.
There is an error in your code, which could be the issue:
fetchtext("http://"google.com");
should be:
fetchtext("https://www.google.com");
(Google uses HTTPS, and always redirects to www subdomain).
Have you checked that the URL you're attempting to query is valid (i.e. structure, escaping of special characters)? It would really help if you can give a real URL you're trying to call.
Regarding from where you are running your php files ( apache or command line ), check that you have allow_url_fopen = On on both php.ini files :
/etc/php5/cli/php.ini
/etc/php5/apache2/php.ini

Unable to execute "testphp" from localhost

I installed LAMP from unixmen.com, but I am not able to run the php script using localhost. It says:
Not Found The requested URL /html/testphp.php was not found on this server. Apache/2.4.18 (Ubuntu) Server at localhost Port 80
Note! My apache server works fine.
Have you started your lampp server?
terminal command is
sudo/opt/lampp/lampp start
provided that is where lampp is installed.
Also the url should be http://localhost/name_of_your_project/testphp.php
first question: this file is first .php what executed in new serve?
case yes: make sure your server is started:
sudo/opt/lampp/lampp start
verify permission in your file.
or
try access using IP 127.0.0.1
http://127.0.0.1
your directory html are sub \var\www\html ?
try move to one level up
Regards

Command executed in PHP with Centos7 and Apache isn't able to connect to network?

I'm debugging my PHP app on CentOS7 using Apache.
My application is a Web GUI to manage the Torque batch system and I used the qmgr, which is a command line tool provided by Torque to do the management work.
Because only the root user can execute the qmgr and the Apache server cannot be running as root user, I have written a C program as a wrapper for anyone to execute commands as root user.
But the PHP application always give the following output:
socket_connect_unix failed: 15137
qmgr: cannot connect to server (errno=15137) could not connect to trqauthd
This means the PHP app cannot raise a socket connection to connect the Torque server.
Here is some additional information:
The command called by the PHP application can be executed correctly in the shell
The same PHP app can be executed correctly on a CentOS6 server with Apache
SELinux and the firewall are disabled
I have tried the two versions (5.1 and 4.10) of Torque, the result is the same
Apache and PHP are used with the default RPM's of CentOS7.
I thought there are some new security limits that maybe influence Apache on the CentOS7 server.
Please give me some suggestions, thank you!
I had the exact same problem.
The cause is that newer Apache.httpd versions default to having the systemd property PrivateTmp set to true. This causes the httpd service to see a private /tmp directory that is actually mapped to some other location in the file system, instead of the real /tmp directory. PHP, running in the Apache process, has the same /tmp directory as the Apache service, and so do any processes forked from PHP (e.g. using exec or system etc). So when PHP calls qsub (etc), that too will see the private /tmp directory.
This causes the error you mentioned because qsub internally uses the unix socket /tmp/trqauthd-unix to communicate with trqauthd. But qsub sees the "fake"/private /tmp directory instead of the real one, so it doesn't find the socket.
This explains why the command works when you run it manually in a console--in that case, qsub sees the real /tmp directory, as opposed to the private one it sees when forked from PHP (running the Apache service).
One solution is to simply change the PrivateTmp property in the file httpd.service from true to false. You can find this file under the /etc/systemd directory. The subfolder it is in probably depends on the linux distribution, so use the find command to locate it:
find /etc/systemd -name httpd.service
This really helped me!
I have been struggling a lot having a php script using exec()-command. For some reason I got permission denied. Having tried vary many things, including running my scripts in shell as the www-data user, but with no success, this was finally the solution to my problem.
BTW, for Ubuntu the apache service config file is located at cat /etc/systemd/system/multi-user.target.wants/apache2.service

FTP in PHP script and command line fails with 553, but Filezilla transfers OK

I'm getting a weird FTP failure in a PHP script. I've recreated the failure just using command line FTP, so I think I can eliminate PHP from my enquiries.
Using command line FTP in Ubuntu 10.10, I can connect to my FTP server, log in, list directories, etc. However, when I try to transfer a file from my local machine to the FTP server using "put" I get a "553 Could not create file" error. I get the same error in active and passive modes. Transfer is set to binary.
When I run FileZilla on the same machine and connect to the same FTP server using the same credentials, and try to transfer the same file to the same destination directory, the transfer works just fine, after defaulting to binary and passive mode.
FWIW, the PHP script (which is a simple ftp_connect, ftp_login and ftp_put) using the same details fails with "Illegal PORT command", although I suspect this is just PHP's interpretation of the same error I get from command line FTP. Again, this error occurs in both active and passive FTP modes.
FileZilla and ftp command probably starts in a different directories. You can setup starting remote directory in FileZilla and you have to go to the same location from command line.
Try using pwd command after logging in from command line client, it will show current working directory, compare that with the one FileZilla is using and then go to the correct directory with a cd command.

Categories