PHP file and/or directory creation trouble

PHP file and/or directory creation trouble

Posted by: username
Posted on: 2007-05-22 15:44:00

I'm not sure if I am going wrong in creating the directory or the file. Usually the error message seems to be concerning the txt file.

Here is a simplified version of my script:

<?php

//get form fields from previous page
$alias = trim($_POST["alias"]);//account for spaces
$search = addcslashes($_POST["searchterms"], '"');
$agree = $_POST["agreement"];

//check required fields
if($agree == NULL || $alias == NULL || $search == NULL)
{
die('<h1>Error:  You must complete all required fields.</h1></p>');
}


if(!is_dir($alias))
{
mkdir("$alias", 0700);//create the directory
}


$file = fopen("/$alias/display.txt", "w+");//create file

if(!$file)//check if previous step failed
{
die("Error creating profile content.");
}

$chmod = chmod("/$alias/display.txt", 0755);//set permissions

if(!$chmod)//check if failed
{
die("Error creating permissions for profile.");
}

fwrite($file, "NEW PROFILE SUBMISSIONn");

//get and write user's details
$usrip = $_SERVER['REMOTE_ADDR'];
$tdate = gmdate(DATE_RFC822);
fwrite($file, "Recieved: " . $tdate . "; " . $usrip . "; " . $alias . "n-------------------------nn");

fclose($file);

?>

If somebody sees something wrong, please help me out.

Re: PHP file and/or directory creation trouble

Posted by: scjessey
Posted on: 2007-05-22 16:07:00

What is the error message you are getting?

Re: PHP file and/or directory creation trouble

Posted by: username
Posted on: 2007-05-23 13:18:00

Usually I get something like this when I try it.
function.fopen]: failed to open stream: No such file or directory

I do notice that when it creates the directory the permissions look different (it says rwx------ and the rest of the directories show rwxr-xr-x, sorry I don't know much about chmoding so I don't know if this is a problem or not, but I think the program would be considered the owner, and therefor have access no matter what).

Re: PHP file and/or directory creation trouble

Posted by: scjessey
Posted on: 2007-05-23 14:34:00

If it cannot find the file, try using a relative path.

Re: PHP file and/or directory creation trouble

Posted by: username
Posted on: 2007-05-23 15:41:00

I think that already is the relative path. It's layed out like this...
|-a directory
|--the directory we are creating
|--the file that creates the directory above

So from the file creating the directory, the path would start out with the name of the directory, right?
$file = fopen("/$alias/display.txt", "w+");//create file

Is there another way that I don't know about?

Re: PHP file and/or directory creation trouble

Posted by: silkrooster
Posted on: 2007-05-23 21:14:00

Do a print of $alias and verify it is correct. Also you may need to use period in the directory.
Example:

./folder/image.jpg

Silk

My website

Re: PHP file and/or directory creation trouble

Posted by: username
Posted on: 2007-05-24 16:16:00

In reply to:

./folder/image.jpg


That works. Thank you both for your help. I really appreciate it.

Tags: php fileerror message