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.
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");
$content = "some text here";
$fp = fopen("myText.txt","w");
fwrite($fp,$content);
fclose($fp);
The above code creates a file in the folder where the PHP script is present. However when the script is called by Cpanel Cron then file is created in home directory.
I want file to be created in the same folder where the php script is present even if its run by cron. How to do that ?
Try using __DIR__ . "/myText.txt" as filename.
http://php.net/manual/en/language.constants.predefined.php
Try something like this, using the dirname(__FILE__) built-in macro.
<?php
$content = "some text here";
$this_directory = dirname(__FILE__);
$fp = fopen($this_directory . "/myText.txt", "w");
fwrite($fp, $content);
fclose($fp);
?>
__FILE__ is the full path of the currently running PHP script. dirname() returns the containing directory of a given file. So if your script was located at /mysite.com/home/dir/prog.php, dirname(__FILE__) would return...
/mysite.com/home/dir
Thus, the appended "./myText.txt" in the fopen statement. I hope this helps.
So i'm currently trying to create a code which simply creates and publishes a file to my webroot, modifies and writes to that file, and then finally change the location of the file to another directory/folder using move_uploaded_file()
This is my code so far
$myfile = fopen($_POST['title'].".txt", "w");
move_uploaded_file($myfile,'$dir/$title.txt');
fwrite($myfile, $_POST['textarea11']);
fclose($myfile);
The code doesn't work, i've tried echoing move_uploaded_file() and it returned nothing, however the file was uploaded but it's location just wasn't changed.
$dir is defined as $dir = __DIR__.'/../uploads/'; and $title is define as $title = $_POST['title'];
move_uploaded_file() can only be used if you are submitting a multipart form and you want to save the uploaded file.
What you probably need is this:
http://php.net/manual/en/function.rename.php
Change your given code as
$dir = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'uploads';
$myfile = fopen($_POST['title'].".txt", "w");
move_uploaded_file($myfile,"$dir".DIRECTORY_SEPARATOR."$title.txt");
fwrite($myfile, $_POST['textarea11']);
In your code
move_uploaded_file($myfile,'$dir/$title.txt');
php variable $dir and $title value is not coming. and value of $dir is consisting '/' and you are adding one more to make full file path too.
Always use directory separator to run in all Operating System. some OS use '/' and some OS use '\'.
I've created a php file that creates another php file,
heres the code:
if (isset($_POST["Submit"])) {
$name=$_POST['name'];
$string = '<?php
echo "'. $_POST["message"]. '";
?>
';
$fp = fopen("$name.php", "w");
fwrite($fp, $string);
fclose($fp);
}
it successfully created a php file using xampp in my local machine,
but when i upload it to my webhosting site, everytime i click submit, it doesnt create any file i written.
I dont get what is wrong with it,
somebody said that i need to connect it to my ftp server,
but i dont get it.
Advance thanks guys for the help.
Check your rigths on a dir where you want to save the file. It must be 664.
I think you have 3 things to solve here.
1) File permission
2) Rewriting a file
3) I thing there is an error on your line with $string because you are already in a php code
Try this
<?php
//If the form is submited
if (isset($_POST["Submit"]))
{
//we parse the informations
$name=addslashes($_POST['name']);
$string = addslashes($_POST["message"]);
//We will recreate the file even if it was existing
$fp = fopen("$name.php", "w+");
fwrite($fp, $string);
fclose($fp);
//We give access permission to the file
chmod("$name.php", 0777);
}
?>
And also if you are creating the file in a directory, use your ftp software like FileZilla to connect to your ftp and right click on the directory and click on Chmod. Make a Chmod 0777 on that directory. And everything should work fine.
Submit verification
Now if still it does not work, you can search bit by bit for the error by creating a file name test.php with this code
<?php
//we parse the informations
$name='my_one_file';
$string = '<h1>It is working<h1>';
//We will recreate the file even if it was existing
$fp = fopen("$name.php", "w+");
fwrite($fp, $string);
fclose($fp);
//We give access permission to the file
chmod("$name.php", 0777);
echo "<a href='$name.php' target='_blank'> <h1> $name.php</h1> </a>";
?>
And then you launch yourwebsite/test.php in your browser. If it works you should have a file name my_one_file.php on your server. If it shows a link then the problem is with your if (isset($_POST["Submit"])). Meaning we have to cross check your html form.
Touch Verification
Finaly if it is still not working then we can verify if the file needs to be created first on your server before being modified like some server do.
For that you will still create a test.php file with the following code
<?php
$name='my_one_file';
$string = 'My message';
touch("$name.php");
//We will recreate the file even if it was existing
$fp = fopen("$name.php", "w+");
fwrite($fp, $string);
fclose($fp);
//We give access permission to the file
chmod("$name.php", 0777);
echo "<a href='$name.php' target='_blank'> <h1> $name.php</h1> </a>";
?>
And then lauch yourwebsite/test.php. If it works you should have a file name my_one_file.php on your server.
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.