tags:

views:

909

answers:

6

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!

A: 

To change the permission of the current directory, use

chmod('.', PERMISSION);

Do you really need 0777, i.e. 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?

rodion
May not work on the containing folder if php is running as a module and the webserver doesn't have permissions to chmod the containing directory (or from the CLI if the user calling the script doesn't either).
Swish
+1  A: 

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.

Ólafur Waage
A: 

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);
Jordan S. Jones
A: 

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

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.

James Socol
A: 

If you want to maintain the chroot jail, yet still need the privileges to chmod the current directory, you're going to have to figure out how to use sudo with the -S option and pipe the root password to STDIN in order to get the priviledges.

Jason