We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 764
    • 7 Posts
    I have a form for image upload in my snippet. I use the folowing line of code to create an image:
    $image = @imagecreatefrompng($_FILES[’file’][’tmp_name’]);
    Everything works fine unless user specifies non-png file or, the bad case, broken png file.
    Thus, I get this message:
    « MODx Parse Error »
    MODx encountered the following error while attempting to parse the requested resource:
    « PHP Parse Error »

    PHP error debug
    Error: imagecreatefrompng(): ’/tmp/phpws7gmC’ is not a valid PNG file
    Error type/ Nr.: Warning - 2
    File: /usr/local/psa/home/vhosts/vmunit.com/httpdocs/manager/includes/document.parser.class.inc.php(766) : eval()’d code
    Line: 95

    Parser timing
    MySQL: 0.0015 s (4 Requests)
    PHP: 0.0372 s
    Total: 0.0387 s


    How can I handle this error situation and show a suitable message to user?
      • 7231
      • 4,205 Posts
      Good question, I have the same problem with a snippet I use. It seems that modX ignores the @.
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Even with @ a function will still return an error code, the @ simply suppresses the error message. MODx has its own methods for dealing with PHP error codes.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 764
          • 7 Posts
          Nice. Can you please tell me how should I deal with this?
          Search gave me nothing, so I will be grateful for proper link to read.
          Thanks.
            • 764
            • 7 Posts
            Guys? Can anyone help?
            Is this too silly question?
              • 14050
              • 788 Posts
              The best way I can think of is to check and make sure the file is a ".png" file before running it through that function, and also seeing if you can find a function that makes sure it is valid without throwing the error, then passing it through that function. As of now, there is no other way to deal with it that I know of.
                Jesse R.
                Consider trying something new and extraordinary.
                Illinois Wine

                Have you considered donating to MODx lately?
                Donate now. Every contribution helps.
                • 764
                • 7 Posts
                Thanks jesster444!
                I’ve checked some snippets sources from download section. They use approach you suggested.
                However, this method can’t help us deal with broken or wrong-format files.

                Specially for MODX developers: It’s a bad, BAD thing if I can’t handle error situation. You should fix it.
                  • 14050
                  • 788 Posts
                  I believe the ability to handle errors is being addressed in 0.9.7. In the meantime, you might want to look at png2wbmp().

                  http://us.php.net/manual/en/function.png2wbmp.php

                  You could do a conditional like:

                  if (png2wbmp(...)) {
                  ....
                  }

                  png2wbmp() does not throw an error or warning like imagecreatefrompng() does upon false. So it should stop the warning on wrong or invalid files types. The only downside, is on success you are creating an extra .bmp with every upload. However, I guess you could have this script delete that file upon success, so then the only downside is a little extra overhead.
                    Jesse R.
                    Consider trying something new and extraordinary.
                    Illinois Wine

                    Have you considered donating to MODx lately?
                    Donate now. Every contribution helps.
                    • 7231
                    • 4,205 Posts
                    Yes, I have been adding a series of pre-checks to a snippet I created that are almost as long as the logic itself, doubling the amount of code... I am surprised that there is not an error api as well (at least I could not find any info regarding this). Edit: good to hear it is being addressed in future.

                    It would be nice to be able to flow around errors so that the remainder of the page would load and the error message would be where the snippet code would be. Or in other words have snippet level errors rather than core level errors. And be able to apply custom messages per situation.


                      [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                      Something is happening here, but you don't know what it is.
                      Do you, Mr. Jones? - [bob dylan]
                      • 764
                      • 7 Posts
                      jesster444, thanks again!
                      However, I couldn’t make it work using png2wbmp function. It throws an error just like the imagecreatefrompng does. May be it’s because of my hosting php version (4.4.1).

                      Thanks anyway. Hope developers will deal with this problem soon.