Page 1 of 1

PHP beginner - confused - help

Posted: Tue May 11, 2010 4:03 pm
by markr
Hi, I am an old Cobol and rpg programmer who is getting into php. I have put together a web page that is meant to get a list of file names from a specific folder and display the images. The intention is a basic gallery software for a friends website, and yes , I have suggested one of the free or cheap commercial packages.

I am using an apache server on my desktop system as 127.0.0.1 and have php installed per the instructions.

Here is the code
The scandir function did not work as expected , which was to use the C:\Program Files\Apache Software Foundation\Apache2.2\htdocs folder as the default . Its what is shown for getcwd.

The html code works if I save the generated source and open it from windows explorer but not from apache.

Any suggestions are welcome and I am aware that this code is likely to be poor ;( but you have to start somewhere.
I have been extending it a little at a time so some stuff is not needed and commented out.

Thanks :banghead:

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}

function imgtype ($imgt)
{
$timg = "None";
if ($imgt == IMAGETYPE_GIF)
$timg = "Gif";
if ($imgt == IMAGETYPE_JPEG)
$timg = "Jpg";
if ($imgt == IMAGETYPE_PNG)
$timg = "Png";
if ($imgt == IMAGETYPE_BMP)
$timg = "Bmp";
if ($imgt == IMAGETYPE_PSD)
$timg = "Psd";
return $timg;
}

$dir = 'C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\photo';
chdir($dir);
$files1 = scandir(".");
foreach ($files1 as $fil) {
//if (is_dir($fil))
//echo "Dir: $fil <br>\n";
if (is_file($fil)) {
$fex = findexts($fil);
if ($fex != strtolower($fil))
//echo "File: $fil $fex <br>";
list($width, $height, $type, $attr) = getimagesize("$fil");
$imgt = imgtype($type);
//echo "$width $height $imgt $attr <br>";
$tmp = "photo\\".$fil ;
echo "<img src='$tmp' $attr ><br>\n";
}
}
?>
</body>
</html>

Re: PHP beginner - confused - help

Posted: Tue May 11, 2010 4:15 pm
by davex
Hi,

Looks to me like path issue - on a very quick scan I see for example you're using a backslash in the photos path. Explorer won't mind this at all but I'm pretty sure Apache and/or a browser may not handle it.

You say scandir() didn't work properly - what was it doing? Is this working now you have done the chdir()?

Also it looks like you're file scanning the current directory but then the link (with backslash as mentioned) is listing the photos directory?

Perhaps please post your output (in code/syntax tags if possible).

Cheers,

Dave.

Re: PHP beginner - confused - help

Posted: Tue May 11, 2010 4:44 pm
by markr
I have used the chdir to get the scandir working for the current folder.
Here is the code from running that php on my server.
I suspect something isnt configured properly at the server but wanted to make sure the code wasnt doing something bad.

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<img src='photo\NoThumb.bmp' width="91" height="91" ><br>
<img src='photo\mousehead.png' width="451" height="384" ><br>
<img src='photo\newjob.jpg' width="800" height="549" ><br>

</body>

</html>

Re: PHP beginner - confused - help

Posted: Tue May 11, 2010 4:45 pm
by davex
Just try changing the backslash in the path to a forward slash (in the path output to the <img> tag that is).

Cheers,

Dave.

Re: PHP beginner - confused - help

Posted: Tue May 11, 2010 5:07 pm
by markr
excellent. that got it working , thanks
I can see this is as fun as python about the // \\ thing .

Re: PHP beginner - confused - help

Posted: Tue May 11, 2010 5:09 pm
by davex
Good glad it's working.

I think you'll find that most file level functions work ok with / even on Windows so probably best to just use them all round and then it's easily ported to any other environment.

Good luck with your project.

Cheers,

Dave.