Calling PHP when running HTML locally not echoing strings - php

I like to test my Javascript by running on my local drive to avoid having to upload to my web page each time I make changes. In my HTML file I just use full paths to point to PHP files on my web page. Haven't had a problem until I ran into this. The PHP file below runs fine if I run the HTML file from my web page. But if I run the HTML file from my local drive, only the first part of the PHP file works. The foreach block does not - or it's just not echoing the strings.
I've tried using the full path to the folder rather than just ".txt" below even though it seems that shouldn't be necessary as the PHP is run at the server end.
I tried other methods of reading files in a directory in case the "glob" useage was the problem when running on my local drive.
I even did away with all the commands within the foreach block and just put an echo statement there, but nothing appeared.
I'm guessing an echo statement doesn't work when the PHP file is called from an HTML file on a local drive?
Tried innerHTML with a but that didn't work - or I'm not doing it correctly.
<?php
$MSG=$_GET["myFile"];
$fh = fopen($myFile, "w") or die("can't open file");
$MSG=$_GET["jsVar"];
fwrite($fh, $jsVar);
fclose($fh);
foreach (glob("*.txt") as $filename) {
echo $filename;
echo "<br />";
$file = fopen($filename, "r");
while(!feof($file)){
$line = fgets($file);
echo $line . "<br />";
}
fclose($file);
}
?>

Windows doesn't support PHP, you must install XAMPP to run it through localhost.

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.

file_get_contents() on network fails. Php - windows 7

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

not writing in text file on server side using php

I'm trying to put the results from the database into a text file after fetching it, and if the text file doesnt exist it creates one and put the data into it. after that i showed the the data by extracting from those txt files in another page. the problem is it is working fine in my local xampp server. but i dont know why its not working in my aws account. the result page shows blank in aws elastic ip.but its connecting to the database and if there is no results found it also shows the message. so its just not putting the datas into the txt file nt even creating it. i tried by creating the txt file manually. still not working. but its working fine in my local server. Here is the code i used-
$wineFile ="WineName.txt";
file_put_contents($wineFile, $W);
From your comments , seems like there is not enough permission. You need to CHMOD 777 on the file.
So make use of CHMOD command on PHP
Something like this
$file ="WineName.txt";
chmod($file, 0777);
$fp = fopen($file, 'w');
fwrite($fp, $content);
fclose($fp);
try
$wineFile = "WineName.txt";
$fh = fopen($wineFile, 'w') or die("can't open file");
$stringData = "Tasty Wine\n";
fwrite($fh, $stringData);
fclose($fh);

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.

php cli reading files and how to fix it?

While using PHP cli in ubuntu i got apache as web server i cant read the file which i can while using it in the browser.
And about the error it just shows nothing and the next like i just used this code for my testing purposes :
<?
$handle = fopen("testFile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
if(!is_readable($handle)
{
die("Not readable");
}
fclose($handle);
}
?>
How do i fix that ?
EDIT :
After removing the '#' before fopen i got the following error
fopen(testFile.txt): failed to open
stream: No such file or directory in
/var/www/log/ka.php on line 2
When you run your script through CLI, it uses your shell's working directory, in opposition to the file's directory (which Apache hands as the current directory). This is important for you, since your fopen call depends on a relative path; and relative paths are resolved relatively to the working directory, not to the script.
To have your script behave like it does with Apache, you either need to cd /var/www/log prior to running the php command, or add this at the beginning of your PHP script:
chdir(dirname(__FILE__));

Categories