1

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
.

23
  • There's no code calling ParseStuff()... Commented Feb 24, 2013 at 23:28
  • $myParse = new Parsing(); $myParse->ParseStuff(); Though for your purpose, you really don't need to make it in a class. Seems like you came from a java background. However, if you are only going to have one function, you may as well put it outside of a class scope so it runs when the page is loaded without the need to call it. Commented Feb 24, 2013 at 23:31
  • Jon's code should be somewhere in there. The class has to be instantiated then the function has to be called. Commented Feb 24, 2013 at 23:32
  • There's a bigger problem, Matt and Jon. He says he is trying to upload a file to the server. The PHP I see, is only loading a file that already exists, by its filename. Commented Feb 24, 2013 at 23:34
  • 1
    @user1950929 Good job paying attention, she's not uploading a file, just passing a filename. Commented Feb 24, 2013 at 23:56

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.