Page 1 of 1

Need to add a string into an html tag using php

Posted: Thu Jul 28, 2005 8:15 am
by JaK
Hi,

I need to be able to scan a folder for its contents and then output this to a file which flash can then grab a variable from.

I can get it to scan the folder, grab the contents and output it.

The problem lies with line 8 (i think). I need to add 'THE_DIR' variable with the filename to so that it read ./music/filename rather than just ./filename.

Can any1 help plz

Thanks

Chris J

Code: Select all

<?php 
//define constants as constants... (at top of file, makes it easier to read!) 
define('THE_FILENAME', './music/text.txt'); //defines the output file
define('THE_DIR', './music/'); //Defines the path to the folder we wish to catalog
$dir=opendir(THE_DIR); 
$files=array(); 
while (($file=readdir($dir)) != false) 
     $file="$THE_DIR . " " . $file"
	 array_push($files, $file); 
closedir($dir); 
sort($files); 

$handle = fopen(THE_FILENAME, 'w'); 
foreach ($files as $file) 
{ 
	 $out = "<A href='$file'>$file</a><BR>"; 
     echo $out;   //output to screen 
     fputs($handle, $out);    //output to file 
} 
fclose($handle);
?>
d11wtq | edited post to use PHP tags ;)

Posted: Thu Jul 28, 2005 8:19 am
by theda
For one thing, your quotes seem wrong to me. For one, you forgot ; at the end, plus I think a few more periods are needed. Try this out and see if it works:

Code: Select all

$file = ".$THE_DIR."".$file.";

Hi

Posted: Thu Jul 28, 2005 9:02 am
by JaK
Thnx for reply
theda wrote:For one thing, your quotes seem wrong to me. For one, you forgot ; at the end, plus I think a few more periods are needed. Try this out and see if it works:

Code: Select all

$file = ".$THE_DIR."".$file.";
Unfortunalty when I add that script I get a parser error :(

What I am tring to accomplish is adding the ./music to the filename so that they can download files from that directory.

Chris

Posted: Thu Jul 28, 2005 9:09 am
by nielsene
I would suggest adding an

Code: Select all

echo "$file";
after you set it so you can look at what its doing. I think you want:

Code: Select all

$file = THE_DIR.$file;
Note: You've defined 'THE_DIR' so its a constant, not a variable. You don't use '$' with constants and you don't put constants inside quotation marks. You can directly concatenate a string variable with a string constant, so there's no need for any quotes in this example.

Posted: Thu Jul 28, 2005 9:09 am
by Chris Corbyn

Code: Select all

$file = $THE_DIR.$file;
;)

EDIT | Dammit - too slow :P
EDIT AGAIN | And I missed the Constant 8O

Posted: Thu Jul 28, 2005 9:09 am
by choppsta
You're trying to reference a constant with $ in front of it...

Try:

Code: Select all

$file = THE_DIR.$file;

Posted: Thu Jul 28, 2005 9:14 am
by theda
I was pondering if I should have suggested removing the quotes and all... But then again, me likes quotes.

Re: Need to add a string into an html tag using php

Posted: Thu Jul 28, 2005 9:20 am
by nielsene
A few other comments...
JaK wrote:

Code: Select all

<?php 
$file="$THE_DIR . " " . $file"
Assuming $THE_DIR wasn't a constant, this would have been

Code: Select all

$file = $THE_DIR.$file; // without quotes or
$file "{$THE_DIR}$file"; // without quotes (and braces to limit possible "greediness"
$file "$THE_DIR"."$file"; // with quotes and concatenation
In your original you had the concatentation operation ('.') inside the quotes, not between the two strings.