Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
gibbed
Apr 10, 2006

Imminent posted:

Might want to mention these two frameworks:
I've recently been trying Agavi,

There's also these, which I have tried...
Don't like any of those? Find more here.

Adbot
ADBOT LOVES YOU

gibbed
Apr 10, 2006

Mashi posted:

Careful there. There is no such type in PHP as 'nothing', that use of return with no value actually returns NULL, and NULL, 0, and false are all equal to "".
Which is why you should always use type comparison unless you don't care.

gibbed
Apr 10, 2006

Gary the Llama posted:

I've just playing with CodeIgniter and have already ran into a problem. I'm trying to load a model from my controller and I'm getting this error:

code:
!!table breaking error!!
It shouldn't even be looking in the system/libraries/Loader.php file, right?

My controllers are in the application/controllers folder.
The views are in the application/views folder.
And the models are in the application/models folder.

Any idea what's going wrong or why it's looking for Test_model in the Loader.php file?
It's not, that's where the error occured.

gibbed
Apr 10, 2006

Evil Angry Cat posted:

When this is absolutely neccesary I go with ${$variable} = "whatever"; . I'd have to see a bigger section of your code to see if there was an easier/cleaner way around it (which there usually is).
Personally I would go with an array.

gibbed
Apr 10, 2006

Bonus posted:

Incidentally, does anyone else think that the interface for mysqli is loving terrible? Especially the bind_param method. First you have to prepare the statement, then you have to bind parameters to it by giving it variables and strings like "sssd", then execute, bind results to variable, then fetch the data and then loop and output the variables that have the results binded to them repeatedly.
Sure, binding results to variables and then the current row being assigned to those variables saves memory by not storing all results in an array but it's not like you're going to be outputting 1 million records on a single page.
ADOdb does it way better.
Havn't used ADOdb, but PDO allows both (I hate that binding stuff).

gibbed
Apr 10, 2006

drcru posted:

Can I assume that #\n# will find all new lines?
If you're serious about using a regular expression to find newlines, you probably want #\r?\n|\r# instead.

gibbed
Apr 10, 2006

stack posted:

Is there a way to catch 'Ctrl-C' in a PHP CLI script running in Windows? PCNTL isn't available for Windows so I can't easily use it to catch the signal for it. Google only turns up one post on php.net where a guy made a custom build of PHP to include PCTNL and that won't work for my situation.
Aside from making another extension like PCNTL, no.

gibbed
Apr 10, 2006

Inquisitus posted:

Or just readfile it:

code:
<?php
header("Content-Type: image/jpeg\r\n"); 
readfile('http://path/.jpg');
?>
Drop the trailing ?> too, there is no need for it in this script.

code:
<?php
header('Content-Type: image/jpeg');
readfile('http://path/.jpg');
Edit: wait why the hell did you put a \r\n in the header() call?

gibbed
Apr 10, 2006

Also suggesting use of glob() instead of the opendir crap.

gibbed
Apr 10, 2006

mr. why posted:

The best thing I came up with is this:

$client->__soapCall('Report.GetDetails', array('param1', 'param2' ... ));

But that defeats the purpose of using the more-elegant callable method support. It also reminds me of NuSoap, which is what I'm trying to get away from (although if I had to, I'd rather use SoapClient than NuSoap no matter what).

Any ideas?
Inherit SoapClient and wrap all the functions in it? That's the only thing you can really do.

gibbed
Apr 10, 2006

mcbuttbutt posted:

I'm trying to implement a simple URL hiding script that retrieves a zip file from the server filesystem. It's pretty basic and could surely be much better. Something like this...
php:
<?
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " .(string)(filesize($file_path)) );

$file = @fopen($file_path,"rb");
if ($file) {
  while(!feof($file)) {
    print(fread($file, 1024*8));
    flush();
  }
  @fclose($file);
}

?>
It works for smaller files, but larger files (>50 megs) are always incomplete. Any idea why this is happening? Thanks!
Why? WHY? :argh:

Don't do silly things like this. Use readfile().

