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
https://forums.phpfreaks.com/topic/120708-solved-function-help/
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
https://forums.phpfreaks.com/topic/120708-solved-function-help/#findComment-621975
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
https://forums.phpfreaks.com/topic/120708-solved-function-help/#findComment-622010
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
https://forums.phpfreaks.com/topic/120708-solved-function-help/#findComment-622017
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
https://forums.phpfreaks.com/topic/120708-solved-function-help/#findComment-622031
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
https://forums.phpfreaks.com/topic/120708-solved-function-help/#findComment-622213
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
https://forums.phpfreaks.com/topic/120708-solved-function-help/#findComment-622250
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.