file_get_contents() on network fails. Php - windows 7 - php

I've been searching the internet for the solution but I still can't find the answer on copying through file network using php.
Please see my sample code ang its output:
<?php
$source = ("file://///server/path/file.txt");
$destination = 'sample.txt';
$data = file_get_contents($source);
$handle = fopen($destination, "w");
if(fwrite($handle, $data)){
echo "success";
fclose($handle);
}
else
{
echo"failed";
fclose($handle);
}
?>
Web output:
failed
destination folder output:
sample.txt with blank contents.
I granted permission to access the file and is available to access using my network. and is using Windows 7 platform
it is very much easy copying a file from your local drive.
what must I do to be able to copy the file from the server?
I even tried \\(computername/ipaddress)\path\file.txt but not worth it. I just make use of google for that said code.
Thanks in advance

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.

fopen & cURL on php ubuntu web server

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.

PHP cannot access local file

I'm trying to simply write to a text file with PHP and every time I try it doesn't return an error but just doesn't write. I'm doing...
$fp = fopen('file.txt', 'w');
fwrite($fp, '1') or die('error');
fclose($fp);
And very time it returns "error". file.txt is definitely in the same directory as the PHP file. I figured PHP couldn't get access to the file. I'm using Windows Server 2008. Does anyone know what the problem could be?
Two things can be happening.
One, consider setting the full path to the file within the directory like this; change /full/path/to/the/file/ to match the actual full path to the file:
$fp = fopen('/full/path/to/the/file/file.txt', 'w');
fwrite($fp, '1') or die('error');
fclose($fp);
Next, does the file itself have permissions that allow the server itself to access it. Remember, the Apache server will run as another user other than you. So need to make sucre the ownership & permissions match the Apache user.

Saving and opening a logfile

I have a vb.net app that webrequests a PHP file which does this:
<?php
$msg = $_GET['w'];
$logfile= 'savedrv.idps';
$fp = fopen($logfile, "w");
fwrite($fp, $msg);
fclose($fp);
?>
I want to make a PHP file that will open the new file created "savedrv.idps" so I can read it in vb.net. This is what I tried:
<?php
$logfile= 'reg.idps';
$fp = fopen($logfile, "r");
fclose($fp);
?>
How can I accomplish this?
Most likely your IIS settings for this Virtual Directory forbid a file with this extension to be browsed to. I got 404.7 error attempting to open a file in IE when I browsed to the URL: http://localhost/mysite/myvbfile.vb Using your browser, try to open the same URL that your VB program is attempting to access. I anticipate that you will get the 404.7 error in the browser window too.
You have two approaches here:
Have your PHP script write the file to a location outside of IIS where your VB.Net program can access.
Modify the Request Filtering in IIS for your site so that this file can be browsed.
(screenshot) http://support.citrix.com/article/html/images/1CTX132655-1.gif

Linux Box using PHP writing file to Windows Server Web Share

We have a bunch of linuix and windows servers.
On my windows desktop I can see all the shares.
Using PHP I'm attempting to write a file to a directory on a windows share using the UNC path.
//ServerShare/directory/file.txt
Using fwrite says it successfully wrote the file but the file never exists on the server.
Using opendir function says the directory isn't accessible.
This is the source pretty simple.
$file_name = "\\SERVER2\Share\CPI\data.txt";
if (!$handle = fopen($file_name, 'w')) {
echo "Cannot open file ($file_name)";
exit;
}
// Write $somecontent to our opened file.
$somecontent = "this is a test";
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($file_name)";
fclose($handle);
Any thoughts on what types of permissions need to be set to let a linux box write files to a windows box?
You should be mounting the fileshare to a local directory:
mount -f smbfs //user#server2/Share/CPI/Data.txt /media/share
Then access /media/share/Share/CPI/Data.txt from your PHP script.
PHP needs to authenticate to the share, even if it is public, and using fopen or opendir does not do this.

Categories