PDA

View Full Version : PHP help



MrPinkPanther
24-06-2009, 02:54 PM
Ok, I just need help with this and then I'm done


<?php
$x = $_POST['position'];
$user = $_POST['user'];

$myFile = <here>;
$fh = fopen($myFile, 'w') or die("can't open file");

fwrite($fh, $x);
fclose($fh);

echo "success";

?>
Firstly I want myFile (the bit in bold) to equal "user".txt so if user = John then myFile John.txt, how would I do that?

Secondly I want it so if the textfile doesn't exist then it creates it with permissions "7 7 7".

Could someone maybe tell me how to do it and then the PHP side of things is all done :D

Jam-ez
24-06-2009, 05:12 PM
Ok, I just need help with this and then I'm done


Firstly I want myFile (the bit in bold) to equal "user".txt so if user = John then myFile John.txt, how would I do that?

Secondly I want it so if the textfile doesn't exist then it creates it with permissions "7 7 7".

Could someone maybe tell me how to do it and then the PHP side of things is all done :D


<?php
$x = $_POST['position'];
$user = $_POST['user'];

$myFile = $user . '.txt';

$fh = fopen($myFile, 'w') or die("can't open file");

fwrite($fh, $x);
fclose($fh);

if ( !file_exists( $myFile ) )
{

chmod( $myFile, 0777 );

}

echo "success";

?>

Did that off the top of my head, try it!

Protege
24-06-2009, 05:51 PM
So James you only chmod the file if it doesnt exist, that makes total sense.

MrPinkPanther
24-06-2009, 07:05 PM
<?php
$x = $_POST['position'];
$user = $_POST['user'];

$myFile = $user . '.txt';

$fh = fopen($myFile, 'w') or die("can't open file");

fwrite($fh, $x);
fclose($fh);

if ( !file_exists( $myFile ) )
{

chmod( $myFile, 0777 );

}

echo "success";

?>

Did that off the top of my head, try it!
Thanks a bunch! REALLY REALLY helpful, its people like you that make this forum great.


So James you only chmod the file if it doesnt exist, that makes total sense.
I wondered that too, maybe he forgot to put the "create file" bit in first if it doesnt exist. Any ideas how I can sort that out?

Protege
24-06-2009, 07:41 PM
<?php
if( ! file_exists( $myFile ) )
{
chmod( $myFile, 0777 );
}

$fh = fopen( $myFile, 'w+' );

Invent
24-06-2009, 07:51 PM
<?php
if( ! file_exists( $myFile ) )
{
chmod( $myFile, 0777 );
}

$fh = fopen( $myFile, 'w+' );

You're still chmoding it even though it doesn't exist?

Protege
24-06-2009, 08:55 PM
I wasnt paying attention to what I was doing sorry.


<?php
if( ! file_exists( $myFile ) )
{
$fh = fopen( $myFile, 'w' );

chmod( $myFile, 0777 );
}
else
{
$fh = fopen( $myFile, 'w' );
}

Dentafrice
25-06-2009, 01:31 AM
Oh the irony :P

Want to hide these adverts? Register an account for free!