$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.
Related
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 '\'.
We have few fields in the HTML page and this is being written into a file using php.
The file has the data, however, running a bash script which takes this file with a .txt as extension is not working.
When the file is opened and re-saved manually the bash script will work properly!
I've tried changing the permissions of the file but the bash script is still not using this file. Any help on this is greatly appreciated.
PHP Script:
$name = "test.txt";
$handle = fopen($name, "w");
fwrite($handle, "my message");
fclose($handle);
Bash Script:
INPUT="$1"
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read message number ; do
echo $message # or whaterver you want to do with the $line variable
#j=$[$(line)]
echo $number
echo "$message" | gnokii --sendsms $number --smsc $SMSC
done < $INPUT
IFS=$OLDIFS
You probably want to use fclose($handle) rather than unlink()
Edit OK then, "Because..."
fclose($handle) closes the file referenced by the handle: $handle
unlink() takes a file path string as a parameter, rather than a resource.
If used as intended, unlink() will actually delete the file.
unlink() as mentioned would delete the file. Just created a plain script like this
<?php
$name = "/var/www/test.txt";
$handle = fopen($name, "w");
fwrite($handle, "test,9944");
fclose($handle);
?>
This script works f9 with storing the data. But running bash script on this with the file input as test.txt does nothing. But again opening and saving with the same file name and it works.
finally got the answer its the end of file that is missing in text file that is created by php. anyway thank you all for contributing.
I am trying to create a file using php to a dirctory which has cmod 0777 so it should be fine.
Here is the code:
$fh = fopen("/_myfiles/myfile.txt", "w+");
if ($fh==false)
{
die("unable to create file");
}
But all I get is "unable to create file". Any ideas on what it could be?
Note: For the path I've also tried:
$fh = fopen($_SERVER['DOCUMENT_ROOT']."/_myfiles/myfile.txt", "w+");
with no success.
fopen() generates an E_WARNING message on failure.
I recommend using error_reporting(E_ALL) to show the warning and this should help you to troubleshoot the problem from there.
Check write permissions on the directory you want to create the file in.
Also the directory "_myfiles" should exist (it won't be created automatically).
If they are correct, then this will create the file in the same directory where the PHP script is located:
$basedir = dirname(__FILE__);
$fh = fopen($basedir . DIRECTORY_SEPARATOR . 'myfile.txt', 'w+');
I am able to put the file contents if I stay in the same directory level
ie: `file_put_contents('cache.txt', $result);
But how would I put the contents up a folder, or even two? Adding '../cache/cache.txt' does not seem to work.`
That would work fine assuming the user the web server runs as has write permission to that directory.
Try using the direct path of where you would like to put the cache content.
<?php
$file = 'people.txt';
$cacheFile = __FILE__ . '/cache/'. $file;
$content = 'Something';
file_put_contents($file, $content);
file_put_contents($cacheFile, $content);
?>
Assuming you have a folder called cache, with write permissions to 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.