fopen & cURL on php ubuntu web server - php

Whether I'm trying to create a file, or use file_get_contents on a external url I just can't seem to get fopen (or curl) code to work. I have checked phpinfo and allow_url_fopen, allow_url_include & curl are all set to on and even looked at the php5/apache2/php.ini and they are all set to on.
But not matter what I do no php code works. Below are some examples (that work on wamp/localhost but not when moved to live web server)
create a html file in folder (works on windows localhost/wamp server)
if (!file_exists('media/folder')) {
mkdir('media/folder', 0755, true);
}
$config_file = 'media/folder/config.html';
$config_file_handle = fopen($config_file, 'w') or die('Cannot open file: '.$config_file); //implicitly creates file
$config_file = 'media/folder/config.html';
$config_file_handle = fopen($config_file, 'w') or die('Cannot open file: '.$config_file);
$config_data = 'add to my file content'.PHP_EOL.'
';
fwrite($config_file_handle, $config_data);
Does not work on live server. Quite frustrating as this needs to work and have tried many curl options but fail as well.
Below is another php code snippet that works locally but not live.
<?php
//works locally if external url used
//works locally if local file used
//Does not work live server if external url used
//Does work live server if local server url used
$string = file_get_contents('my url');
$json_a = json_decode($string, true);
foreach ($json_a as $person_name) {
//Person Name
echo $person_name['PersonName'];
}
?>
Cant really achieve what is have set out to do if I can't get this to work.
Any Help appreciated

An extremely common error, you may need to allow the user www-data to access your desired files. Simply right click on the file or folder you want click properties, go to the permissions tab and set www-data to the owner.

Related

Opening/Reading a local file from PHP Web Application

I feel like this should be a pretty straightforward process.
I have the following code:
<?php
$filename = "c:/TestFolder/config.txt";
echo "Attempting to read: ".$filename."<br/>";
$fh = fopen($filename, 'r') or die("file doesnt exist");
$readtext = fread($fh, filesize($filename));
fclose($fh);
echo "The Text is: ".$readtext;
?>
I have checked that I do indeed have "config.txt" in a folder called "TestFolder" on my C:/ drive... but I keep getting an error that the file doesn't exist.
I checked my PHPInfo to ensure that "allow_url_fopen" is turned on.
I have also tried different file path variations, such as:
C:\\TestFolder\\config.txt
C:\/TestFolder\/config.txt
This doesn't seem to make a difference.
Any idea what might be preventing me from opening this file?
Edits:
It should be noted that this file is uploaded to my web host, while I am attempting to access a file on my local machine. Not sure if this changes things at all.
This is not possible. "local files" are files on the server where PHP is running, not the client running the web browser. While they might be the same machine when you're testing locally, once you upload the script to a web host, PHP tries to read files on the web host, not your machine.
The only way for PHP to access a file on the client machine is for the application to upload it.

Some FTP commands on PHP work from localhost, but don't work from client live site

I've created a module for my client's project that should send a file via FTPS onto a web-service. The web-service allows connection from some IP addresses only.
It works on my localhost, but some FTP-commands don't work on the client's live site.
For example, primarily I connect with ftp_ssl_connect. It works on the client's site.
Then, I log in with ftp_login. It also works on the client's site.
Then, I move to the passive mode with ftp_pasv. It also works on the live site.
But when I'm trying to get the files list with ftp_nlist command, it works on from my localhost but refuses to execute on the client's site.
Also, when I send a file with command ftp_put, it works on my localhost but refuses to work from the client's project.
$config = yrv_eboks_get_config_data();
$conn = ftp_ssl_connect($config->ftp_host, 21, 15);
if (ftp_login($conn, $config->ftp_login, $config->ftp_password)) {
if (ftp_pasv($conn, true)) {
$files = ftp_nlist($conn, ".");
var_dump($files);
}
} else {
// "Could not login via login via FTPS"
};
I don't know where is the problem and how to solve it.
Could you advise me, where can be the problem and what to do?
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
write this at the top of the file and access the php file your browser will display the error. are you sure these functions are enabled on your hosting? because shared server disable lots of function to prevent abuse for example source, symlink, ftp_put etc.
You can check which function is disabled by creating a phpfile using below code
<?php
phpinfo();
?>
save the above code as anyname.php then access it and you will see php information there. press ctrl+f (search) button and write disable_functions and there you will see all the disabled functions or create a file with the below code and access it from browser ( it may not work if your provider blocked ini_get function too)
<?php echo "Disabled functions: ".ini_get('disable_functions')."\n"; ?>
if your function is disabled you can enable them by creating php.ini file in your directory or request your hosting provider to enable them.
to enable function: create php.ini with following code
disable_functions = ""
however it may not works if your hosting environment is use default php.ini with priority so better contact with provider and request them to enable.

PHP Read/Write files on remote server

