Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Code question: Upload
  • Hi there,

    this is just a question of how and what this particular code does:

    In Upload::get_files()




    foreach ($file as $item => $value)
    {
    $item == 'element' and $item = 'field';
    $item == 'tmp_name' and $item = 'file';
    $item == 'filename' and $item = 'saved_as';
    $item == 'path' and $item = 'saved_to';
    $data[$item] = $value;
    }




    do? I'm familiar with the and keyword.



    // The constant true is assigned to $h and then false is ignored
    // Acts like: (($h = true) and false)
    $h = true and false;


    From php.net.
    But here? Further down there is the comment
    // swap item names for BC
    , which doesn't help me.

    In Upload::is_valid()

    public static function is_valid()
    {
    return static::$upload->getValidFiles() == array() ? false : true;
    }


    I think the methods name is misleading. If it's called is_valid and I can call it on the hole Upload, I expect it to return true iff all fields return Upload::UPLOAD_ERR_OK.

    Otherwise what would be the meaning of it?
  • It's very simple. The Upload class has been a part of Fuel for over two years now.

    Recently we swapped it by the new Upload package we've created (from scratch) for Fuel 2.0. Since this isn't compatible with 1.x, we needed to add a Facade class, to translate between the old 1.x static API and the new non-static 2.0 classes. Like for example the property names in the File container, which are different in 2.0.

    So what you see here is what has always been the Upload API in 1.x.

    As to is_valid(), have you actually read the documentation?

    It states "The is_valid method can be used to check if there are any uploaded files present that have passed the upload validation."

    So it does what it should. That this is not what you want or need may be true, but that doesn't make it misleading.

    If you want more control and direct access, I suggest you don't use the Facade class (which is there for compatibility reasons, something you don't have when you're making something new), and use the new 2.0 package directly.
  • Ah,... I see something got deleted during my attempt to make my post more legible:

    What in particular does $item == 'element' and $item = 'field'; do?
    Compare, then assign? Or is
    $item = 'field' only executed when the comparision returns true?

    I've read the Docs. That's why I said misleading and not wrong or faulty. It's just something I find logically wrong. The misleading part was more directed to the name of the method.

    Cool, I look into it.
  • HarroHarro
    Accepted Answer
    Ah, ok.

    $item == 'element' and $item = 'field';

    is short for

    if (
    $item == 'element') $item = 'field';

    It uses the fact that PHP stops evaluating a boolean expression if it has the answer (in an and-operation it stops at the first 'false', and in an or-operation at the first 'true').

    Since this is an and-operation, if the first bit (
    $item == 'element') is true, it has to evaluate the second bit, which is an assignment. if the first bit is false, the evaluation stops and the assignment doesn't happen.

    The 2.0 Upload code creates a File object for every uploaded file, which can use accessed indivudually. It also provides an isValid() method, which does indeed only return true if there are files uploaded, and all of them are valid.

    So you're question is already addressed, but we can't implement that in the 1.x facade because that will break all existing applications which expect the current behaviour.
  • Cool, thank you.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion