PHP sucks (help with php needed urgently!)

Started by Pet Terry, Sun 24/02/2008 12:56:13

Previous topic - Next topic

Pet Terry

I've been working on my first big project involving php lately. It's a website for a band and they wanted the site to be easily updated by themselves, so update system written in php was a natural choice. I've had pretty much everything else nailed down now, but I'm having problems with the image gallery page. I'm desperate enough to post about this on the AGS forums because it seems I can't get help anywhere else.

Basically, it's going to be quite a simple system. The band uploads the images in a directory via ftp. The page then goes through all the subdirectories under given root directory, prints all the names of subdirectories on the page and then displays the images from the directories under the names.

I posted about it on the php freaks forum, but didn't really get any useful help. The thread can be found at http://www.phpfreaks.com/forums/index.php/topic,183582.msg822546.html#msg822546

The deadline is tomorrow... please, I really need help.
<SSH> heavy pettering
Screen 7

tube

/images/galleria/promokuvat/testi/ is probably not the real absolute path. You can get the absolute path to a relative one by calling realpath(). Try putting it inside your opendir() call like this: opendir(realpath($dir)). I'm pretty much just guessing based on a quick peek at your script, so that might not help much.

PS: Was there any reason why you couldn't use a simple cms like phpsqlitecms instead of doing it all from scratch?

Pet Terry

Adding realpath() gave me "unexpected {" error on the line that has opendir().

I put echo $dir; and echo realpath($dir); before opendir() - echo $dir; printed "images/galleria/promokuvat/" and echo realpath($dir); printed "/var/www/vhosts/yadapops.com/httpdocs/jrh/images/galleria/promokuvattesti
testi/". Neither of those are correct!

As for why to do all from scratch... I want to learn.
<SSH> heavy pettering
Screen 7

tube

Quote from: Petteri on Sun 24/02/2008 14:01:37
Adding realpath() gave me "unexpected {" error on the line that has opendir().

I put echo $dir; and echo realpath($dir); before opendir() - echo $dir; printed "images/galleria/promokuvat/" and echo realpath($dir); printed "/var/www/vhosts/yadapops.com/httpdocs/jrh/images/galleria/promokuvattesti
testi/". Neither of those are correct!
The realpath() output seems weird indeed. Are you sure you pasted it correctly? Where the hell does it get that "testi testi" if $dir is "images/galleria/promokuvat/"?

Quote from: Petteri on Sun 24/02/2008 14:01:37
As for why to do all from scratch... I want to learn.
Good for you, except maybe a tight deadline isn't the right time to do things the hard way on purpose. (As if I have never done the same mistake. :))

Pet Terry

"testi" is the name of the subdirectory under "images/galleria/promokuvat". The part of the code that prints the name of the subdirectory to the page actually works. Could it be $dir = $dir."/"; part that adds "testi/" to "testi", equaling to "testitesti/"? Then again, I'm not sure if that makes any sense... I can't remember if that's actually how it's supposed to work? And why the echo with realpath() doesn't include "testi" at all?

Quote from: tube on Sun 24/02/2008 14:15:27
Quote from: Petteri on Sun 24/02/2008 14:01:37
As for why to do all from scratch... I want to learn.
Good for you, except maybe a tight deadline isn't the right time to do things the hard way on purpose. (As if I have never done the same mistake. :))

Well, I did start coding the page weeks ago and left the image page last - I had an old code similar to this and I though modifying it a bit would be a piece of cake. Turned out it wasn't!
<SSH> heavy pettering
Screen 7

tube

Quote from: Petteri on Sun 24/02/2008 14:24:57
"testi" is the name of the subdirectory under "images/galleria/promokuvat". The part of the code that prints the name of the subdirectory to the page actually works. Could it be $dir = $dir."/"; part that adds "testi/" to "testi", equaling to "testitesti/"? Then again, I'm not sure if that makes any sense... I can't remember if that's actually how it's supposed to work? And why the echo with realpath() doesn't include "testi" at all?

Ok, let's start at the beginning. According to the error messages you posted to the other forum the script seems to bomb out at the call to foreach() on line 29 because the call to readDirectory on the previous line doesn't return an array. This probably indicates that variable $folder passed to readDirectory might be the culprit. (Proper return value checking would make your job a lot easier. There'd be much less guessing involved.)

Check the contents of $folder before the call to readDirectory() there if you're not certain. If it isn't a proper directory (and it most probably isn't as you've just stripped the preceding path information a few lines above), you know where to start fixing.

If I'm right and $folder only contains a directory name without the path, you'll need to substitute lines 17-20 currently reading like this
Code: ags

				$pieces = explode("/", $d);
				$folder = $pieces[count($pieces)-1];
				print "$folder<br>\n";
				showThumbnails($folder, $thumbsPerRow);

with something like this
Code: ags

				echo basename($d) . "<br>\n";
				showThumbnails($d, $thumbsPerRow);

to pass an actual path to the showThumbnails function. Well okay, only the row with the showThumbnails call needs changing, but I couldn't resist pointing out how you can simplify things with the basename() function. You might still need to use realpath() to get the absolute path if opendir() requires one.

Again, that might or might not make any sense as I didn't go through all the script and I haven't got the time to think this though right now. HTH anyway.

PS: I might be able to take some time tonight to go through this with you via IM or something if you can't get it to work.

Pet Terry

Holy crap, tube! That really seemed to be the case! Echoing $folder only displayed the name of the directory, not full path. It's still giving me some errors - "Undefined variable: picCount", "Undefined variable: dirArray" and "Invalid argument supplied for foreach()" - but hey, at least the images are displayed now! Now I just have to try and figure out how to get rid of those errors. I guess I have to supply foreach() with something else than $folder?

Thank you so much for your help, by the way. You're a real lifesaver!
<SSH> heavy pettering
Screen 7

tube

Can't give you anything concrete if you don't give me a bit more info about the errors, but seems like the error about $picCount not having been defined should be fixed if you initialize the variable to 0 before entering the foreach loop where it's used. You should also initialize $dirArray to an empty array with $dirArray = array(); before the while loop at the end of your script. If you still get the foreach error after fixing those (and why wouldn't you), please provide a bit more info starting with the line number.

Pet Terry

Great! That did the trick. Thank you again, you're the greatest. :)
<SSH> heavy pettering
Screen 7

tube

Quote from: Petteri on Sun 24/02/2008 16:42:49
Great! That did the trick. Thank you again, you're the greatest. :)

I aim to please.

SMF spam blocked by CleanTalk