I'm making a utility that provides a GUI to easy edit certain values in a csv file on a remote server. My boss wants the utility in php running on the private webserver. I'm new to php, but I was able to get the GUI file modifier working locally without issues. The final piece now is rather than the local test file I need to grab a copy of the requested file off of the remote server, edit it, and then replace the old file with the edited one. My issue is uploading and downloading the file.
When I searched for a solution I found the following:
(note in each of these I am just trying to move a test file)
$source = "http://<IP REMOTE SERVER>/index.html";
$dest = $_SERVER['DOCUMENT_ROOT']."index.html";
copy($source, $dest);
This solution ran into a permissions error.
$source ="http://<IP REMOTE SERVER>/index.html";
$destination = $_SERVER['DOCUMENT_ROOT']."newfile.html";
$data = file_get_contents($source);
$handle = fopen($destination, "w");
fwrite($handle, $data);
fclose($handle);
This also had a permissions error
$connection = ssh2_connect('<IP REMOTE SERVER>', 22);
ssh2_auth_password($connection, 'cahenk', '<PASSWORD>');
ssh2_scp_recv($connection, '/tmp/CHenk/CHenk.csv', 'Desktop/CHenk.csv');
This solution has the error Fatal error: Call to undefined function ssh2_connect() which I have learned is because the function is not a part of the default php installation.
In closing, is there any easy way to read/write files to the remote server through php either by changing permissions, having the php extension installed, or a different way entirely that will work. Basically I'm trying to find the solution that requires the least settings changes to the server because I am not the administrator and would have to go through a round about process of getting any changes done. If something does need to be changed instructions on doing so or a link to instructions would be greatly appreciated.
Did you set the enable-url-fopen-wrapper in your php.ini?(only if your php version is older)
Please look # php remote files storing in example 2

PHP - Create directory on different server

I have Wamp (server called emerald) running and Mamp running on my Mac. People register on Mamp. Emerald is basically file hosting.
Emerald connects to Mamp's mysql database, to login users. However, I want to create a directories for new registrations on Emerald using PHP.
How can I do this? I have tried using this code:
$thisdir = "192.168.1.71";
$name = "Ryan-Hart";
if(mkdir($thisdir ."/documents/$name" , 0777))
{
echo "Directory has been created successfully...";
}
But had no luck. It basically needs to connect the other server and create a directory, in the name of the user.
I hope this is clear.
You can't create directories through http. You need a filesystem connection to the remote location (a local hard disk, or a network share for example).
The easiest way that doesn't require setting up FTP, SSH or a network share would be to put a PHP script on Emerald:
<?php
// Skipping sanitation because it's only going to be called
// from a friendly script. If "dir" is user input, you need to sanitize
$dirname = $_GET["dir"];
$secret_token = "10210343943202393403";
if ($_GET["token"] != $secret_token) die ("Access denied");
// Alternatively, you could restrict access to one IP
error_reporting(0); // Turn on to see mkdir's error messages
$success = mkdir("/home/www/htdocs/docs/".$dirname);
if ($success) echo "OK"; else echo "FAIL";
and call it from the other server:
$success = file_get_contents("http://192.168.1.71/create_script.php?token=10210343943202393403&dir=HelloWorld");
echo $success; // "OK" or "FAIL"
Create a script on another server that creates the dir and call it remotely.
Make sure you have security check (+a simple password at least)
There is no generic method to access remote server filesystems. You have to use a file transfer protocol and server software to do so. One option would be SSH, which however requires some setup.
$thisdir = "ssh2.sftp://user:pass#192.168.1.71/directory/";
On Windows you might get FTP working more easily, so using an ftp:// url as directory might work.
As last alternative you could enable WebDAV (the PUT method alone works for file transfers, not creating directories) on your WAMP webserver. (But then you probably can't use the raw PHP file functions, probably needs a wrapper class or curl to utilize it.)
I know this is old but i think this might me useful, in my experience:
if(mkdir($thisdir ."/documents/name" , 0777))
doesn't work, i need to do it:
mkdir($thisdir, 0777);
mkdir($thisdir ."/documents" , 0777);
mkdir($thisdir ."/documents/name" , 0777));
hope it helps :)

PHP file read not working

On a server I have the file some.php. I try to read the contents using file_get_content and fread. Both these don't read my content.
If I try some other URL like yahoo.com, these functions are reading the contents. Why does this happen?
$my_conetn = file_get_contents("http://example.com");
echo $my_conetn;
The above snippet is not working.
$handle = fopen($filename, "r");
$contents = fread($handle, 20);
echo $contents;
fclose($handle);
Also the above snippet is not working.
How to check my server? Is there any file read operation locked or not?
edit: I am not sure, but it is only my server file that can't be read. I can read the other server links. So I contact my hosting provider, then I get back to you guys/gals.
When using file functions to access remote files (paths starting with http:// and similar protocols) it only works if the php.ini setting allow_url_fopen is enabled.
Additionally, you have no influence on the request sent. Using CURL is suggested when dealing with remote files.
If it's a local file, ensure that you have access to the file - you can check that with is_readable($filename) - and that it's within your open_basedir restriction if one is set (usually to your document root in shared hosting environments).
Additionally, enable errors when debugging problems:
ini_set('display_errors', 'on');
error_reporting(E_ALL);

Categories