SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    writing php through php

    I'm creating a file dynamicly though php, it works fine creates a file writes to it (html code) except it freaks out when it needs to write php into that new file.
    i'm speculating here but Im thinking it, on the fly, creates a php code as it also tries to read the same code maybe...?

    anyway I get an error when it gets to the php part of the code. Escaping characters doesn't help either. It actually crashes saying that it expected '=' on line such and such

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    enclose your code in single quotes, and make sure to escape single quotes.

    PHP Code:

    $var = '
    <?php echo "hi!"?>
    <?php 
    echo \'foo\'; ?>
    <?php echo $foo; ?>
    '
    ;

    // now write $var to a file

    but, do you really need to write php code to a file? there is likely a better way to accomplish your goal. if any of the data your writing to this file comes from a user, you need to be very careful as they could possibly pass you some php code, which your server would then execute.

    again, its likely a better solution exists.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it works... except in my case and I'm still missing something...
    this is what I'm trying to enclose in the var and write to file (here I've already made my attempt, this is what didn't work):
    PHP Code:
    $var = '
    <?php
    $ftpStream 
    ftp_connect(\'ftp.server.com\');
    $loginResult = ftp_login($ftpStream, \'login@server.com\', \'password\');
    if ($loginResult) {
    $dir = \'project1\';
    $files_array = ftp_nlist($ftpStream, $dir);
    array_shift($files_array);
    array_shift($files_array);
    krsort($files_array);
    $recentRev = current($files_array);
    echo \'<a href=\'../../../_ProjectFiles/HTTPalt.php?ProjRev=$recentRev\'><span class=\'bodyTextLG\'>$recentRev</span></a>\';
    ftp_quit($ftpStream);
    ?>
    it freaks up about the
    href=\'../../../_ProjectFiles/HTTPalt.php?ProjRev=$recentRev\'><span class=\'bodyTextLG\'>$recentRev</span></a>\'; line, something about not liking '.'

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah... this is definatelly about hrefs, their dots slashes and i don't know if anything else. Anyone has ever found a solution to an issue like this?

  5. #5
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo '<a href=\'../../../_ProjectFiles/HTTPalt.php?ProjRev=$recentRev\'><span class=\'bodyTextLG\'>$recentRev</span></a>';

    Try with only escaping the single-quotes in the middle of your string.

    echo 'Twin sister\'s strings';
    Upgrading to Mysql 5? Auto-increment fields now strict
    use NULL
    Or zero or leave the field name out completely.

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ... this is really strange, looks like that might've worked but now it finds something that isn't there, look, this is output after all has been passed and file with the contents below was created:

    <?php
    $ftpStream = ftp_connect('ftp.server.com');
    $loginResult = ftp_login($ftpStream, 'login@server.com', 'password');
    if ($loginResult) {
    $dir = 'project1';
    $files_array = ftp_nlist($ftpStream, $dir);
    array_shift($files_array);
    array_shift($files_array);
    krsort($files_array);
    $recentRev = current($files_array);
    echo '<a href=\'../../../_ProjectFiles/HTTPalt.php?ProjRev=Proj_Rev05.pdf\'><span class=\'bodyTextLG\'>Proj_Rev05.pdf</span></a>';
    ftp_quit($ftpStream);
    ?>

    this is the error that I get from that:

    Parse error: parse error, unexpected $ in /home/..../file-fwd18.php on line 13

    ... there's no $ on line 13
    what's up with that?

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    you need to amend your code so that you add a closing } for the one opened on this line:
    PHP Code:
    if ($loginResult) { 
    and it should work

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •