0

I was running through the tutorial on http://net.tutsplus.com/tutorials/php/online-file-storage-with-php/comment-page-2/#comments and it was working fine until:

if(strlen($message) > 0)
{
    $message = '<p class="error">' . $message . '</p>';
}

This line of php is found in index.php. When I few the page in firefox, it looks like the php parser stops at the greater than. Can I escape the character? Do I need to?

EDIT: All the php code:

<?php 
//Load the settings
require_once("settings.php");

$message = "";
//Has the user uploaded something?
if(isset($_FILES['file']))
{
    $target_path = Settings::$uploadFolder;
    $target_path = $target_path . time() . '_' . basename( $_FILES['file']['name']); 

    //Check the password to verify legal upload
    if($_POST['password'] != Settings::$password)
    {
        $message = "Invalid Password!";
    }
    else
    {
        //Try to move the uploaded file into the designated folder
        if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
            $message = "The file ".  basename( $_FILES['file']['name']). 
            " has been uploaded";
        } else{
            $message = "There was an error uploading the file, please try again!";
        }
        }

    //Clear the array
    unset($_FILES['file']);
}


if(strlen($message) > 0)
{
    $message = '<p class="error">' . $message . '</p>';
}    
?>
<html> ... </html> //my html code
7
  • If the code is contained with the normal <?php and ?> then I cannot see a problem with it. You certainly shouldn't need to escape the > character Commented Sep 27, 2012 at 16:24
  • What is the output that the browser receives (the HTML source)? Commented Sep 27, 2012 at 16:24
  • I initialize it as $message = ""; Commented Sep 27, 2012 at 16:25
  • 2
    paste the error message here, it will help much Commented Sep 27, 2012 at 16:28
  • what you mean by php parser stops ? Are you saying $message is not printing the output in your browser? Commented Sep 27, 2012 at 16:36

4 Answers 4

3

The > won't cause the PHP parser to stop.

Without seeing the HTML output by the server, it is hard to say for sure, but since the > is the first > in the file it seems likely that the PHP parser never starts and the browser treats everything between the <?php at the start of the file and the strlen($message) > as a tag.

You need to access the PHP through a web server with PHP installed and configured to process that file (which is typically done by giving it a .php file extension).

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

1 Comment

This is probably accurate. I "fixed" the "> problem" by taking yoeriboven's answer, but it then fails at the next "."
1

What about this?

if(!empty($message)){
    $message = '<p class="error">'.$message.'</p>';
}

But why don't you directly assign the paragraph tags to the error message instead of first assigning the error message to $message and then the paragraph tags?

3 Comments

To answer your question, here's why I'd do it: You may want to add multiple error messages for different cases, so why would you double the markup for every message.
And add the HTML MarkUp to every item? What for? In this case, I'd still add it to every loop item and/or the container.
This almost works, but then it fails at the '.' on ".$message."
0

there is not any error in the if condition its working fine

enter image description here

the possible problem in the

  1. if(isset($_FILES['file']))
  2. if($_POST['password'] != Settings::$password)
  3. if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path))

if you are not getting in the if body it mean the problem in

  if(isset($_FILES['file']))

because if it fase than $message = "";

1 Comment

It seems to fail before that.
-1

Always use Yoda Conditions and write such statements in (the) reverse(d) order (you're normally used to:

if ( 0 !== strlen( $message ) )
{
    $message = 'Hello World!';
}

Anyway, you could also simply check for ! empty( $message )

2 Comments

While this is popular advice, it doesn't explain why the code in the question doesn't work.
No, but it is a way to get error output, which won't happen in the reverse order

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.