how to map the path to the file easily?
public_html/api/function.php
<?php
function writetologfile($content)
{
$filename = 'logfile/testing_randomstring.txt';
if (!$handle = fopen($filename, 'a'))
{
echo "Cannot open file ($filename)";
exit;
}
fclose($handle);
}
?>
the actual path of the text file is in public_html/r/admin/logfile/testing_randomstring.txt
so if I run the script at public_html/folder1/folder2/addlog.php, it won't be able to find the path to the testing_randomstring.txt
addlog.php
<?php
include("../../api/function.php");
writetologfile('hahaha');
?>
How I can able to easily point to this text file path, no matter where my php calling script is from.
I tried to change $filename = 'logfile/testing_randomstring.txt'; inside writetologfile function by enforcing it to absolute fix path,
something like $filename='/r/admin/logfile/testing_randomstring.txt',
but it is not working
Instead of using a relative path, you could specify an absolute path. Assuming public_html is in your home directory, try this:
$filename = '/public_html/r/admin/logfile/testing_randomstring.txt';
fopen(getenv('HOME') . $filename, 'a');
This uses getenv to read the contents of the environment variable $HOME.
Related
I'm currently writting a login-system with PHP, for that I need to read the files with some user-information in it.
But after changing the folder system, PHP fopen doesn't read the files anymore.
Both the users.php and userinf.csv files are in the samle folder.
I allready tried to change the filepath, hard-coded the filepath , recreated the file. All of which file.
//Read file
$fp = fopen("userinf.csv", "r");
if(!$fp)
{
echo "File couldn't be read";
return false;
}
Before changing the file system, it worked. But now I am geting the error:
Warning: fopen(userinf.csv): failed to open stream: No such file or directory in FILEPATH on line 45
When you use the fread function without any reference it could fail. I always say that you need to check your path first with getcwd()
<?php
echo getcwd(); //Current Working Directory
?>
Use absolute paths, always. It removes any ambiguity. Using a relative path may change based on where your script is located, among other things, depending on your system.
$fp = fopen("/home/somewhere/blah/userinf.csv", "r");
You can always use a variable for the path as well:
// Somewhere in your code
define('ROOT_PATH', "/home/somewhere/blah");
// In the implementation
$fp = fopen(ROOT_PATH . "/userinf.csv", "r");
I am facing the issue with this code:
<?php
$files = scandir("D:/Dummy");
foreach($files as $file) {
$filenam = $file;
$path_to_file = $filenam;
$file_contents = file_get_contents($path_to_file);
echo "Hello ".$filenam;
$printFileName="";
if(strpos("9222339940", $file_contents) === false)
{
$printFileName=$filenam." ";
}
}
echo $printFileName;
?>
Basically, I have written this code to scan all the files in the directory and from the each file, I need to replace the mobile number. But for some reason, I'm not able to run the script. It is throwing error:
file_get_contents(name of the file) failed to open stream. No such file or directory error.
The scandir() function of PHP will only return the basenames of the files within the directory. That is, if your directory D:\Dummy contains a file test.txt, then scandir() will not return the full path D:\Dummy\test.txt, but only test.txt. So the PHP process will not find the file, because you need to provide the complete path of the file.
Is there any way to file_put_contents from different path i mean instead of
c/programfiles/../../htdocs/test.txt using c/test/test.txt
the below is the code i am using:
$date=current_date;
$file = "test_"$date".txt";
$contents = "testtest";
file_put_contents($file, $contents);
Many thanks in advance!
When placing files on your computer you can either use relative or absolute path like this:
// absolute path
$file = "C:/temp/test.txt";
// relative path from C:/Program Files/wamp/htdocs/index.php
$file = "../../temp/test.txt";
Both of those files point to the same place
I'm attempting to check for file existence with Zend Framework and, if the file doesn't exist, have it be created. Here's the code being used:
$filename = "/assessmentsFile/rubrics/$rubricID.php";
$somecontent = "test";
if (!$handle = fopen($filename, 'w+')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === false) {
echo "Cannot write to file ($filename)";
exit;
}
However, I assume due to Zend's way of handling file structure, if a file doesn't exist it just spits out:
Warning: fopen(/assessmentsFile/rubrics/1.php) [function.fopen]: failed to open stream: No such file or directory
Because the fopen function isn't working, fwrite is unable to write the file.
Is there another way of doing this?
Most likely the issue is with the path to $filename.
You have
$filename = "/assessmentsFile/rubrics/$rubricID.php";
which tries to create a file in the root of the server in a directory called assessmentsFile.
Most likely you need to be using:
$filename = $_SERVER['DOCUMENT_ROOT'] . "/assessmentsFile/rubrics/$rubricID.php";
$_SERVER['DOCUMENT_ROOT'] should do the trick if the assessmentsFile folder is in your web root. Otherwise there are other variables you can use to get a fully qualified path, or you can simply hard-code the path:
$filename = "/home/yoursite/public_html/assessmentsFile/rubrics/$rubricID.php";
There's a function file_exists that tells you if the file exists, and with is_file you can check it's a file (and not a directory for example).
(Another way is to suppress warnings by putting an # before the function call (e.g. $handle=#fopen(...), but it's better to check for file existence)
Try this:
if(is_file($filename)){ // exists
$handle=fopen($filename,"w+");
}else{
$handle=fopen($filename,"w"); // create it
}
// ...
I am learning php, trying to use the fopen() function.
The php file I am coding is in this directory /domains/xxxxx.com.au/public_html/phpfile.php
What path do I specify for the file to be opened, the example I am looking at is based on a server on a pc where this is the file path $filename = "c:/newfile.txt"; not an online server.
UPDATE!
This is the whole script, I have the file location correct, now the4 script is returning "couldnt create the file" does this have something to do with ther permission of the folder location of the file?
<?php
$filename = "/domains/xxxxxxxx.com.au/public_html/newfile.txt";
$newfile = #fopen($filename, "w+") or die ("couldnt create the file");
fclose($newfile);
$msg = "<p>File Created</p>";
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<? echo "$msg" ?>
</BODY>
</HTML>
Assuming that your php file is inside the public_html too, you could use :
$fp = fopen( "newfile.txt", "rt" );
Or, giving the full path :
$fp = fopen( "/domains/xxxxx.com.au/public_html/newfile.txt", "rt" );
This will open it if it already exists.
Refer to this for further details of opening flags.
UPDATE:
You can even use the is_writable/is_readable function to check file access before trying to open it.
Read http://us2.php.net/manual/en/function.fopen.php Example #1 is relevant for a Unix system.