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
LP0 ON FIRE
Jan 25, 2006

beep boop
I would like to sort file names with file extensions numerically. It's not sorting correctly right now, and I think it is because of the file extensions.

So far I have:

code:
<?php

$dir=opendir(".");
$files=array();
while (($file=readdir($dir)) !== false){
	if ($file != "." and $file != ".." and $file != "allBlocks.php"){
		array_push($files, $file);
	}
}
closedir($dir);
rsort($files);
foreach ($files as $file)
echo "<img src=".$file.">";

?>

Adbot
ADBOT LOVES YOU

LP0 ON FIRE
Jan 25, 2006

beep boop

duz posted:

You probably want to do a Natural Sort.

Yes! This is awesome. Exactly what I wanted. Now, I'm trying to make it go in reverse order like before, and I can do it but I don't know if it's the best way because it seems to be working very slow, but then again it could just be my internet connection.

code:
<?php

$dir=opendir(".");
$files=array();
while (($file=readdir($dir)) !== false){
	if ($file != "." and $file != ".." and $file != "allBlocks.php"){
		array_push($files, $file);
	}
}
closedir($dir);
natsort($files);
foreach (array_reverse($files) as $file)
echo "<img src=".$file.">";

?>

LP0 ON FIRE
Jan 25, 2006

beep boop

gibbed posted:

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

But do I really need it? Is there a reason this would be better in my situation?

LP0 ON FIRE
Jan 25, 2006

beep boop

duz posted:

It's slow because you're reversing the array each time you loop.

Yeah I see that, but even if I put the array_reverse($files) after natsort, it won't permanently do anything because its not resorting the array. Also putting rsort after the natsort will do away with the natsort.

It doesn't seem too bad now, and maybe it was just my connection, but is there a better way I should do this?

LP0 ON FIRE
Jan 25, 2006

beep boop

duz posted:

Read the docs. array_reverse returns the reversed array. You'll need to do $files = array_reverse($files);.

Well that was all too obvious. :doh:

Thanks. I probably would have thought to do that after taking a break, but yes it does work a bit faster.

LP0 ON FIRE
Jan 25, 2006

beep boop

gibbed posted:

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

Okay, glob is way better you're right. Gets rid of a lot of code and I don't have to tell it to open or close the directory. Definitely using this from now on. Thanks.

LP0 ON FIRE
Jan 25, 2006

beep boop
I was using php code to check if a sent $_GET didn't come from outside of my website. Now I want to do the same thing with $_POST, and I expected the code to work for it, but it doesn't do anything inside the if statement if a $_POST occurred. I tested to make sure this was the case by echoing the sent $_POST outside of the if statement, which works.

Here's the code I used to validate a $_GET:

code:
<?php
if (strpos(strtolower($_SERVER['HTTP_REFERER']), 'mywebsite.com') !== false){
echo $_POST["sentVar"];
}
?>

LP0 ON FIRE fucked around with this message at 22:19 on May 2, 2008

LP0 ON FIRE
Jan 25, 2006

beep boop
Lumpy I tried your test and it failed.

LP0 ON FIRE
Jan 25, 2006

beep boop
I figured out the HTTP_REFERER issue. I don't know if this was obvious to any of you, but when you use $_POST as opposed to $_GET, you actually have to specify the ENTIRE address, not just the file name even if it is in the same directory to be able to send the HTTP_REFERER information and get it back with $_POST. Of course the information for the $_POST will return, but not the HTTP_REFERER.

LP0 ON FIRE
Jan 25, 2006

beep boop
edit sorry wrong thread.

LP0 ON FIRE fucked around with this message at 18:52 on May 3, 2008

LP0 ON FIRE
Jan 25, 2006

beep boop
If header( 'Location: example.php' ); is placed at the top of a PHP script outside of a function, if statement or any other logic is the rest of the code guaranteed to run properly before it performs the header redirect?

LP0 ON FIRE
Jan 25, 2006

beep boop

Mashi posted:

I don't know about guarenteed to run properly (that would be nice wouldn't it?) but outputting a header will not disrupt the execution of your script at all.

Okay, good to know. Thanks.

LP0 ON FIRE
Jan 25, 2006

beep boop

noonches posted:

You can still used ob_start() and ob_end_clean() and output a header at any point.

I'll try this out too. I'm just trying to debug something. For some reason when I when I have the header there SOMETIMES my information that goes to another part of the site doesn't actually go there, and I was just wondering if it was because of something in the header that wasn't giving the site enough time to get the information it needed or something.

