简体   繁体   中英

PHP: Setting permissions so I can write a file?

I am trying to create a file with PHP here is my code:

$file_to_send = 'ftp_test_from_created_file.txt';
chmod($file_to_send, 0777);
$fh = fopen($file_to_send,'w');
$tsv_line = 'Hey\tThese\tAre\tTab\tSeperated\tValues';
fwrite($fh, $tsv_line);
fwrite($fh, '\n');
$tsv_line = 'Hey\tThese\tAre\tToo';
fwrite($fh, $tsv_line);
fclose($fh);

This clearly gives me an error because I am trying to set permissions on a file that does not exist yet. Although if I skip the chmod call I get an another error telling me I do not have permissions set. So either I am in a sick chicken vs egg cycle or I need to set the permissions on the containing directory...but how? it tried all of the folling:

chmod('../containingfolder/', 0777);
chmod('../', 0777);
chmod('', 0777);

None of which work... what should I do? Oh and by the way once I am done do I need to change the permissions back?

Thanks!

UPDATE I tried chmod('.', 0777) and this is the error I got:

Warning: chmod(): Operation not permitted

The only reason I am trying to make this file is so I can FTP it somewhere else, then I planned to unlink() it. So if there is a better way? Like maybe storing the file in memory to send it(don't know if thats possible)? Also I am using PHP4. Thanks!

It could also be that the apache server running (if it is apache) does not have access to change the permissions on that directory. It could have root / root as user / group and apache runs on apache group or wwwuser or something else.

Do a ls -l on the parent directory and see what user and group has access to this folder.

To change the permission of the current directory, use

chmod('.', PERMISSION);

Do you really need 0777, ie world read- and writable? Seems very lax to me!

Edit : Your suggestion chmod('../containingfolder/', 0777); should have worked too. Can you be more verbose on the errors you get?

Create a directory outside your document root which we will call $tmpFolder make sure that the user that the webserver is running as has read/write permissions to it. Then the following:

$file_to_send = $tmpFolder . '/ftp_test_from_created_file.txt';
chmod($file_to_send, 0777);
$fh = fopen($file_to_send,'w');
$tsv_line = 'Hey\tThese\tAre\tTab\tSeperated\tValues';
fwrite($fh, $tsv_line);
fwrite($fh, '\n');
$tsv_line = 'Hey\tThese\tAre\tToo';
fwrite($fh, $tsv_line);
fclose($fh);

You can't set the permissions of an entry if the user running php can't modify it. Look at apache's config, search for the user it is running with (by default it is www-data). To avoid these in the future, set yourself to the same group as default group, and give document root chmod -R g+a

If the current user can write to the directory, you should be able to simplify all of that with file_put_contents() .

<?php
$file = 'file_to_write.txt';
$data = "Tab\tseparated\tvalues\nMore\tseparated\tvalues";
file_put_contents($file);

If you don't have permissions to write to the current directory (or whatever directory) you won't be able to create the file, let alone chmod it.

If you could post the errors you get from your chmod() commands, that would help.

chmod('.',0777);

should work.

如果您想维护chroot监狱,但仍然需要chmod当前目录的特权,则必须弄清楚如何将sudo与-S选项一起使用,并将根密码传递给STDIN以便获取特权。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM