Need to add a string into an html tag using php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JaK
Forum Newbie
Posts: 2
Joined: Thu Jul 28, 2005 8:02 am

Need to add a string into an html tag using php

Post 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 ;)
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post 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.";
JaK
Forum Newbie
Posts: 2
Joined: Thu Jul 28, 2005 8:02 am

Hi

Post 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
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

$file = $THE_DIR.$file;
;)

EDIT | Dammit - too slow :P
EDIT AGAIN | And I missed the Constant 8O
Last edited by Chris Corbyn on Thu Jul 28, 2005 9:12 am, edited 2 times in total.
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

You're trying to reference a constant with $ in front of it...

Try:

Code: Select all

$file = THE_DIR.$file;
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

I was pondering if I should have suggested removing the quotes and all... But then again, me likes quotes.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

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

Post 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.
Post Reply