LP0 ON FIRE
Jan 25, 2006

beep boop
I'm using imagepng to create PNGs out of code into a certain directory. That all works fine, but why on the page where i use imagepng it spits out weird characters and how do I get rid of them?

To see what I mean, just draw some nonsensical thing on the page, type in the CAPTCHA and then the page where it says it was successful shows that mess.

Just to let you know, depending what people have made, might be considered :nws: not work safe. But they are just pixelated small images.

http://www.majoroutput.com/TestMOCode2/index.php

edit:

I don't know if there's any alternate way of using it, but here's the line of code: imagepng($thumb, "blocks/".$numOfFiles.".png");

If I get rid of that, it takes away the characters.

LP0 ON FIRE fucked around with this message at 02:25 on Oct 26, 2008

LP0 ON FIRE
Jan 25, 2006

beep boop

Internet Headache posted:

I'm not sure why imagepng would function that way. As a short-term hack for a library of hacks, you could use output buffering around the function and just dump the contents.

php:
<?
ob_start();
imagepng($thumb, "blocks/".$numOfFiles.".png");
ob_end_clean();
?>

Excellent! It works, thank you.

LP0 ON FIRE
Jan 25, 2006

beep boop

dagard posted:

Apropos of nothing, please, don't do that. Fan out your directories, don't put everydamnthing in one directory. readdir() calls will start beating the hell out of your performance. A better way would be, let's say $numOfFiles is 38234234, write it to:

blocks/038/234/234.png

Storage, especially if/when you get to the point where you're using shark/netapp/isilon/whatever, is going to plotz if it has to stat() across a directory of 10000+ files. Try to keep it around 1-2k, you're golden, and really, it can scale forever.

(Ex: 3823423482340238423.png translates to 003/823/423/482/340/238/423.png), and you're looking at, max, around 2k entries per directory, which about any OS or storage system should be able to handle

File size versus FS sector size being left as a discussion for later, as is the extended cost of walking up that whole directory tree.

Thanks. Yes, it will eventually go to different directories. I never thought of the way you suggested, so thank you.

edit:

Isn't that scaling way further into the trillions of possible files?

LP0 ON FIRE fucked around with this message at 00:07 on Oct 27, 2008

LP0 ON FIRE
Jan 25, 2006

beep boop
mysql_real_escape_string is making variables blank for me. For instance, I have:

$nameVar="test";
$nameVar=mysql_real_escape_string($nameVar);
echo "name".$nameVar;


I found was now inserting nothing into my database and echos nothing back. Anyone know what could cause this issue?

LP0 ON FIRE
Jan 25, 2006

beep boop

Strong Sauce posted:

Possibly this,

Ah! I remember reading that somewhere. Thanks!

LP0 ON FIRE
Jan 25, 2006

beep boop
This is a mixed php/Actionscript 2/MySQL question. PHP is echoing back a variable queried from MySQL just fine, called $imageSourcesString. When gets the variable with loadVariables, it returns empty in Flash. If I redefine $imageSourcesString in php as $imageSourcesString="test"; its sent to Flash just fine.

Why can the original variable echo in php, but not be sent to Flash? I've tried adding quotes, characters around the original, etc. Say if I redefine it as $imageSourcesString="A".$imageSourcesString."A"; in Flash it shows up as "AA". I can see that $imageSourcesString echos in php perfectly alright as the image names. What the hell? Is $imageSourcesString not a proper string for Flash or something coming from MySQL?

LP0 ON FIRE
Jan 25, 2006

beep boop

Lumpy posted:

