Php: writing files

I want to create a file on a web server dynamically in PHP.

First, I create a directory to store the file. THIS IS 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 an index.php file and write some content to it.

I'm trying to:

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

// 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 $ourFileHandle = fopen($ourFileName, 'x') or die("can't open file");

Saying that the file doesn't exist, but that's the point. I want to create it.

I echo echoed and the path (/ people / jason) exists and I am trying to write /people/jason/index.php

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

PHP 5 on a Linux server I believe.



source to share

5 answers

First you will do:

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


But the filename you are trying to write is not prefixed with "..", so try changing

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



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


or probably tidier:

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


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



It could be the result of one of your php ini settings, or maybe apache security settings.

Try to create a directory just like rwxr-x --- and see how it goes.

I remember a shared hosting setup where "safemode" was compiled, and generally this behavior occurred mainly if the files / dirs were writable by too many people that they would magically cease to be available.

It was probably in php but needs to be checked.



why not use:

file_put_contents( $filename, $content )


or you could touch

save the file before writing it.



Does the index.php file already exist? When you open "x" mode, if the file exists, fopen will return FALSE and raise a warning.



I first noticed that you create a directory higher up in the tree and then try to make a php file in the current folder. Correct me if I'm wrong, but aren't you trying to make the file in the newly created folder? if i name php correctly (sorry for that, i'll probably add something from another language here without noticing) it is easier to figure out a way for newbies here, of course change the values ​​accordingly, this just makes the directory and makes the file then installs permissions.


$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 = "Qaru was here!";  // string to input

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

fclose($handle);  // close the handle





All Articles