adam291086 Posted August 21, 2008 Share Posted August 21, 2008 I am trying to run a function to determin which image to use for each different file type. Here the Function function filetype($image) { switch ($image) { case jpg: $src = <img src=\"images/image.png\" border=\"0\"> break; case php: $src = <img src=\"images/web.png\" border=\"0\"> break; default: $src = <img src=\"images/file.png\" border=\"0\"> } return $src; } i have include('code/filetype'); at the top of the page and i call the function like //getting the file extension $path_parts = pathinfo($trimFile); $path_parts = $path_parts['extension']; $image = filetype($path_parts) ?> <a href='code/download.php?currFile=<?php echo $fList[$i]; ?>&currDir=<?php echo $currDir; ?>'><?php echo $image ?></a><?php echo $trimFile; ?><br /> No image is being displayed and i cant see why Quote Link to comment Share on other sites More sharing options...
trq Posted August 21, 2008 Share Posted August 21, 2008 function filetype($image) { switch ($image) { case 'jpg': $src = "<img src=\"images/image.png\" border=\"0\">"; break; case 'php': $src = "<img src=\"images/web.png\" border=\"0\">"; break; default: $src = "<img src=\"images/file.png\" border=\"0\">"; } return $src; } ?> Quote Link to comment Share on other sites More sharing options...
adam291086 Posted August 21, 2008 Author Share Posted August 21, 2008 i am still getting nothing Quote Link to comment Share on other sites More sharing options...
adam291086 Posted August 21, 2008 Author Share Posted August 21, 2008 ok realised i had messed up the include part now i get the error message Fatal error: Cannot redeclare filetype() in /homepages/12/d214897219/htdocs/rubberduckiee/admin/FTP/code/filetype.php on line 2 Quote Link to comment Share on other sites More sharing options...
trq Posted August 21, 2008 Share Posted August 21, 2008 ok realised i had messed up the include part No it would appear you are somehow including the file twice. Also, you html is invalid, so it not going to display properly. Its pretty hard to actually see what your trying to do here. Quote Link to comment Share on other sites More sharing options...
adam291086 Posted August 21, 2008 Author Share Posted August 21, 2008 i had messed up the include as i hadn't put .php on the end of the file type. Heres what i am doing I am making an FTP client. For each item in the directory it is looping through to find out if its a file or a folder. If its a folder then display the folder image. For the files i use the $path_parts = pathinfo($trimFile); $path_parts = $path_parts['extension']; to get the file extetion type. i then call on the function to decide which image to display depending on the file extention type. I then echo out the result from the switch statement. I can see that the problem is that i am trying to redeclare the fuction but i need to so that every file has its image determined. How can i get around this as the function needs to be defined for every iteration of the for loop Quote Link to comment Share on other sites More sharing options...
trq Posted August 21, 2008 Share Posted August 21, 2008 as the function needs to be defined for every iteration of the for loop No, the function needs to be called for every iteration, not defined. There is a difference. Example: wrong <?php foreach (range(0,10) as $i) { function foo($i) { echo $i; } foo($i); } ?> correct <?php function foo($i) { echo $i; } foreach (range(0,10) as $i) { foo($i); } ?> Quote Link to comment Share on other sites More sharing options...
adam291086 Posted August 21, 2008 Author Share Posted August 21, 2008 do i still have include('code/ftp.php') at the top of that page? Quote Link to comment Share on other sites More sharing options...
adam291086 Posted August 21, 2008 Author Share Posted August 21, 2008 still getting the error Fatal error: Cannot redeclare filetype() in /homepages/12/d214897219/htdocs/rubberduckiee/admin/FTP/code/filetype.php on line heres my for loop (sorry the code is a mess) for($i = 2; $i < sizeof($fList); $i++) { // We will remove the parent directory (if any) // from the name of the file that gets displayed $trimFile = ereg_replace("^$currDir", "", $fList[$i]); // Remove any forward slash at front of name $trimFile = ereg_replace("^/", "", $trimFile); if(@ftp_chdir($conn, $fList[$i])) { @ftp_cdup($conn); // Display the folders ?> <tr> <td> <a href='?command=listFiles&currDir=<?php echo $fList[$i]; ?>'><img src='images/folder.png' border="0"></a><?php echo $trimFile; ?><br /> </td> <td> <?php $filename = $fList[$i]; $filename = str_replace("/rubberduckiee", "", $filename); $filename= $_SERVER['DOCUMENT_ROOT'].$filename; echo "Last modified " . date("l, dS F, Y @ h:ia", filemtime($filename)); ?> </td> <td> <?php $filename = $fList[$i]; $filename = str_replace("/rubberduckiee", "", $filename); $filename= $_SERVER['DOCUMENT_ROOT'].$filename; echo filesize($filename) . ' bytes'; ?> </td> </tr> <?php } //files else { //getting the file extension $path_parts = pathinfo($trimFile); $path_parts = $path_parts['extension']; ?> <tr> <td> //this is where i call the function <a href='code/download.php?currFile=<?php echo $fList[$i]; ?>&currDir=<?php echo $currDir; ?>'><?php filetype($path_parts); ?></a><?php echo $trimFile; ?><br /> </td> <td> <?php $filename = $fList[$i]; $filename = str_replace("/rubberduckiee", "", $filename); $filename= $_SERVER['DOCUMENT_ROOT'].$filename; echo "Last modified " . date("l, dS F, Y @ h:ia", filemtime($filename)); ?> </td> <td> <?php $filename = $fList[$i]; $filename = str_replace("/rubberduckiee", "", $filename); $filename= $_SERVER['DOCUMENT_ROOT'].$filename; echo filesize($filename) . ' bytes'; ?> </td> </tr> <?php } } ?> i have at the top of the page that contains the for loop include('code/filetype.php'); so the request happens on each interation of the for loop unless i am not understanding you Quote Link to comment Share on other sites More sharing options...
trq Posted August 21, 2008 Share Posted August 21, 2008 Can you indent your code so its readable? Quote Link to comment Share on other sites More sharing options...
adam291086 Posted August 21, 2008 Author Share Posted August 21, 2008 hope that is better for($i = 2; $i < sizeof($fList); $i++) { // We will remove the parent directory (if any) // from the name of the file that gets displayed $trimFile = ereg_replace("^$currDir", "", $fList[$i]); // Remove any forward slash at front of name $trimFile = ereg_replace("^/", "", $trimFile); if(@ftp_chdir($conn, $fList[$i])) { @ftp_cdup($conn); // Display the folders ?> <tr> <td> <a href='?command=listFiles&currDir=<?php echo $fList[$i]; ?>'><img src='images/folder.png' border="0"></a><?php echo $trimFile; ?><br /> </td> <td> <?php $filename = $fList[$i]; $filename = str_replace("/rubberduckiee", "", $filename); $filename= $_SERVER['DOCUMENT_ROOT'].$filename; echo "Last modified " . date("l, dS F, Y @ h:ia", filemtime($filename)); ?> </td> <td> <?php $filename = $fList[$i]; $filename = str_replace("/rubberduckiee", "", $filename); $filename= $_SERVER['DOCUMENT_ROOT'].$filename; echo filesize($filename) . ' bytes'; ?> </td> </tr> <?php } //files else { //getting the file extension $path_parts = pathinfo($trimFile); $path_parts = $path_parts['extension']; ?> <tr> <td> <a href='code/download.php?currFile=<?php echo $fList[$i]; ?>&currDir=<?php echo $currDir; ?>'><?php filetype($path_parts); ?></a><?php echo $trimFile; ?><br /> </td> <td> <?php $filename = $fList[$i]; $filename = str_replace("/rubberduckiee", "", $filename); $filename= $_SERVER['DOCUMENT_ROOT'].$filename; echo "Last modified " . date("l, dS F, Y @ h:ia", filemtime($filename)); ?> </td> <td> <?php $filename = $fList[$i]; $filename = str_replace("/rubberduckiee", "", $filename); $filename= $_SERVER['DOCUMENT_ROOT'].$filename; echo filesize($filename) . ' bytes'; ?> </td> </tr> <?php } } ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted August 21, 2008 Share Posted August 21, 2008 you call the function, but you don't echo the output. if you want to see what the function returned, you'll need to send it to the browser: <?php echo filetype($path_parts); ?> Quote Link to comment Share on other sites More sharing options...
adam291086 Posted August 21, 2008 Author Share Posted August 21, 2008 still get the error Fatal error: Cannot redeclare filetype() in /homepages/12/d214897219/htdocs/rubberduckiee/admin/FTP/code/filetype.php on line 2 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted August 21, 2008 Share Posted August 21, 2008 are you showing us the code in filetype.php? Quote Link to comment Share on other sites More sharing options...
adam291086 Posted August 21, 2008 Author Share Posted August 21, 2008 yep its the function above, but here it is again <?php function filetype($image) { switch ($image) { case 'jpg': $src = "<img src=\"images/image.png\" border=\"0\">"; break; case 'php': $src = "<img src=\"images/web.png\" border=\"0\">"; break; default: $src = "<img src=\"images/file.png\" border=\"0\">"; } return $src; } ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted August 21, 2008 Share Posted August 21, 2008 doh, i'm stupid. filetype is already a PHP function. you're getting the error because you are re-declaring an existing PHP function. use a different name for the function. Quote Link to comment Share on other sites More sharing options...
adam291086 Posted August 21, 2008 Author Share Posted August 21, 2008 genius. Its works beautifully. Thanks Guys Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.