I am saving a file on my server through file handling like this
$my_file = $_SERVER['DOCUMENT_ROOT'].'/wp-content/themes/mythemev2.0/'.$name.'.php';
echo $my_file;
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
fwrite($handle, $res->name);
$my_file perfectly shows me the complete path of the file. But when I go to the particular directory I am unable to see the file. What could be the error? What else can I do for debugging ?
Make sure you're calling fclose() after fwrite()
Related
I am trying to create and write a file to ec2 instance. I am getting the error below despite the folder that i am writing possessing all the permissions required.
$handle = fopen("thumbnailFolder/testFile.txt", "r"); // line 4
if($handle != false){
echo 'File created!';
}else{
echo 'File create failed!';
}
The 'thumbnailFolder' has the following permissions:
drwxrwxrwx
Error message:
fopen(thumbnailFolder/testFile.txt): failed to open stream: No such file or directory in /var/www/html/book_aws/my_server/folder/web/thumbnailTest.php on line 4
File create failed!
As the error clearly say. System is failing to open the file which is not there.
$handle = fopen("thumbnailFolder/testFile.txt", "r");
Above code open files in read mode. if there is no files then throws an error.
If you want to open file to write then use try below code, this tries to open file and sets pointer at the begining if file is not there then creates the file in given name. for read and write use w+ instead of w
$handle = fopen("thumbnailFolder/testFile.txt", "w");
There are different modes with respect files. you can check below link for further details.
http://php.net/manual/en/function.fopen.php
I'm trying to save some text that comes into a php script via a POST to a file. Here is my script:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$file = $path . '\test.txt';
$text = $_POST['text'];
// save it to a file
if (file_exists($file))
chmod($file, 0777);
$handle = fopen($file, 'w');
fwrite($handle, $text);
fclose($handle);
echo "success";
?>
I'm getting this error:
Warning:
fopen(D:\Hosting\11347607\html\test.txt) [function.fopen]: failed to open stream:
Permission denied in
D:\Hosting\11347607\html\test_file_saver.php on line
12
I've tried a number of different attempts and read many posts. The directory is set to RWX permission. How can I get permission to write to this file? Thanks!
If you don't have permissions to even open the file, what makes you think you have permission to change permissions?
That wasn't a question or a comment. You don't have permission to change the permission. Give your PHP process proper permissions.
I cannot update any txt files using php. When I write a simple code like the following:
<?php
// create file pointer
$fp = fopen("C:/Users/jj/bob.txt", 'w') or die('Could not open file, or fike does not exist and failed to create.');
$mytext = '<b>hi. This is my test</b>';
// write text to file
fwrite($fp, $mytext) or die('Could not write to file.');
$content = file("C:/Users/jj/bob.txt");
// close file
fclose($fp);
?>
Both files do exist in the folder. I just cannot see any updates on bob.txt.
Is this a permission error in windows? It works fine on my laptop at home. I also cannot change the php files on my website, using filezilla.
It may very well be a file permissions issue.
Try your code using a direct pointer to the file instead of a path to it, using the following code:
I added a chmod directive. See the comments above chmod ($file, 0644);
Tested succesfully on my hosted WWW website:
<?php
// create file pointer
$file = "bob.txt";
$fp = fopen($file, 'w') or die('Could not open file, or fike does not exist and failed to create.');
// chmod ($file, 0777); // or use 0777 if 0644 does not work
chmod ($file, 0644);
$mytext = '<b>hi. This is my test</b>';
// write text to file
fwrite($fp, $mytext) or die('Could not write to file.');
$content = file("bob.txt");
// close file
fclose($fp);
?>
Perhaps you have to set the permission from bob.txt to 0777 (or something else). In FileZilla it is very easy, because you can just check the permissions you want.
I want to HTTP POST data to a PHP file which will in-turn write the data into a file. The script I used is as follows,
<?php
#error_reporting(E_ERROR | E_PARSE);
$msglen=strlen($_POST["msgarea"]);
$msg=$_POST["msgarea"];
$fp = fopen("dinesh.txt", 'w');
fwrite($fp, $msg);
fclose($fp);
echo "Data Written -> $msg";
?>
I am hosting this script file in sourceforge.
I have already just created the empty file dinesh.txt and placed the file in the same directory as that of the script file. But unfortunately its not written in the file.
What is the reason ?
Thanks in advance
Check that $_POST["msgarea"] is not empty and the file is writeable for the user who tries to write it.
And check the logs for errors of course.
<?php
error_reporting(E_ALL);
var_dump($_POST["msgarea"]);
$msglen=strlen($_POST["msgarea"]);
$msg=$_POST["msgarea"];
$fp = fopen("dinesh.txt", 'w');
$result = fwrite($fp, $msg);
fclose($fp);
if ($result) {
echo "Data Written -> $result";
} else {
echo "Error";
}
I think you are writing in read-only webspace, check file permissions and path
From: http://sourceforge.net/apps/trac/sourceforge/ticket/2772
In generally it is not allowed to write to folders and files that reside in the project's web space. If you need to write to the file system you should use the folder named persistent that is on the same level as htdocs.
I use on my computer:
$ mkdir persistent
$ chmod 0777 persistent
$ scp -r persistent my_account#web.sourceforge.net:/home/project-web/my_project/
And set in PHP:
$fp = fopen("../persistent/dinesh.txt", 'w');
And it works!
Edit:
You can get access for SSH console for 4 hours (https://sourceforge.net/apps/trac/sourceforge/wiki/Shell%20service). And you can go to web directory of your project, make dirs, set privileges, remove files etc. Midnight Commander is available.
you can, check that folder permission, do they have 777 permission
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Bobby Bopper\n";
fwrite($fh, $stringData);
fclose($fh);
I have the following code:
$cachefile = "http://www.DOMAIN.com/users/{$user}.php";
$fp = fopen($cachefile, 'w'); // open the cache file "cache/home.html" for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_clean(); // Send the output to the browser
chmod($cachefile, 0644);
The file path is not valid, as it produces this error: Warning: chmod() [function.chmod]: No such file or directory in ...
However, this code does:
$cachefile = "{$user}.php";
$fp = fopen($cachefile, 'w'); // open the cache file "cache/home.html" for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_clean(); // Send the output to the browser
chmod($cachefile, 0644);
The file--using the second block of code--is created in the same folder as the script that created it. However, I would like to save it to the first block's location. I look on php.net for fopen and I didn't see that I was doing anything wrong, but obviously I am.
EDIT (comment response):
I also tried $cachefile = $_SERVER["DOCUMENT_ROOT"] . "/users/{$user}.php" to no avail.
You cannot write to a remote file using fwrite(). If you're writing to a file that is on your own server, use a relative path.
You need to use the full path to the file on the server, not the web address:
$cachefile = "/var/www/path/to/users/{$user}.php";
You cannot open remote files, you can only edit files on the local filesystem, if the file is on the file system, then you have to use the relative path,
Also instead of using fopen, you could just use file_get_contents() and file_put_contents().