php read contents of file on shared directory - php

I have a laptop running a server and I want to read a text file which is mounted on a raspberry pi. I have shared the Pi directory on the windows network and can access this folder ok. I have got a PHP script that can open a file on the localhost drive and display the contents, but I cannot get it to read from the shared Pi folders.
The following is a simple php script which is suppose to read the file and display the contents.
<?php
$data = file_get_contents('file:///Y:/test1.txt');
echo ($data);
?>
I can access the folder through the windows explorer so it has been shared correct. I can also see the contents of the file if I put the following in google explorer
file:///Y:/test1.txt
Is there a way to do this easily or i need to think of a different approach. I see others have tried this but cannot see how they have got this to work
I have read about having to change the reg file in the windows machine.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters
RestrictNullSessAccess to key 0 which I have done but still does not work

Related

PHP - fopen("filename", "w") creates file in local environment but doesn't create document in the hosting server

I have a website with a hosting server running PHP.
I have written an experiment that retrieves data through a survey model, and I use the fopen("filename", "w") function to store retrieved data.
I tried this in my local environment while building up the webpage. Everything worked fine, and a document was created in the subfolder TestResponses/ with the data requested.
I just uploaded the file in my server, and tried it out. Everything works fine except that my file is not created or stored.
I use the following code to submit my data:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "storedata.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("test_variables=some_variables");
and I use the following code to write it down in a file to be stored in my subfolder:
$test_variables = $_POST["test_variables"];
$myfile = fopen( "TestResponses/" . substr($test_variables, 0, 18) . ".txt", "w") or die("Unable to open file!");
fwrite($myfile, $test_variables);
fclose($myfile);
Does anybody know if I missed to do something required to create and store the file in the server?
P.S. the subfolder TestResponses/ already exists in the server, so it does not to be created.
P.P.S. I don't get any error message by pressing F12.
update: I discovered that the fopen() function works correctly and files are updated to the folder in the server side. It was a delay problem, and files were uploaded (visible) only after about 5 minutes. So, I will mark this question solved.
The local environment and the remote server are different (duuh :) ).
Your first step is to figure out what is the user (on remote server) that runs your php file. What OS is the remote server? If it is linux, probably your user that runs php is www-data (apache) if you are running a version of ubuntu/debian.
If you have console access to the remote server you can check the permission of the folder TestResponses using ls -alh command in the parent folder of TestResponses.
HOW TO TEST
The first and the simplest thing is to chmod 777 TestResponses folder - but this is a security risk, only use the 777 permission to test that your data is created inside the folder.
If you use a software like filezilla or winscp, you can also check what permissions the TestResponses folder has.
The correct permissions for a folder should be 775 or 755 (depending on the group).
You might need to check how permissions RWX are on linux (if linux is your remote server OS).
P.S. make sure you also add encoding UTF-8 to your website.
On outside servers, always check write access using is_writable() on the folder. If there's no write access, check the rights of the folder with some FTP client or console (see #Mike 's answer), and if it's 775, make sure your FTP user is in the same group as www-data (or whatever the web server is running with). If you're not sure how, talk to the Linux guy operating the server.
If it's 755, you can only write with the same user (not group) that created the folder - so in this case, if it's an option to delete the folder (from FTP or console) and re-create it with your php script, it can be a good solution. (Obviously, if there's something in the folder or if it's something used by others, not just you, well, then please don't delete it)

Open txt file from desktop using PHP

It is possible to open a txt file from desktop to my webserver?
I have tried fopen but It cant open the stream.
Code:
$txtfile = fopen("C:\Users\steve\Desktop\AntiCheat.txt","r");
echo $txtfile;
The Error message:
Warning: fopen(C:\Users\steve\Desktop\AntiCheat.txt): failed to open stream: No such file or directory
This works fine If I using XAMPP but on my webserver it does not works
This works fine If I using XAMPP but on my webserver it does not works
XAMPP is a package that includes a web server, so presumably, you mean that it doesn't work if you use a web server that is running on a different computer.
This shouldn't be a surprise as the other computer won't have your hard disk in it, so it won't have your C drive or your files.
You need to either:
Share your file over the network to the computer running the web server (since the web server probably doesn't run on your LAN, this is likely to be logistically challenging).
Copy the file to the computer running the web server.

Open directory on network in PhpStorm

I'm working from a windows 7 PC and my source code is on an Ubuntu PC. I have a shared folder on the Ubuntu computer that I can browse from my Windows computer.
I'm trying to open this directory in PhpStorm via File -> Open Directory. The problem is I can't seem to browse the network using the File browser (shown below) PhpStorm provides. Also entering a path like \my-dev-pc\projects\php\cms just causes it to open my C:\ directory.
Any ideas on how I can solve this? Perhaps some configuration that allows PhpStorm to use the default Windows Explorer file browser?
Since your project files are on network, you should follow below steps:
file > new project from existing source.
Then you will get a pop up with options for connecting to remote machine/server using ftp/sftp.
Select that and then enter your credentials.
There is also an option to select files which are accesile via network.
This should help you.

How to resolve linux folder name vs Win7 localhost folder name

I have a text file on my Linux website and I'm trying to run my website php code on my Win7 localhost machine.
The text file on the server is located at:
/home/vault/public_html/ssfiles/mysql.txt
On my local machine I'm running Apache/PHP/MySQL and my php files are located in:
C:\wamp\www\
How do I get my php script to open the file with the first folder string and to also work locally with the second folder string?
I'm new to running php on my local machine and have always run my php scripts directly on the Linux server...so I'm not sure how to resolve this?
Is there a php.ini setting that handles this? based on what machine the script is executing on?
Thanks...
If you place the files in htdocs folder in c:\wampp\ you can directly access the file with localhost/folder_name/file_name.php from broswer

navigate to C:Drive via php error

I'm trying to navigate to C drive via PHP on my local drive, this one line of code works fine:
<a href='<?php echo'file:///C:\Users\Emily\Documents\'?>' TARGET="_blank" >Clcik Me</a>
This works fine from my local drive but as soon as this is on my sever it throws an error. I've been trying to strip and replace slashes but to no luck, hope sone one can help me.
This is because on the server there is no "C:\Users\Emily\Documents".
You need to make sure that the filepath you use is available to you on the server. My recommendation would be to setup your local machine to have the same filepath which exists on your server.
For example lets say you have a folder named Web, in which you keep the following files and folders:
index.php
\images (This is another folder)
From your file if you wanted to access a file in the images folder, from your index.php file you would access it as "images\FileYouWant.jpg"
Your server should have the same setup so you copy index.php and the images folder into the folder on your server, this way they keep the same file paths.
You can find more on file paths here
you can't access local file links from remote servers. if that were still possible (Like it was many years ago), a remote site could get at your local files...
Your server OS maybe not windows and file path is different!
Try to debug with <?php var_dump(real_path('file_path.php')); ?>

Categories