FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP: fopen('./directory/$variable'); Problem





liamthebof
$FileName = "test.php";
$fh = fopen('./premium/$FileName', 'w') or die("can't open file");

From that code, a file called $filename is made instead of test.php

Is there a way to work around this.

Help appreciated,

Liam
MrBlueSky
Change it to:

Code:

$fh = fopen("./premium/$FileName", 'w') or die("can't open file");


When you use single quotes variables are not replaced with their values. See http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double
Agent ME
Make sure if you use this script, you don't make the $filename based off of user-input, or if you do, make sure that they cannot use '../' inside it, or else they can possibly get access to other files on the server.
liamthebof
Code:

$FileName = "capop.txt";
$ourFileName = './premium/ $FileName ';


My file is still being called $FileBame. Help please.

Appreciated, Liam
imagefree
remove single Quotes and use double quotes.

Single quotes doesnt recognize a variable as variable. It just take it as part of text.

So, change ' to " and you will definitely get it right.
liamthebof
AH brilliant, misunderstood the post about single and double. Anyway, thanks to all of you helping. Your very kind people.

Appreciated, Liam
mahirharoon
Quote:
$FileName = "test.php";
$fh = fopen('./premium/$FileName', 'w') or die("can't open file");

just use
Code:

$FileName=test.php;
$fh = fopen('./premium/$FileName', 'w') or die("can't open file");

double qoutes or whithout qouotes and spaces works
note: i think a is better than w (MODE)
MrBlueSky
mahirharoon wrote:

double qoutes or whithout qouotes and spaces works
note: i think a is better than w (MODE)


Nice. I didn't know that. That the string passed to fopen gets variable-interpolated even with single quotes makes sense. But that you are allowed to leave out quotes when using strings is a bit weird.
mahirharoon
Quote:
Quote:
double qoutes or whithout qouotes and spaces works
note: i think a is better than w (MODE)


Nice. I didn't know that. That the string passed to fopen gets variable-interpolated even with single quotes makes sense. But that you are allowed to leave out quotes when using strings is a bit weird.

i said it because
without leaving it i could not use the get from what posted function
Agent ME
mahirharoon wrote:
Quote:
$FileName = "test.php";
$fh = fopen('./premium/$FileName', 'w') or die("can't open file");

just use
Code:

$FileName=test.php;
$fh = fopen('./premium/$FileName', 'w') or die("can't open file");

double qoutes or whithout qouotes and spaces works
note: i think a is better than w (MODE)

I don't think your example would work - nothing in single quotes should ever be parsed.

I just checked and that won't work.
blueray
When programming with php in most of time that
the single quote will not get parsed many things
but double quote will do.

So, most of scripts will use single quote for strings join
and double quotes for variables parsing.
sonam
Quote:
$FileName=test.php;
$fh = fopen('./premium/$FileName', 'w') or die("can't open file");


This is not right way of programing in php. Maybe Rolling Eyes some servers will parse this way but most of them (case sensitive) will produce error.

Sonam
imagefree
sonam wrote:
Quote:
$FileName=test.php;
$fh = fopen('./premium/$FileName', 'w') or die("can't open file");


This is not right way of programing in php. Maybe Rolling Eyes some servers will parse this way but most of them (case sensitive) will produce error.

Sonam


what is case sensitive problem here>?
Fire Boar
These will work:
Code:
$a = "My name is $b";
$a = 'My name is '.$b;
$a = "My name is ".$b;


This will not:
Code:
$a = 'My name is $b';


Also, if you're going to include other parts in the variable part of the string, you can use {} to seperate the variable from the rest of the text like this:

Code:
$beer = 'Heineken';
echo "$beer's taste is great"; // works, ' is an invalid character for varnames
echo "He drank some $beers";   // won't work, 's' is a valid character for varnames
echo "He drank some ${beer}s"; // works
echo "He drank some {$beer}s"; // works


More funky string syntax that works - you can use {} to do arrays and classes:

Code:
// Note that ' must be used around the array key value (width in this case) or else it will try and find a constant named "width".
echo "The width of the third rectangle is {$rectangle[3]['width']}.";
echo "The width of the third rectangle is {$rectangle->third->width}.";


And finally, let's have a look at the good ol' Heredoc string notation. I quite like this myself. It works in exactly the same way as double quoted strings, except that it doesn't use quotes at all, and is only ever terminated by a set pattern (user defined, so nothing except $ needs escaping):

Code:
$variable = <<<TEXT
I can keep on typing across multiple lines, doing whatever I want.
All this stuff will be added to \$variable.
I like $favourite best of all. On a scale of 1 to 10, I would rate it {$ratings[$favourite]}.
TEXT;
sonam
Quote:
what is case sensitive problem here>?


Servers. Linux and Unix servers are more sensitive then Win. Maybe I am not true but this is my experience. I have Win on my computer. Some script what I am create and run on Win without any problem don't work on Linux. When I change few simple things everything is OK.

Sonam
Stubru Freak
Case sensitive means the case (capital letters or not) of a filename is important.

And about this code:
Code:
$FileName=test.php;


$FileName will contain "testphp" unless test or php are defined as constants.
PHP sees the dot and tries to append php to test, it can't find any of the two constants so it falls back to literal strings. It also gives a warning.
sonam
Quote:
Case sensitive means the case (capital letters or not) of a filename is important.


Jeah, you are right and I am using wrong words. Problem is with my English knowledge. But, I am absolutely sure how programing for *nix servers need more attention then for Win.

Sonam
mahirharoon
do not use fopen on frih host
because fopen is disabled in frihost
i think this code works
Code:

$FileName = "./premium/test.php; "
$fh = fopen('$FileName', 'w') or die("can't open file");

instead of
Quote:

Code:

$FileName = "test.php";
$fh = fopen('./premium/$FileName', 'w') or die("can't open file");
 

Stubru Freak
mahirharoon wrote:
do not use fopen on frih host
because fopen is disabled in frihost
i think this code works
Code:

$FileName = "./premium/test.php; "
$fh = fopen('$FileName', 'w') or die("can't open file");

instead of
Quote:

Code:

$FileName = "test.php";
$fh = fopen('./premium/$FileName', 'w') or die("can't open file");
 



Of course that code doesn't work.

This code will work:
Code:

$FileName = "./premium/test.php";
$fh = fopen($FileName, 'w') or die("can't open file");


But this also works:
Code:

$FileName = "test.php";
$fh = fopen("./premium/$FileName", 'w') or die("can't open file");
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.