Also, I don't know if that is only a snippet of the code, but please:
  • Verify the input filename is a valid one (that is, it's a file that you want allowed to be downloaded). If this is the complete script, hello exploits!
  • Don't depend on register_globals.

gibbed fucked around with this message at 06:49 on Jun 12, 2008

gibbed
Apr 10, 2006

mcbuttbutt posted:

Easy now, that's just a snippet. And thanks, but readfile() didn't work either... still a partial download. I've increased the memory_limit variable in my php.ini to 80M and that did nothing as well. Stumped!
Is the file completely uploaded on the system you're downloading it from?

There is no reason why readfile() should fail unless the file is damaged somehow.

gibbed
Apr 10, 2006

ante posted:

I need to read 8-bit alpha channels from PNGs.

GD only supports 7-bit alpha channels, and the documentation says something to the effect of "why would you ever want to properly read PNGs 7-bits are plenty for an alpha channel :downs:" so I don't think they're going to add it any time soon.

Now, I have two choices that I'm aware of.

I can use ImageMagick (I'm assuming), but it's poorly(read: not) documented. So I don't even know if this is an option.

I can also parse the file data myself. This is the route I went, but again, I'm having trouble finding adequate documentation.
The official documentation is excellent in explaining how to read header data, separating chunks, etc.
As soon as it gets to reading IDAT information, however, it mentions decompressing it, and that's pretty much it.

My understanding from the documentation is that the data is formatted like this:
code:
[Filter method for scanline 1][R for pixel 1][G for pixel 1][B for pixel 1][A for pixel 1][R for pixel 2]...
[Filter method for scanline 2]...
But tests I've done on very small PNGs I've created myself have been completely different. Not in a recognizable pattern, either.


So, I guess I'm asking for a hint on any of the above. Maybe I've missed a hidden 8-bit alpha in GD, or an explanation of the png data format, or even a recommendation to try and puzzle out ImageMagick. Something like that.
Are you on a host that will install extensions for you? Or have the capability to? There is an awesome extension called Imagick which provides an API to ImageMagick (and it's faster).

http://www.php.net/imagick

Here's a crappy example:
php:
<?
$image = new Imagick('image.png');
$width = $image->getImageWidth();
$height = $image->getImageHeight();

$iterator = $image->getPixelIterator();

foreach ($iterator as $y => $pixels)
{
    foreach ($pixels as $x => $pixel)
    {
        $color = $pixel->getColor(true);
        // color is an array like array('r' => ..., 'g' => ..., 'b' => ..., 'a' => ...)
        // passing true to getColor() returns them in floats,
        // so to get the 'real' value you just multiply the values by 255.
    }
        
    $iterator->syncIterator();
}?>
I currently use this for pulling out raw image data instead of rolling my own PNG code for reading Spore images.

It's crude but it works.


To answer your question about the PNG format:

All IDAT blocks are joined together as one block of data, this data is compressed with zlib, after you decompress it, you'll get an amount of data depending on the image settings in the IHDR. Your understanding of this data format is correct, each scan line has a filter type associated with it, this filter type determins how the data for that scanline is stored.

http://www.cs.toronto.edu/~cosmin/pngtech/optipng.html

Skip down to "2.2 The PNG delta filters".

gibbed fucked around with this message at 11:37 on Jun 22, 2008

gibbed
Apr 10, 2006

Zorilla posted:

If PHP isn't configured, the web server interprets it as text/plain and just sends off the script without processing it. (beaten)
This is especially nasty when the Apache server begins to break down for some reason (hardware failure, etc) and stops handling PHP (been bitten by this, not just once, but twice). Which is why I put any important code outside of webroot now and include it. Can't take that chance again.

gibbed
Apr 10, 2006

Lankiveil posted:

What is the current cool framework to use for PHP? At the moment I'm playing with CodeIgniter, but I'm happy to jump ship if there's something better out there.
I'm quite partial to Agavi.

gibbed
Apr 10, 2006

Are you creating the SoapClient from a remote WSDL file or something local? Try downloading the WSDL and pointing the SoapClient at the local WSDL file instead?

gibbed
Apr 10, 2006

Zorilla posted:

Not even big-time CMSes have a way to secure this as far as I know. WordPress, Joomla and others just put those as plaintext in a central config file.
#1 reason to provide access to PHP a location outside web root, I usually set up my domains in this manner:

/data/www/somesite.com/www, /data/www/somesite.com/subdomain

I give access to PHP to /data/www/somesite.com

I store things like configs there, or backend related code in /data/www/somesite.com/backend

It saves you the hassle of people getting access to important information / code in the event your HTTP server breaks and starts serving raw content.

gibbed
Apr 10, 2006

duck monster posted:

Sure. Let me explain.

Null means "I dont have a value for this." , or alternatively "Not a number" (Some languages have different signifiers for null, and not-a-number), so if Y = null, then X + Y = null because if you dont have a value for Y , then you dont have a value for X. Same with subtraction, multiplication and in fact pretty much any operation, with a few knobs.

Now when you type cast to an int your actually saying , you MUST return this as an integer. The truly correct answer is to explode in flames and drop an exception, but assuming you don't actually want that (int) Null might as well be a good way of saying "Treat Null as zero".
Or you can just live with PHP's implicit casting of your null value to 0. It's not that bad.

gibbed
Apr 10, 2006

duck monster posted:

Except that its wrong and causes serious loving headaches.
In what situation would you find '1 + null = null' useful?

gibbed
Apr 10, 2006

duck monster posted:

Wrong
Submit a bug report to PHP then and hope they agree with you.

gibbed
Apr 10, 2006

duck monster posted:

throw an exception when attempting arithmatic in a variable that hasnt even been allocated yet.

quote:

Notice: Undefined variable: test in test.php on line 2
code:
<?php
	1 + $test;

gibbed
Apr 10, 2006

Golbez posted:

... what now? How do I get an associative array out of this? Or should I care, and just learn to live with the buffered results?
Wow, that's uh, pretty crappy way of returning results. Is PDO an option?

gibbed
Apr 10, 2006

Golbez posted:

Well that's just it, I'm assuming I'm missing a better way of returning results, but the best the PHP documentation will give me is a bland list of functions, rather than answering the simple question, "What methods does one use to get results from a prepared query?" I'd rather avoid PDO, since I don't need the abstraction it offers; this will be once and always on a MySQL database.
True, but from what I can tell from the mysqli documentation, PDO would probably be a better option (not to mention you don't have to install the other drivers, just pdo and pdo_mysql).

gibbed
Apr 10, 2006

The smart way is to put any important source files outside of webroot, period.
The dumb way is to do something like, if (!defined('MY_APP_BE_RUNNIN')) { return; } in your header fine, and define('MY_APP_BE_RUNNIN', true); in any files that include it.

gibbed
Apr 10, 2006

Tots posted:

Does anyone know how to search directories other than the current with glob?
Very simple.

php:
<?php
    $files = array();
    $paths = array(realpath('.'));
    while (count($paths) > 0)
    {
        $path array_shift($paths);
        
        $dirs glob($path '/*'GLOB_ONLYDIR);
        foreach ($dirs as $dir)
        {
            $paths[] = $dir;
        }
        
        $files array_merge($filesglob($path '/{*.jpg,*.gif,*.png}'GLOB_BRACE));
    }
    
    var_dump($files);
?>

gibbed
Apr 10, 2006

Tots posted:

The number of functions available is loving huge. Do people remember all of these?
No, gently caress no. No programmer is complete without a manual.

gibbed
Apr 10, 2006

Tots posted:

You just used 3 functions that are completely new to me. I just threw chdir() in my script and it works. Is there any advantage to doing it your way?
chdir changes the directory the current process is operating from, depending on what you are doing this can have unintended behavior.

If any of my code is confusing feel free to ask about what you are having trouble with. All my code does is build an array ($files) of absolute paths to all *.jpg/*.gif/*.png files that it finds.

array_merge (merges arrays), array_shift (removes an item from the beginning of the array and returns it), realpath (resolves relative paths to absolute ones).

Use the PHP manual! PHP may have lots of functions but nearly all of them are documented well enough.

gibbed
Apr 10, 2006

php:
<?php
    $files = array();
    $paths = array(realpath('.'));

//I don't understand why you have this while statement.  Won't realpath('.'); always resolve to a single pathname?

///Yes, what's happening here is that, as the inner loop runs,
///it will add more paths to the $paths array. The first loop will always 
///happen of course, because I put a starting element in the 
///array (realpath('.'))
    while (count($paths) > 0)
    {
        $path array_shift($paths);

//Here you are taking the current directory, and searching for any subfolders (I think)
//Now I start to get lost.  I think I'm too tired, and I've been trying to figure out too many things at once today.  What exactly is the statement $paths[] = $dir doing?

///This was dumb code on my behalf, I should have just used array_merge. What I'm doing is adding
///found subdirectories to the paths array, so the while loop will search them too, this results
///in every subdirectory from the top directory is searched for files.
///
///The original code, $paths[] = $dir. '$var[] = $thing' is basically syntax magic for array_push($var, $thing).
        $paths array_merge($pathsglob($path '/*'GLOB_ONLYDIR));

//Can you break down what this merge is doing also?
///It's adding all found .jpg/.gif/.png files to the files array (array_merge combines two or more arrays into a single array)
        $files array_merge($filesglob($path '/{*.jpg,*.gif,*.png}'GLOB_BRACE));
    }
    
    var_dump($files);
?>

gibbed
Apr 10, 2006

Zorilla posted:

If you need recursive file listings like gibbed seemed to be doing, include this function and use it instead (pulled from the comments section of glob() in php.net)
Yeah, what I was doing avoided having to define a function (and would work with any amount of subdirectories, where a recursive function would hit the stack limit eventually).

gibbed
Apr 10, 2006

Zorilla posted:

By stack limit, you mean the number of functions running simultaneously, right? One would think you'd hit the limit of the filesystem much sooner than you'd run out of recursions. I can't think of any situation where you'd go 255 directories deep looking for things (or is this limit much lower?)
Rather than bother with the possibility of it happening, I wrote to avoid it entirely :).

gibbed
Apr 10, 2006

Enable curl's verbose mode to see if it's actually returning any cookies to you.

gibbed
Apr 10, 2006

php:
<?php
    foreach ($urls as $url)
    {
        $content = @file_get_contents($url); // note the @ will supress error/warnings that would normally be outputted
        if ($content === false)
        {
            // failed to obtain content of xml
        }
        else
        {
            $xml = new SimpleXMLElement($content);
            $name = (string)$xml->name// it might be $xml->user->name if user isn't the root element.
            // etc...
        }
    }
?>

gibbed
Apr 10, 2006

What the gently caress? Why are you using define that way? :suicide:

Edit: also your problem is probably the fact that ImageCopyResized wants an image resource not a filename.
Edit 2: drop the ImagePNG and you will probably see lots of warnings/errors.

gibbed
Apr 10, 2006

awdio posted:

I'm making constants for security reasons when I add onto the code. What exactly do you mean by image resource? Sorry for the dumb question, I just have no idea what you mean by this.

I don't get any warnings or errors if I drop imagepng.

ImageCopyResized

Note the second argument. You're passing a string when it wants a resource, you'll want to use ImageCreateFromPNG() or equivilent to load the image you want to resize.

You certainly should have gotten warnings or errors with your code, is your error reporting off?

Where did you get the idea that constants somehow magically affect security?

gibbed
Apr 10, 2006

I have no problem with newwidth/newheight being define()s (though I personally wouldn't do that).

But the image resources (thumb, source) should definitly not be constants.

gibbed
Apr 10, 2006

TreFitty posted:

I'm learning PHP and was wondering: can you guys recommend any of the frameworks in the original post? I mean, does one completely outclass the other? Which is better for what? etc.
They all have their unique taste and have their own advantages and disadvantages. Use whatever you like.

In that regard, another good option that isn't in the OP is Agavi.

gibbed
Apr 10, 2006

And for the love of god use .php as the extension, not .inc, or at least .inc.php.

gibbed
Apr 10, 2006

http://php.net/oop4 - PHP4
http://php.net/oop5 - PHP5

gibbed
Apr 10, 2006

eHacked posted:

I'm learning OOP from a pretty fantastic book (so far): PHP Object Oriented Solutions by David Powers.

Anyway, he regularly says this "-> operator". Is there a name for '->'?

How the hell do I tell someone what I am talking about in plain English?
-> is officially called the object operator (T_OBJECT_OPERATOR).

Adbot
ADBOT LOVES YOU

gibbed
Apr 10, 2006

What's the error? You're not giving enough information, given that there can be server settings to run other PHP code in addition to yours, etc, that would help.

Edit: Oh, it's not that at all. It's your verbatim copying of the code from the guide. The “” are not properly interpreted as " by PHP, fix that and you fix the code.

gibbed fucked around with this message at 11:59 on Jun 13, 2009

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply