0

Being a noob ... I can't quite figure out what isn't working here .... if the file is empty, $this->result contains the right error message. But if I have a file name, the result array is empty and I'm not getting an upload

class upload_f
{
    public $path;              // path to upload from root ie  files/images/
    public $fileName;          // current file  ie $_FILES['uploadedfile']['name'];
    public $result = array();  // array containing error to be loop outside object

    public function validateInput()       // verify is minimal data is entered.
    {

        if (empty($this->fileName))
        {
            $this->result[] = "ERROR: File name is empty.";
            return false;
        }
    } // end of validate


    public function upload()
    {
        // run validation
        if (!$this->validateInput())
        {
            return $this->result;
        }
        else
        {
            $f_name = $this->fileName;
            $path_fileName = $this->path.$this->fileName;

            if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $path_fileName))
            {
                $this->result[] =
                    "File  ".  basename( $_FILES['uploadedfile']['name']). " was uploaded";
            }
            else
            {
                $this->result[] = "There was an error uploading the file, please try again!";
            }

            $this->result[] = $path_fileName;
            return  $this->result;

        } // end of else : upload execution if no errors
    }

} // end of upload class

[...]

//****************************************/

// call the object
// form here with a if - post ... 
$the_array = $test->upload();
$test = new upload_f();
// assgin values
$test->path = "docs/";
$test->fileName = $_FILES['uploadedfile']['name'];
$the_array = $test->upload();
echo "<pre>";
print_r ($the_array);
echo "</pre>";
3
  • When true, you might be returning null -nothing- from validateInput instead. Commented Feb 1, 2012 at 18:24
  • 1
    It's amazing how far consistent indentation in your code will take you during the debugging process. Of course, the inverse of this also applies. Commented Feb 1, 2012 at 18:27
  • In what you've posted here there is a missing bracket }. There should be an end of function between end of else and end of upload class. I'm guessing this was just a posting error though. Using a more classic indentation method would REALLY help you catch these errors. It is not just for show. Commented Feb 1, 2012 at 18:28

2 Answers 2

2

You should change validateInput() to:

public function validateInput() {    
    if (empty($this->fileName)) {
        $this->result[] = "ERROR: File name is empty.";
        return false;
    }
    return true; // <-- return true if input is valid
}

As you have it, the method returns something falsy for all cases, causing !$this->validateInput() always to evaluate to true.

Reference

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

3 Comments

hmmm ... that seems to work THANKS ... but why ? doesn't it only go false if the IF statement validates ???
@zefrank See that link. NULL == false, and your method was returning either false or NULL.
ah! great ... learn something everyday. Self taught and never used boolean much. tkx!!!
0

Not sure if it's your problem, but you're missing a closing brace at the end of your upload class.

I noticed looking at your code that you have an odd style of using curly braces. It would be great to get in the habit of picking a style and sticking with it. You can use any style, as long as it's consistent. Here's a list of them: Indent Styles

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.