Here is the error message that my code is giving me:
Warning: fopen(users.txt): failed to open stream: Permission denied in /opt/lampp/htdocs/assignment3/form.php on line 79
Unable to open file.
Here is my code:
$file = fopen("users.txt", "a") or die("Unable to open file.");
I've just started coding with mac so I don't know very much about it. thank you in advance for your help
Run this code to find correct folder easily
<?php
$txt = 'users.txt';
echo '<br> your php file is here : ' . __FILE__;
echo '<br> your text file should exists in : ' . __DIR__ . '/' . $txt;
echo '<br> your text file '.( file_exists($txt)?'exists':'not exists' );
echo '<br> your text file is '.( is_readable($txt)?'readable':'not readable' );
echo '<br> your text file is '.( is_writable($txt)?'writable':'not writable' );
echo '<br> your dir is '.(
is_writable(__DIR__)?
'writable, then you can create new files':
'not writable, then you can not create any files'
);
Demo
Related
Here is my simple code
$dst = "otps/hello.txt";
echo $dst, file_exists($dst) ? ' exists' : ' does not exist', "\n";
echo $dst, is_readable($dst) ? ' is readable' : ' is NOT readable', "\n";
echo $dst, is_writable($dst) ? ' is writable' : ' is NOT writable', "\n";
$fh = fopen($dst, 'w');
if ( !$fh ) {
echo ' last error: ';
echo '<pre>';print_r(error_get_last());echo'</pre>';
}
My current directory is set to 0777 but still the error, I created file manually and set permission to 0777 and still the same error.
here is output
Please note, I have setup my VPS server myself on digital ocean. I tried changing chown of the whole directories and files to apache, root, nobody but nothing is working.
running command
setenforce 0
works for me, not sure why :)
So, I am working on a system where people working in a company can upload files to a system which will be sorted by departments. I have managed to get the files info(name, size, type), but the problem occurs while trying to upload the file.
I get:
move_uploaded_file(/Advanced Java Programming.pdf): failed to open stream: Permission denied
move_uploaded_file(): Unable to move 'C:\xampp\tmp\php1B99.tmp' to '/Advanced Java Programming.pdf'
I have set the permissions of the folder where the file needs to be uploaded to everybody (777).
Here's my code
<?php
$department = $_POST['department'];
$file = $_FILES['fileToUpload'];
echo "<b>Department: </b>" . $department . "<br>";
echo "<b>Name: </b>" . $file['name']. "<br>";
echo "<b>Size: </b>" . $file['size'] . " bytes<br>";
echo "<b>Type: </b>" . $file['type'];
move_uploaded_file($file['tmp_name'], "/". $file['name']);
?>
Try using an absolute path for your destination or at least start it with the DIR-constant, "/" ist not a valid (Windows)-path.
Also think about using the constant DIRECTORY_SEPARATOR as "/" is a *nix-standard, but as you're running on Windows, it should be "\" - using the constant will hold the right slash for every system.
I just want to upload a single pdf file using cakephp,
here is my view called pdfadd.ctp:
<?php echo $this->Form->create('pdfadd1', array('enctype' => 'multipart/form-data'));?>
<fieldset>
<?php
echo $this->Form->file('Document.submittedfile');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
Here is my conroller:
public function pdfadd(){
if ($this->request->is('post') || $this->request->is('put')) {
//die();
$file = $this->request->data['Document']['submittedfile'];
//$this->pdfadd1->save($this->request->data);
move_uploaded_file($this->data['Document']['submittedfile']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/app/webroot/files/' . $this->data['Document']['submittedfile']['name']);
}
It gives me this error:
Warning (2): move_uploaded_file(D:/Program Files D/xampp/htdocs/app/webroot/files/Functions Package for email (1).pdf): failed to open stream: No such file or directory [APP\Controller\PagesController.php, line 29]
Warning (2): move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\Program Files D\xampp\tmp\php862.tmp' to 'D:/Program Files D/xampp/htdocs/app/webroot/files/Functions Package for email (1).pdf' [APP\Controller\PagesController.php, line 29]
And also I want to rename the file to 1.pdf. The file should save in webroot/files.
Replace this:
$_SERVER['DOCUMENT_ROOT'] . '/app/webroot/files/' . $this->data['Document']['submittedfile']['name']
with this:
WWW_ROOT . 'files' . DS . '1.pdf'
However, you really should do more validation, like using PHP's is_uploaded_file function, making sure the file really is a PDF, etc.
move_uploaded_file($this->data['Document']['submittedfile']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/app/webroot/files/' . $this->data['Document']['submittedfile']['name']);
1.Change this code according to directory
2.c:xampp/htdocs(its a default location for uploading in cakephp ),then put your upload file location
move_uploaded_file($this->data['Document']['submittedfile']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . 'cakephp/app/webroot/files/' . $this->data['Document']['submittedfile']['name']);
3.You can rename it before uploading
This line get only file name, not file.
$file = $this->request->data['Document']['submittedfile'];
You could use this.
$file = $_FILES ['Document'] ['submittedfile'];
Since the title of this post is pretty much self-explanatory, I'll just jump to the code :
echo sprintf('%o', fileperms('test.txt'))."<br/>";
fopen("test.txt", "w");
And with this I get :
100777
fopen(test.txt): failed to open stream: Permission denied
Any ideas ?
Edit : Problem solved : there were access control lists on the server that were not configured correctly.
Thanks !
I think its possible that you have write/read permissions on the file but not on the folder. Try this in the public root of your website and see if you can read or write the file.
For safe mode (http://php.net/manual/en/function.fopen.php), php doc's say the following:
Note: When safe mode is enabled, PHP checks whether the directory in
which the script is operating has the same UID (owner) as the script
that is being executed.
Last you also need to be sure that php has access to the folder you are trying to write to.
I had same issue: folder was 777, but fopen does not worked. fopen said permission deny. Make sure your script have a 'good' permissions. maybe it will help you:
echo $dst, file_exists($dst) ? ' exists' : ' does not exist', "\n";
echo $dst, is_readable($dst) ? ' is readable' : ' is NOT readable', "\n";
echo $dst, is_writable($dst) ? ' is writable' : ' is NOT writable', "\n";
$fh = fopen($dst, 'w');
if ( !$fh ) {
echo ' last error: ';
var_dump(error_get_last());
}
I think the problem you are having is file ownership issue ... you can use this to find out the problem
error_reporting(E_ALL);
ini_set('display_errors','On');
$file = "a.jpg";
echo sprintf ( '%o', fileperms ( $file ) ), PHP_EOL;
echo posix_getpwuid ( fileowner ( $file ) ), PHP_EOL; // Get Owner
echo posix_getpwuid ( posix_getuid () ), PHP_EOL; // Get User
if (is_file ( $file )) {
echo "is_file", PHP_EOL;
;
}
if (is_readable ( $file )) {
echo "is_readable", PHP_EOL;
;
}
if (is_writable ( $file )) {
echo "is_readable", PHP_EOL;
}
fopen ( $file, "w" );
I just ran into this issue, and unfortunately the error message provided no clue to the actual reason. I had to give 777 to the file of the class included in the file that gave the error message. The error message only said the php file that calls that class, which already had 777.
So, check the file that is mentioned in the error message (let's say index.php), and then check which classes are instantiated within that file (class-file.php, class-writer.php, etc). Then check the permissions of those files (class-file.php, class-writer.php).
Once I gave permissions for the class file, it worked normally. Perhaps the webhost changed something in their config, since everything worked until a few days ago.
From a similar question I used this code to debug the failure of opening a file
echo'<pre>';
error_reporting(E_ALL);
ini_set('display_errors', true);
echo 'phpversion: ', phpversion(), "\n";
echo 'uname: ', php_uname("s r"), "\n"; // name/release of the operating system
//echo 'sapi: ', php_sapi(), "\n";
echo $file, file_exists($file) ? ' exists' : ' does not exist', "\n";
echo $file, is_readable($file) ? ' is readable' : ' is NOT readable', "\n";
echo $file, is_writable($file) ? ' is writable' : ' is NOT readable', "\n";
$fp = #fopen($file, 'x');
if (! $fp) {
echo 'last error: ';
var_dump(error_get_last());
}
echo '</pre>
I get this helpful message
D:\data\openid\nonces\4d895d80---2jmj7l5rSw0yVb.vlWAYkK.YBwk-Bk0DdMtjVYDVZi0npvGwNNFSRy0
phpversion: 5.3.1
uname: Windows NT
D:\data\openid\nonces\4d895...FSRy0 exists
D:\data\openid\nonces\4d895...FSRy0 is readable
D:\data\openid\nonces\4d895...FSRy0 is writable
last error: array(4) {
["type"]=>
int(2)
["message"]=>
string(188) "fopen(D:\data\openid\nonces\4d895d80---2jmj7l5rSw0yVb.vlWAYkK.YBwk-Bk0DdMtjVYDVZi0npvGwNNFSRy0) [function.fopen]: failed to open stream: File exists"
["file"]=>
string(38) "D:\web\library\Utils.php"
["line"]=>
int(179)
}
"failed to open stream: File exists" - What could that mean?
This happens because you used the 'x' flag to fopen(). The PHP manual says about it:
'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
You probably want to use fopen($file, 'c') or just the ordinary w mode.