This is on Windows Small Business Server 2011 Essentials running iis7
The following code always returns "unable to write"
<?php
$myFile = "http://www.ascbits.com/test/test.txt";
if (is_writable($myFile)) {
$fh = fopen($myFile, 'a');
}else{
die("unable to write");
}
$body = "test ";
fwrite($fh, $body);
fclose($fh);
?>
I've checked the permissions on the file and it looks like I should be able to write to it.
Any suggestions?
try:
$myFile = $_SERVER['DOCUMENT_ROOT'] . "/test/test.txt";
The PHP streams layer is what allows you to read and write to URLs. If you check the documentation on the http stream wrapper, you'll see that it's read-only:
Allows read-only access to files/resources via HTTP 1.0, using the HTTP GET method.
It looks like you're just trying to write to a local file. Reference it by path, depending on where it's located the following may work:
$myFile = $_SERVER['DOCUMENT_ROOT'] . "/test/test.txt";
$myFile = "test/test.txt";
Related
I'm creating a Telegram Bot in PHP.
My aim now is read from a .txt file a integer and put him in a variable.
This text.txt is uploaded in a server. I tryed to do this:
$filename = "http://<MY HOST NAME>/test_bot/file/text.txt";
$fp = fopen($filename, "r+");
$send = fgets($fp);
fclose($fp);
echo $send;
But when I try to open my index.php, nothing comes out written.
am I doing something wrong?
Since you are opening from a remote host you can't open for read/write, just read. Try this instead:
$fp = fopen($filename, "r");
If you are trying to open a file on the same server as the PHP script then do not use the "http://..." path but instead the local file path and then you can open it for read/write. In the case of your script it does not appear you need write access so "r" should be sufficient.
To access without http just use the path to the file:
$filename = '/path/to/file/text.txt';
$fp = fopen($filename, "r+");
Or if you want a relative path from the script itself I prefer:
$filename = dirname(__FILE__) . '/../some/relative/path/text.txt';
i'm using PHP as a server backend, to create API, and hence it doesn't console log to browser.
However I'm finding it very hard to debug without using console, and I have to use error_log(json_encode($variable)) all the time to write to error log to see what is being returned/received.
Is there anyway I can 'monitor' the API, and use console.log or similar to write to somewhere, where I can view my output live?
Thanks #Chris, for answering my need. So I'm using the following codes to do a simple print to text file and use tail to see the live output. Works brilliantly.
function mylog($data) {
$myFile = "/home/user/html/log.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, json_encode($data, JSON_PRETTY_PRINT));
fclose($fh);
}
My advice would be to write to a log file within your PHP:
$myFile = "log.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, "write this to my file\n");
fclose($fh);
If you have command line access then you can run this command to view the contents of the file live:
tail -f log.txt
This will then show anything written to the file immediately.
This might help.
ini_set('display_errors', 1);
as this will show errors when you call an API. You can check the errors by clicking on the API call made.
I have Ubuntu installed on an Odroid. I need this box to connect to a remote website to check a .txt file. I have used the code below on two remote servers and I get the result I need. However, running the code on the odroid, I get "Unable to open file!".
I am sure this must be something to do with the PHP settings but I am now at a loss as to what it could be. Any help would be greatly appreciated.
$filename = "ftp://username:password#80.1.1.1/version.txt";
$handle = fopen($filename, "r") or die("Unable to open file!");
$contents = fgets($handle);
echo "Version = " . $contents . "<br>";
fclose($handle);
Try to open file by providing document root path with filename.
$rootPath = $_SERVER['DOCUMENT_ROOT'];
$filename = $rootPath.'version.txt';
I have a page called index.php which is calling a function "writelog" in includes/Logger.php
I have file located at includes folder and code is as below.
function writelog($logText){
$myFile = "testlog.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $logText + "\n";
fwrite($fh, $stringData);
fclose($fh);
}
It shows errror "can't open file" . I have set FullPermission for everyone and still it says it cant access file.I tried to put file in same folder as index.php and same error. What can be possible cause ? Am I having wrong path ?
Try using the full path of the log file
$myFile = "/full/path/to/testlog.txt";
I am assuming this file is also in includes, I'm guessing this is called from another script so the path would be one of the calling script. You can use this:
$prevdir = getcwd();
chdir(dirname(__FILE__));
$myFile = "testlog.txt";
chdir($prevdir);
But it's best to use absolute paths
I'm trying to create a php file which I can edit straight away without manually set the permissions.
I'm trying this...
<?php
$var = '<?php $mycontent = new Content(); echo $mycontent->block($p_name);?>';
$myFile = "testFile.php";
$fh = fopen($myFile, 'w+') or die("can't open file");
$stringData = $var;
fwrite($fh, $stringData);
fclose($fh);
?>
...it creates the file, but when I try to edit the file in my IDE it won't let me of course. I have to manually set the permission of the file created. Is there any way I can create the file and have the permission already set?
Thanks in advance
Mauro
Yes, you can thanks to PHP CHMOD
// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);
Since this aspect wasn't covered in previous answers I'll add it here:
chmod() will only take a path string as the 1st argument. So you cannot try to pass to resource that was open with fopen(), in this case $fh.
You need to fclose() the resource and then run chmod() with the file path. So a proper practice would be storing the filePath in a variable and using that variable when calling fopen() rather than passing it a direct string in the first argument.
In the case of the example code in the answer this would simply mean running chmod($myfile, 0755) (the permission code is only an example and be different of course.)
full code after corrections:
<?php
$var = '<?php $mycontent = new Content(); echo $mycontent->block($p_name);?>';
$myFile = "testFile.php";
$fh = fopen($myFile, 'w+') or die("can't open file");
$stringData = $var;
fwrite($fh, $stringData);
fclose($fh);
// Here comes the added chmod:
chmod($myFile, 0755);
?>
Php has chmod, works just like the Linux version.