Yecats
Yecats

Reputation: 1815

PHP & Jquery - Sending data between the client and server

I am trying to write code that sends a file path to the server, uses php to parse out each line into an array and returns the array to the client to have stuff done to it.

When I run my program it doesn't look like it is processing the php file as the echo that i put in for testing purposes is never called.

I know absolutely nothing about PHP so help is greatly appreciated!

Jquery:

$("#codelines").load('ParsePHPForDisplay.php?filename=DBManager.php');

    console.log($('#codelines'));
    pieces = $("#codelines").html().split("\n");

PHP:

<?php

class Parsing
{
    function ParseStuff()
    {
        echo('hi');
        $parsedFile = file($_REQUEST['filename']);
        echo($_REQUEST['filename']);
//        foreach ($parsedFile as $parsedFile_num => $parsedFile) {
//            echo"Line #<b>{$parsedFile_num}</b> : " .htmlspecialchars($parsedFile) . "<br/>\n";
//        }
        return $parsedFile;
    }
}

?>

A piece of my HTML:

<section id="codestuff">
    <h2>Code Lines</h2>
    <pre id="codelines">
    </pre>
</section>

EDIT:

Here is my PHP now:

<?php


        echo('hi');
        $parsedFile = file($_REQUEST['filename']);
        echo('test');
        echo($_REQUEST['filename']);
//        foreach ($parsedFile as $parsedFile_num => $parsedFile) {
//            echo"Line #<b>{$parsedFile_num}</b> : " .htmlspecialchars($parsedFile) . "<br/>\n";
//        }
        return $parsedFile;



?>

I get this error:

Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in ParsePHPForDisplay.php on line 5
.

Upvotes: 1

Views: 405

Answers (2)

mavrosxristoforos
mavrosxristoforos

Reputation: 3643

Alright, I believe the PHP you should use, since you are splitting the file into an array in the Javascript part, is:

<?php
  $file = file_get_contents($_REQUEST['filename']);
  echo $file;
?>

But before publishing this, you should check security issues, like the user loading files from other directories, using ../ in the filename, also not to fetch php files, etc.

Upvotes: 1

Matt Browne
Matt Browne

Reputation: 12419

I don't see why you would need PHP to parse the file into an array...just have it send the whole thing as a big string, then you can split it into an array of lines, if you need to, in Javascript once you get it from the server (e.g. myString.split(/\n\r|\n|\r) ).

Then, from PHP, you can just do:

readfile($_REQUEST['filename']); exit;

(note that this is not secure; you should add some conditions to the PHP code so it can only outptut files from a particular directory, for example. See PHP's explode function which will help you split the filename into an array using DIRECTORY_SEPARATOR as the delimiter).

Note that in PHP, a return statement doesn't cause anything to be output. Very often you'll get the value you need via a function that returns a value, but you still need to echo or print it.

Upvotes: 0

Related Questions