It needs to be a URLecoded string... your problem seems odd though. Are you trying to output the string $myVar=A[$myvar's value]A as a result?

[EDIT] looks like you *can* use an "$" as the first char of a variable name in AS2, but I'm guessing the URLencoding is hosing it up still.

Using echo $imageSourcesString in php shows result.

In Flash, it shows nothing.

Doing this however after its defined: (I'm redefining the variable)

$imageSourcesString="test text";

Shows this in Flash:

test text

Say if my variable $imageSourcesString equaled "123456789" as a result from the database. If I redefine it as:

$imageSourcesString="A".$imageSourcesString."A";

In php it shows "A123456789A"

In Flash it shows "AA"

I was starting to think it had to do with register_globals being enabled. I'm currently using php 4 and after trying to change it to off, GoDaddy notified me I won't be able to do this unless I'm using php 5, which doesn't seem right. I swear I changed that setting before in php 4 on another server. Maybe its just the way GoDaddy does things. This is really blowing my mind though and I'm starting to think it has to do with some weird thing occurring with register_globals.

LP0 ON FIRE
Jan 25, 2006

beep boop

Munkeymon posted:

OK, but what does Flash get when you do $imageSourcesString=urlencode($imageSourcesString);?

Same thing, still nothing.

LP0 ON FIRE
Jan 25, 2006

beep boop

awdio posted:

Using echo $imageSourcesString in php shows result.

In Flash, it shows nothing.

Doing this however after its defined: (I'm redefining the variable)

$imageSourcesString="test text";

Shows this in Flash:

test text

Say if my variable $imageSourcesString equaled "123456789" as a result from the database. If I redefine it as:

$imageSourcesString="A".$imageSourcesString."A";

In php it shows "A123456789A"

In Flash it shows "AA"

I was starting to think it had to do with register_globals being enabled. I'm currently using php 4 and after trying to change it to off, GoDaddy notified me I won't be able to do this unless I'm using php 5, which doesn't seem right. I swear I changed that setting before in php 4 on another server. Maybe its just the way GoDaddy does things. This is really blowing my mind though and I'm starting to think it has to do with some weird thing occurring with register_globals.

I finally figured out what was causing the problem, but I don't know WHY it happens. I had a variable defined with a get:

$catNum=$_GET['catNum'];

If I simply make that variable a number and not the get my other variable from the database gets sent to Flash. Why??

$catNum=3;

Edit: Basically, if I have that variable $catNum defined with the get $_GET['catNum'], $imageSourcesString does not pass to Flash! If I make $catNum equal say, "3", everything works. But I NEED the get for $catNum.

LP0 ON FIRE fucked around with this message at 04:52 on Jan 8, 2009

LP0 ON FIRE
Jan 25, 2006

beep boop

Standish posted:

Looks like Flash is not supplying the "catNum" parameter in its GET query string for some reason, try running wireshark to see exactly what's going over the wire.

But why would it need to? Flash is NOT getting the variable $catNum. It's getting $imageSourcesString.

I'd try WireShark, but I don't think there's a non-Intel Mac version.

LP0 ON FIRE
Jan 25, 2006

beep boop
I guess the question I should be asking is: How do you pass a variable with GET from one page to another (or even the same page) and have Flash load that variable in? I'm just passing it with the same page, but I've also tried passing it to another page. PHP gets it. Flash does not. If I just make a non-POST or non-GET variable it passes to Flash fine.

If you think this normally should be done, try it out, because I'm starting to think it can't be done..

Edit: Its as if Flash is being loaded before the POST or GET vars.

LP0 ON FIRE
Jan 25, 2006

beep boop

Lumpy posted:

I'm confused now.

Are you loading a page: blah.php?var=value And you want the flash that is embedded on the page to know that var = value?

Or you are using flash to send a request to a script after your page loads, and the return from the script is var=value and flash is unable to pick that up?

Works like this:

I click on a URL that goes back to itself with a variable attached like so:
http://pageurl.com/index.php?catNum=9

So when the page reloads, php sees that catNum is set, then does a mySQL query based on the catNum to form a variable to send to Flash - a list of images with a delimiter that Flash breaks down. The variable is called $imageSourcesString and is put into a hidden text field that Flash picks up with loadVariables.

For some reason this isn't working BUT if I ignore the GET on the page reload and just set $catNum to a number everything else works fine. i.e. $catNum=12 instead of $catNum=$_GET["catNum"]

Interestingly enough its not getting stuck until the Flash stage of things. PHP can still pick up the results of the image string based on the GET.

LP0 ON FIRE fucked around with this message at 22:27 on Jan 9, 2009

LP0 ON FIRE
Jan 25, 2006

beep boop

Lumpy posted:

Can you give us a real URL. I think I know what your poblem is, but I can't be sure unless I see WTF.
Sure

http://www.donnastangerphotography.com/dbFormat/indexTest.php?catNum=12

LP0 ON FIRE
Jan 25, 2006

beep boop
I've been trying so many things. I even tried putting the URL into a variable and using explode to get the value after the equals sign, echoed the value to make sure php got it, and it still breaks Flash. I also tried making javascript function that fills in a div with the Flash code after a timed interval but it behaves exactly the same.

LP0 ON FIRE
Jan 25, 2006

beep boop

Ghotli posted:

Have you tried a small proof of concept completely separate from the code you've been working with? If all you need is for flash to receive the value of a php variable you can mock up this situation in very few lines of code. This should help isolate the problem. My guess is there's a problem with your code that you're overlooking because you have been debugging for too long. Step back, relax, and write a simple proof of concept. This will help you see if it works on that server/browser at all or if it's a specific problem with your code.


Yes. And I can get a php variable into Flash. Its the GET variable in a mysql query is what causes things to get hung up. If I define the variable without the GET, it does not happen. I can echo back the correct results of the php query, the Flash just does not get anything from the result of the query.

Read above for details.

Also if people want a more direct way of looking at this, here's the php/html:

code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<body>
<?php

$catNum=$_GET["catNum"];
echo $catNum;

//$catNum=12;

//connect and select db
include 'connect.php'; 
mysql_select_db("dataXXXXXX") or die(mysql_error());

$result = mysql_query("SELECT * FROM pictures WHERE catNum='$catNum' ORDER BY picOrder") or die(mysql_error());  

$filenames = array();

// Add the results to the arrays 
while($row = mysql_fetch_array( $result )) {
	array_push($filenames, "pictures/".$row['filename']);
} 


//create a string from the array with a pipe delimiter 
$imageSourcesString=implode("|",$filenames);

//echo "<td align='center' valign='middle' width='402'>".$imageSourcesString."</td>";

$descriptionString=0;

$pauseTime=200;
$fadeRate=8;

//$imageSourcesString="$imageSourcesString";

echo "<INPUT type = 'hidden' name = 'Page' value='&imageSourcesString=$imageSourcesString&pauseTime=$pauseTime&fadeRate=$fadeRate&sdd=$sdd'>";

echo $imageSourcesString;

?>

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="400" height="222" id="flashPhotos" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="flashPhotos.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#e5dec6" /><embed src="flashPhotos.swf" quality="high" bgcolor="#e5dec6" width="400" height="222" name="flashPhotos" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>



</body>
</html>
edit: FYI I changed some stuff around because I don't want people to know too much about the database, etc.

LP0 ON FIRE fucked around with this message at 01:34 on Jan 11, 2009

LP0 ON FIRE
Jan 25, 2006

beep boop

Lumpy posted:

How is the Flash supposed to get the stuff you generate to the hidden field? Magic? Why not tack on your vars to the end of the URL to the movie?

Flash can get the stuff on the hidden field perfectly fine except when the initial variable from GET that SQL uses for a query is used. If I just simply defined the variable instead of using the value of GET everything would be fine. I'd explain how this works all over again but I've explained this in at least 3 other replies so far. Read those so you can understand what the problem is more. But thanks for your suggestion I'll try that.

LP0 ON FIRE fucked around with this message at 05:12 on Jan 13, 2009

LP0 ON FIRE
Jan 25, 2006

beep boop

Lumpy posted:

Your replies are confusing at best. "Flash can get stuff out of hidden fields" doesn't make any sense whatsoever.

Flash actionscript can use loadVariables("currentpageURL.php", this, "POST"); to get POST variables from the current page that it is on. You can make the value of hidden fields like so to make a POST variable like so:
php:
<?

$variableSentToFlash="hello there";

echo "<INPUT type = 'hidden' name = 'Page' value='&variableSentToFlash=$variableSentToFlash'>";


?>

LP0 ON FIRE
Jan 25, 2006

beep boop
I've been using preg_match to only accept certain characters, but now I want to use it to accept certain characters AND spaces, but NOT leading spaces, and NOT more than one space in a row. Will that involve more than just preg_match?

I've tried the following to just plain old accept spaces, but it doesn't work! On another forum they showed something similar:

preg_match('%^[A-Za-z0-9_- ]+$%', $_POST["user"])

Notice the space after the "-"

LP0 ON FIRE
Jan 25, 2006

beep boop

KuruMonkey posted:

Question: do you really want to 'not accept' leading spaces and multiple spaces? Or just not have them in the final accepted string?

If the second:

php:
<?
$input = ' whatever  it is';
$input = ltrim($input, ' ');
$input = str_replace('  ', ' ', $input);
preg_match($your_current_pattern, $input);
?>
edit: might want a while loop in there to wittle many spaces down to 1? or a preg_replace to do the same?

edit 2: looking at your example it looks like you're going to look up a user name with this...
in that case; coerce the input like above for creating the username, and just allow spaces however when inputting a login that is checked against existing names. In that case you sanitise the input so its safe to use,and just let the fact that you can't create names with multiple or leading spaces mean that you cannot login to one anyway... (you have to handle the 'invalid username' case anyway, why add another for 'impossible username'?

Lastly, to check for wrong spacing only, use:
php:
<?
$coerced = ltrim(str_replace('  ', ' ', $input), ' ');
if(strcmp($input, $coerced) != 0)
{
// oops
}
?>


This is some great information for fixing up the spaces. Only problem is that I haven't had preg_match work correctly at accepting spaces. It works other wise.

php:
<?
if(preg_match('%^[A-Za-z0-9_- ]+$%', $_POST["user"])){

}else{
//return false
}
?>
Notice the space after the "-"

That if will return false with $_POST["user"] with or without spaces.

LP0 ON FIRE
Jan 25, 2006

beep boop
Thank you KuruMonkey and Munkeymon! I knew about escaping the - character but it never came to me when I saw that! Those condensed expressions are pretty awesome, but I have no idea what's REALLY going on in that, so I'll probably read up about it.

LP0 ON FIRE
Jan 25, 2006

beep boop
I haven't been able to get a cookie work from a page in one directory to another page in the root directory. If it's in the root directory, it works page to page in the root directory.

I use:
$expire=time()+60*60*24*30;
setcookie('user', $_POST["user"], $expire, '/', '.example.com');

LP0 ON FIRE fucked around with this message at 08:26 on Feb 27, 2009

LP0 ON FIRE
Jan 25, 2006

beep boop
I've copied and resized a picture before in PHP, but for some reason the image is resizing the correct size but comes out all black.

php:
<?php

define('filename'"testResize/testSource.png");

define('sizeIncreasePercent'5);

// Get new sizes
list($width$height) = getimagesize(filename);
define('newwidth'$width sizeIncreasePercent);
define('newheight'$height sizeIncreasePercent);

// Load
define('thumb'imagecreatetruecolor(newwidthnewheight));
define('source'filename);

// Resize
imagecopyresized(thumbsource0000newwidthnewheight$width$height);

// Output
imagepng(thumb"testResize/test.png");;

?>

LP0 ON FIRE
Jan 25, 2006

beep boop

gibbed posted:

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.

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.

LP0 ON FIRE
Jan 25, 2006

beep boop

gibbed posted:

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?

Excellent! Changing it to this: define('source', imagecreatefrompng(filename)); worked! It's exactly the effect I need. No softness or blurring. I don't have error reporting off.. I don't know why it didn't give me errors. Everything is fine now though, thank you.

The script may be used in a form, and I just think it's good practice for data that isn't going to change throughout the script to use a constant. I don't have exact plans how I'm going to change this code, but I know the constants can remain constants without a problem. I read about the importance of using constants in a form as a security measure here: http://dev.mysql.com/tech-resources/articles/php-security-ch02.pdf

LP0 ON FIRE fucked around with this message at 07:43 on Mar 10, 2009

LP0 ON FIRE
Jan 25, 2006

beep boop
Why can't I get this simple script to work? All I want to do is take an image from a URL and store it to a folder on my server. The folder has the correct permissions and I've tried a manual image uploader to that same folder and it works.

I've also wrote $fp as open('http://www.majoroutput.com/sonicCircus/images/', 'w'); (without the filename).

php:
<?php
$contents 'http://www.majoroutput.com/sonicCircus/test.jpg';

echo "<img src='".$contents."'>";

$fp fopen('http://www.majoroutput.com/sonicCircus/images/test.jpg''w');
fwrite($fp$contents);
fclose($fp);
?>
edit: I've also tried $contents = file_get_contents('http://www.majoroutput.com/sonicCircus/test.jpg');

LP0 ON FIRE fucked around with this message at 23:26 on Aug 14, 2009

LP0 ON FIRE
Jan 25, 2006

beep boop

waffle iron posted:

Are you trying to use the http file io wrappers to write? If all these files are on your webhost, why don't you just use copy? It also looks like you're passing the wrong arguments to fwrite.

http://php.net/fwrite

I'm just using files on the same host as a test. I've tried off the site too.

I've seen multiple people mention using fwrite to get offsite images and save them to your host, including:

http://www.webmasterworld.com/forum88/374.htm

Adbot
ADBOT LOVES YOU

LP0 ON FIRE
Jan 25, 2006

beep boop
I'm doing what you wrote: defining $contents with file_get_contents and putting the URL into a variable, if that does anything. Doesn't work. :(

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