Saving and opening a logfile - php

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

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/fwrite path above public_html issue

I'm testing php scripts on my web server.
I'm using a basic fopen / fwrite to write data to file.
The following scripts works perfectly in public_html/ but fails to work in folders above "I'm trying in /test/"
centOS with WHM/cPanel, latest.
<?php
$fp = fopen('lidn.sh', 'w');
fwrite($fp, 'Cats chase mice');
fclose($fp);
?>
This works fine. However:
<?php
$fp = fopen('/test/lidn.sh', 'w');
fwrite($fp, 'Cats chase mice');
fclose($fp);
?>
doesn't write anything to file.
I need php to output data to a ".sh" file, so having it inside public_html would, I think, be a gigantic security flaw.
The goal is to securely output data to a ".sh" via PHP
You should check the test folder permissions.

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.

Writing to a file PHP

I have the following code:
$cachefile = "http://www.DOMAIN.com/users/{$user}.php";
$fp = fopen($cachefile, 'w'); // open the cache file "cache/home.html" for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_clean(); // Send the output to the browser
chmod($cachefile, 0644);
The file path is not valid, as it produces this error: Warning: chmod() [function.chmod]: No such file or directory in ...
However, this code does:
$cachefile = "{$user}.php";
$fp = fopen($cachefile, 'w'); // open the cache file "cache/home.html" for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_clean(); // Send the output to the browser
chmod($cachefile, 0644);
The file--using the second block of code--is created in the same folder as the script that created it. However, I would like to save it to the first block's location. I look on php.net for fopen and I didn't see that I was doing anything wrong, but obviously I am.
EDIT (comment response):
I also tried $cachefile = $_SERVER["DOCUMENT_ROOT"] . "/users/{$user}.php" to no avail.
You cannot write to a remote file using fwrite(). If you're writing to a file that is on your own server, use a relative path.
You need to use the full path to the file on the server, not the web address:
$cachefile = "/var/www/path/to/users/{$user}.php";
You cannot open remote files, you can only edit files on the local filesystem, if the file is on the file system, then you have to use the relative path,
Also instead of using fopen, you could just use file_get_contents() and file_put_contents().

PHP can't fopen remote file

I'm making a script to open remote file (ftp) and edit it with fopen and fwrite.I have problem while connecting to ftp.I made this code
$handle = fopen("ftp://".$ftp['username'].":".$ftp['password']."#".$ftp['server']."/cstrike/addons/amxmodx/configs/users.ini", "w") or die("ftp error");
fwrite($handle,$text."\n") or die('err2.');
al variables are set.I tried with ftp://... in web page and it works.allow_url_fopen is On...
It just says "ftp error"
The ftp:// handler only allows creating new files with fopen("ftp://...", "w");.
If the file already exists you can only write to it if you open it with overwriting set, i.e mode w+.

Categories