Jump to content

[SOLVED] Function help


adam291086

Recommended Posts

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

Link to comment
Share on other sites

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;
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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);
  }

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

}
}
?>

Link to comment
Share on other sites

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;
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.