0

its been a long time since i've done any PHP coding so I'm really back to square one, apologies in advance if I am missing something really obvious/trivial.

I am trying to create a form, which processes and submits the data and appends a piece of code to a specific section of an XML file.

This is what I've got

<? $title = $_POST['title'] ;  
$author = $_POST['author'];
$filesize = $_POST['filesize'];
$pubdate = $_POST['pubdate']; 
$duration = $_POST['duration']; 
$summary = $_POST['summary']; 



$key = '<itunes:category text="Business"></itunes:category>';
$newline = '<item>
<title>$title</title>
<link>http://example.com/CS/podcast/<? php echo($target_path);?></link>
<itunes:author>$author</itunes:author>
<itunes:summary><?php echo($summary); ?></itunes:summary>

<enclosure url="http://example.com/CS/$targetpath" length="$filesize" type="audio/mpeg"/>

<guid>http://www.example.com/CS/podcast/<? php echo($target_path);?></guid>
<pubDate><?php echo($pubdate);?></pubDate>
<itunes:duration><?php echo($duration);?></itunes:duration>
<itunes:keywords>My Podcast</itunes:keywords>
<category>Podcasts</category>
<itunes:explicit>no</itunes:explicit>
</item>';


//copy file to prevent double entry
$file = "list.xml";
$newfile = "listtemp.xml";
copy($file, $newfile) or exit("failed to copy $file");

//load file into $lines array
 $fc = fopen ($file, "r");
 while (!feof ($fc)) 
 {
$buffer = fgets($fc, 4096);
$lines[] = $buffer;}

fclose ($fc);

//open same file and use "w" to clear file 
$f=fopen($newfile,"w") or die("couldn't open $file");

/* uncomment to debug
print_r($lines);
print "<br>\n";
*/

//loop through array using foreach
foreach($lines as $line){
   fwrite($f,$line); //place $line back in file    
if (strstr($line,$key)){ //look for $key in each line
fwrite($f,$newline."\n");
} //place $line back in file }

fclose($f);

copy($newfile, $file) or exit("failed to copy $newfile");
echo "done";
echo "$newline";?> 

The issue I seem to have is that it appends the code perfectly into the right place, but displays $title instead of the inputted title the same is true of

I think the issue is that you cannot have a variable within another variable, since it could be dangerous if anyone could just type code into forms, is there a way around this? I don't even need tight security as this would be soley for internal use.

Found

http://uk3.php.net/manual/en/function.eval.php but not been able to put it to good use.

Thank-you

EntropicFox
  • 727
  • 5
  • 11

5 Answers5

1

try:

$newline = '<item>
<title>'.$title.'</title>
<link>http://example.com/CS/podcast/'.$target_path.'</link>
<itunes:author>'.$author.'</itunes:author>
<itunes:summary>'.$summary.'</itunes:summary>

<enclosure url="http://example.com/CS/'.$targetpath.'" length="'.$filesize.'" type="audio/mpeg"/>

<guid>http://www.example.com/CS/podcast/'.$target_path.'</guid>
<pubDate>'.$pubdate.'</pubDate>
<itunes:duration>'.$duration.'</itunes:duration>
<itunes:keywords>My Podcast</itunes:keywords>
<category>Podcasts</category>
<itunes:explicit>no</itunes:explicit>
</item>';
Liam Allan
  • 1,115
  • 1
  • 8
  • 13
0
$newline = '<item>
<title>' . $title . '</title>
<link>http://example.com/CS/podcast/' . $target_path . '</link>
<itunes:author>' . $author . '</itunes:author>
<itunes:summary>' . $summary . '</itunes:summary>';

And so on - i think you get the picture. The way you have written it, you tell it that $title is a string - but it is a variable. And then you put the php tags and echo function as string aswell. This doesn't really make any sense.

Kentora
  • 96
  • 2
  • 12
0

The best ay to edit an XML-Document is using a XML parser to do all the formatting for you. Just like you don't edit an image in binary but in image-processing software package.

Lucky for us, there are some really strong xml-libraries out there: How do you parse and process HTML/XML in PHP?

There is also another thread here that answers the 'what is the best' question: Best XML Parser for PHP

Now to your problem. In PHP there are 2 different String declaration. there is the ' notation and the " notation. The difference is that when PHP comes along a " it parses the string andevaluates all variables whereas ' doesn't do any parsing of the string. An example:

$string = 'foo';
'my' . $string === 'myfoo';
'my$string' === 'my$string' !== 'myfoo';
"my$string" === 'myfoo';

I hope this is clear?

Community
  • 1
  • 1
Pinoniq
  • 1,365
  • 9
  • 13
0

Welcome back to the PHP fold. The other 2 answers will get you going but just so you know what you had forgotten I will add this.

The single quote and double quote work slightly differently in PHP

If you wrap a variable in double quotes it will expand that variable, in single quoutes it will not. So :-

$title = 'Hello';

$text = "$title World";   // $text = 'Hello World'

$text = '$title World';   // $text = '$title World'
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

If you don't want to stick them together by dots ('my var '.$myvar.' is cool'), you could just change your ' into "

So it would look like this:

<?php

    $inlineVar = "even this";
    $content = "This is a weird string! And look: '$inlineVar' works.";
    $content .= "Stick an additonal string or something to it.";

?>

In advance: Try to not write plain HTML or XML in your PHP-strings. Roll them out (*.html, *.xml or *.phtml)

wSkc
  • 149
  • 5