$myFile = "folder1/folder2/order.txt";
$fh = fopen($myFile, 'a');fwrite($fh, $Message);
fclose($fh);
I am use this code.But file is not create in that particular folder
Try this:
<?php
$myFile = "folder1/folder2/order.txt";
$fh = fopen($myFile, 'a'); or die("Unable to open the file");
fwrite($fh, $Message);
fclose($fh);
?>
If this code shows "Unable to open the file" that means there is something wrong while opening the file. This may happen if the folder1/folder2 doesn't exist. Or may be you are not permitted to open a file to write on that folder. If you are in Linux you can change the permission of you directory like this:
chmod 777 folder/folder2
Related
i am trying to read contents from a text file in php. i am using wamp on windows. i m getting this error:
Warning: fopen(/input.txt): failed to open stream: No such file or directory in C:\wamp\www\cycle_gps_sender.php on line 3
this is my code:
$location = fopen("/input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);
both the php file and input.txt are placed in www folder of wamp.
Hope this will help you:
$File = "log_post.txt";
$fh = fopen ($File, 't') or die("can't open file");
fclose($fh);
$location = fopen("input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);
Use this code and keep the input.txt file in the same directory where this code is written.
First check if file exist or not?
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
chmod($filename, 0777);
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
$location = file_get_contents('./input.txt', FILE_USE_INCLUDE_PATH);
echo $location;
or
$location = file_get_contents('input.txt');
echo $location;
hope it will help
Add full path to file.
On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes.
$location = fopen("C:\\folder\\input.txt", "r");
Remove '/'(Slash)
$location = fopen("input.txt", "r") or die("Unable to open file!");
$file = fopen("path/input.txt","a+") or die("Unable to open file!");
.....
fclose($file);
You have to create file before you READ or if you open file by 'r' , so if you open file by 'a+' , your file will be created automatically.
So basically, I have a form that opens a php file when submitted, i have the php writing to a file, but it will not continue adding values, the text file only has one "1" inside of it when it is supposed to have a "1" inside for every time the form has been submitted. Here is my code.
<?php
$myFile = "Data.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "1";
fwrite($fh, $stringData);
fclose($fh);
?>
Any ideas?
Either open in append only format as Robert Rozas stated: fopen($myFile, 'a+');
Or get the contents and do the append manually some pseudo[esk]-code:
$contents = file_get_contents("somefile.txt");
//generate what to write
$contents .= $whatIGenerated;
$success = file_put_contents("somefile.txt", $contents);
Change this one line to this:
$fh = fopen($myFile, 'a') or die("can't open file");
Using w means write, using a means append.
I am using version 5.1.6 and am observing a strange issue. I cannot create and write to a file from the script whereas if I explicitly create a file and then run the script it writes data.
Am I missing something obvious in here?
The test code I am trying is:
$message = "Test";
$myFile = "testFile.txt";
if (file_exists($myFile)) {
$fh = fopen($myFile, 'a');
fwrite($fh, $message."\n");
} else {
chmod("/path/to/dir/*", 0755); //updated code
$fh = fopen($myFile, 'w') or die("Cannot open file \"$myFile\"...\n");
fwrite($fh, $message) ;
}
fclose($fh);
CONCLUSION:
Thanks for the responses everybody. It is a permission issue. I changed the directory path and it works :)
Your code is fine. Only the line where chmod resides, is not required.
Commented out chmod("/path/to/dir/*", 0755); this will chmod all files within the set folder.
Consult the PHP manual on chmod at
http://php.net/manual/en/function.chmod.php
$message = "Test";
$myFile = "testFile.txt";
if (file_exists($myFile)) {
$fh = fopen($myFile, 'a');
fwrite($fh, $message."\n");
} else {
//chmod("/path/to/dir/*", 0755);
$fh = fopen($myFile, 'w') or die("Cannot open file \"$myFile\"...\n");
fwrite($fh, $message) ;
}
fclose($fh);
I had a similar problem and solved it by changing the owner of the folder to the apache user. This should give your php script needed permissions for making files and writing to the files in that folder.
I guess you won't be able to chown the folder from php script, only through server access (ssh or ftp). At least, that was the path I had to go.
I am a php beginner.I want every time when the web page is open to create a file that does not exist. But every time when I run the program I have an error teling me that the file was not created.
This is my code:
$ip=$_SERVER["REMOTE_ADDR"];
if(!isset($_COOKIE['firsttime'])){
setcookie('firsttime', 'no');
$myfile = 'file/form'.$ip.'.txt';
if(file_exists($myfile) == FALSE){
$fo = fopen($myfile, 'w');
$code = '<form action = "" method = "post">';
fwrite($fo, $code);
fclose($fo);
}else{
unlink($myfile);
$file = new File();
}
}
where is my mistake?
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
Do this to open a file, and it will create the file if it doesn't exist.
Not completely sure but that would result in a pretty weird file name.
$myfile = 'file/form'.$ip.'.txt';
If my ip is 1.0.0.01.23 (really random and pretty weird) the file name would be:
file/form.1.0.0.01.23..txt
Try saving a file with that name in notepad.
$new_file=$data['url']."_files";
if(!is_dir($new_file))
mkdir ($new_file);
echo $new_file=$new_file.'\class.ticket.php';
$ourFileHandle = fopen($new_file, 'w') or die("can't open file");
echo fwrite($ourFileHandle, $new_data) or die('cannot write');
fclose($ourFileHandle);
fwrite() returns 1473
this does not write new file in other directory but creates file '$newfile\class.ticket.php in the same directory.
Can anone explain how i can put file in new directory.
Thanks
On what operating system is your server running? in linux, directory separator is a forward slash, so maybe that's causing a problem. use the predefined constant DIRECTORY_SEPARATOR to return the correct separator for the OS.
Try this:
$data['url'] = 'foo';
$new_data = '<?php echo \'Hello World!\';';
$new_file=$data['url']."_files";
if(!is_dir($new_file))
mkdir ($new_file);
$new_file=$new_file.'/class.ticket.php';
$ourFileHandle = fopen($new_file, 'w') or die("can't open file");
fwrite($ourFileHandle, $new_data) or die('cannot write');
fclose($ourFileHandle);