tags:

views:

262

answers:

5
+1  Q: 

php: writing files

Hi All,

I want to create a file on the webserver dynamically in PHP.

First I create a directory to store the file. THIS WORKS

// create the users directory and index page
$dirToCreate = "..".$_SESSION['s_USER_URL'];
mkdir($dirToCreate, 0777, TRUE); // create the directory for the user

Now I want to create a file called index.php and write out some content into it.

I am trying:

$ourFileName = $_SESSION['s_USER_URL']."/"."index.php";
$ourFileHandle = fopen($ourFileName, 'x') or die("can't open file");
fclose($ourFileHandle);

// append data to it
$ourFileHandle = fopen($ourFileName, 'a') or die("can't write to file");

$stringData = "Hi";

fwrite($ourFileHandle, $stringData);

But it never gets past the $ourFileHandle = fopen($ourFileName, 'x') or die("can't open file"); Saying the file does not exist, but that is the point. I want to create it.

I did some echoing and the path (/people/jason) exists and I am trying to write to /people/jason/index.php

Does anyone have any thoughts on what I am doing wrong?

PHP 5 on a linux server I believe.

-Jason

A: 

It could be a result of one of your php ini settings, or possibly an apache security setting.

Try creating the dir as only rwxr-x--- and see how that goes.

I recall a shared hosting setup where "safemode" was compiled in and this behaviour tended to occur, basically, if the files/dirs were writable by too many people they would magically stop being acessible.

Its probably doc'd in php, but ill have to check.

Kent Fredric
In my php.ini I have:safe_mode = OffThis is shared hosting through Hosting Zoom. How can I check how they compiled php? is it phpinfo()?Jason
if Safe_mode is off its probably just path-confusion. If you are sure your path is right, try different combinations of RWX/RWX/RWX on the dir sperms and see what eventuates.
Kent Fredric
+5  A: 

First you do :

$dirToCreate = "..".$_SESSION['s_USER_URL'];

But the filename you try to write to is not prefixed with the '..', so try changing

$ourFileName = $_SESSION['s_USER_URL']."/"."index.php";

to

$ourFileName = '..' . $_SESSION['s_USER_URL'] . '/index.php';

or probably tidier:

$ourFileName = $dirToCreate . '/index.php';

You are probably getting the warning because the directory you are trying to write the file into does not exist

Tom Haigh
A: 

why not use:

file_put_contents( $filename, $content )

or you could touch the file before writing to it.

Kris
A: 

Does the file 'index.php' already exist? When you fopen with the 'x' mode, if the file exists fopen will return FALSE and trigger a warning.

Zoredache
A: 

What i first noticed is you are making a directory higher in the tree, then attempting to make the php file in the current folder. Correct me if i'm wrong, but aren't you trying to make the file in the new created folder? if i recall php correctly (pardon me it's been a while, i'll probably add something from another language in here not noticing) here is an easier to understand way for a beginner, of course change the values accordingly, this simply makes a directory and makes a file then sets permissions.

<?php

$path = "..".$_SESSION['s_USER_URL'];   
// may want to add a tilde (~) to user directory
// path, unixy thing to do ;D

mkdir($path, 0777); // make directory, set perms.

$file = "index.php"; // declare a file name

/* here you could use the chdir() command, if you wanted to go to the 
directory where you created the file, this will help you understand the 
rest of your code as you will have to perform less concatenation on
 directories such as below */

$handle = fopen($path."/".$file, 'w') or die("can't open file");
// open file for writing, create if it doesn't exist

$info = "Stack Overflow was here!";  // string to input

fwrite($handle, $info);   // perform the write operation

fclose($handle);  // close the handle

?>
John T