Jump to content

NEED HELP w displaying multiple files with similar filenames!


prototype18

Recommended Posts

I need to display multiple files that have a similar file name, but there are timestamps added to the filename once its been modified.  What could I use in PHP to display all these files?

 

Right now I use a $path variable that looks into a SQL table to build the path to the folder that the files reside in..

 

ELSEIF($Recordmyrow['Applicatio'] == "pdf file")
	{
		IF($securityvalue != "P"){$display = false;}
		IF(file_exists(substr($path, 5))){}
		ELSE{$column4 = "<A href='javascript:mbox();'>UNAVAILABLE</A>";}
	}

 

So there is a file called file1.pdf then another one called file1.mmddyyyy.pdf

 

My code shows file1.pdf but not file1.mmddyyyy.pdf

 

Is there some kind of wildcard I can use to get it to show all the files if they exist?

 

 

Link to comment
Share on other sites

So your logic says...

 

Check for securityvalue, if its not right, then set a boolean to false.

Check for a file to exist (the file name starts at 5 characters into the $path variable), and if it exists do nothing. If it doesn't exist, save a link to the $column4 variable.

 

Well, do you not want it to *do* something if the file exists?

 

You say your code 'shows' one file but not another. Where does it 'show' it?

 

I'm assuming $path is the full path to the pdf file, not just the directory? or?

Link to comment
Share on other sites

Assuming all the files will be pdf's and are in the same directory, here is all you need.

 

$files = glob(substr($basefile, 0, -4)."*.pdf");

 

$files will be an array of all the files that match the $basefile with the same name in the format you specified above.

 

NOTE: That example is case sensitive. So, if the primary name of the file has a different case than the other files or if some files use "pdf" while others use "PDF" you will not get all the matches. You can check the manual for glob() in the "User Contributed Notes" to see examples on how to make the matching case-insensitive.

 

http://us3.php.net/manual/en/function.glob.php

Link to comment
Share on other sites

Okay, I screwed up somehow, because the code I put up before isn't where the path is being built... I guess...

 

Here is the code that has an effect on the actual path...

 

//Check display value and print row				
	IF($display)
	{
		//Check if file exists
		IF(substr($path, 0, 5) == "file:")
		{
			IF(file_exists(substr($path, 5)))
			{
				IF(strtolower(substr($path, -4, 4)) == ".pdf")
				{
					$column4 = "<A HREF=pdf.php?src=".substr($path,5).">VIEW</A>";	
				}


				IF(strtolower(substr($path, -4, 4)) == ".jpg")
				{
					$column4 = "<A HREF=jpg.php?src=".substr($path,5).">VIEW</A>";	
				}
			}
			ELSE{$column4 = "Not Available";}
		}

 

So can I throw the glob function somewhere in here...? :confused:

Link to comment
Share on other sites

It appears that the file name is part of a string, specifically:

file_exists(substr($path, 5)

 

By the way, instead of using that same function multiple times, just assign that to a variable so the PHP parser doesn't need to run that function multiple times.

 

Not knowing everything about your particular need, this is just a shot in the dark:

//Check display value and print row
if($display)
{
    //Check if file exists
    if(substr($path, 0, 5) == "file:")
    {
        $filename = substr($path, 5);
        if(file_exists($filename))
        {
            $basename = substr($filename, 0, strrpos($filename, '.'));
            $ext      = strtolower(substr(strrchr($filename, '.')));
            $files = glob("{$basename}.*.pdf");

            foreach($files as $file)
            {
                if($ext == ".pdf")
                {
                    $column4 = "{$file} <a href=\"pdf.php?src={$file}\">VIEW</a><br />\n";
                }
                if($ext == ".jpg")
                {
                    $column4 = "{$file} <a href=\"jpg.php?src={$file}\">VIEW</a><br />\n";
                }
            }
        }
        else
        {
            $column4 = "Not Available";
        }
    }
}

Link to comment
Share on other sites

I don't think the glob function is working because the files that need to be accessed are on a shared network drive.

 

Would that screw things up?

 

As long as you can access the "default" file I see no reason you wouldn't be able to access the other files in the same directory. You say you don't think it is working but have provided no details as to why you believe it is not working. Give an example of a default file name and the other files in the same directory that should match.

 

I tested the logic above so I know it works. Using a folder with the following files:

 

convert.file_size.php

convert.images.php

convert.php

create mp3 playlist.php

 

I used the following code:

$path = 'file:temp/convert.php';
$filename =  substr($path, 5);
$basename = substr($filename, 0, strrpos($filename, '.'));
$ext      = strrchr($filename, '.');
$allfiles = glob("{$basename}.*.php");

 

And get the following output:

Array
(
    [0] => temp/convert.file_size.php
    [1] => temp/convert.images.php
)

Link to comment
Share on other sites

Okay, this is the code that builds the $path variable.  Your code did kind of work, just didn't give the desired result.

 

The path variable is looking at a SQL table for these UNC paths to folders on the network.

 


	//Replace variables in path
	$path = $Recordmyrow['Path'];
	$path = str_replace("['XXX']",$item,$path);
	$path = str_replace("['XXX']",$sqlmyrow['XXX''],$path);
	$path = str_replace("['XXX']",$sqlmyrow['XXX''],$path);
	$path = str_replace("['XXX']",$sqlmyrow['XXX''],$path);
	$path = str_replace("['XXX']",$sqlmyrow['XXX''],$path);
	$path = str_replace("['SESSIONID']",session_id(),$path);
	$path = str_replace("['APPLICATIO']",$applicatio,$path);
	$path = str_replace("['APPLICATIO3']",substr($applicatio,3),$path);
	$path = str_replace("['YEAR24']",substr($applicatio,2,4),$path);
	$path = str_replace("['YEAR34']",substr($applicatio,3,4),$path);
	$path = str_replace("['YEAR44']",substr($applicatio,4,4),$path);
	$path = str_replace("['YEAR54']",substr($applicatio,5,4),$path);
	$path = str_replace("['LATITUDE']",$sqlmyrow['LAT'],$path);
	$path = str_replace("['LONGITUDE']",$sqlmyrow['LON'],$path);


	//
	ELSEIF($Recordmyrow['Applicatio'] == "CO")
	{
		IF($securityvalue == "P"){$display = false;}
		IF(file_exists(substr($path, 5))){}
		ELSE{$column4 = "<A href='javascript:mbox();'>UNAVAILABLE</A>";}

	}
        //
	ELSEIF($Recordmyrow['Applicatio'] == "CO (Printable)")
	{
		IF($securityvalue != "P"){$display = false;}
		IF(file_exists(substr($path, 5))){}
		ELSE{$column4 = "<A href='javascript:mbox();'>UNAVAILABLE</A>";}
	}

	//Check display value and print row				
	IF($display)
	{
		if(substr($path, 0, 5) == "file:")
    {
        $filename = substr($path, 5);
        if(file_exists($filename))
        {
            $basename = substr($filename, 0, strrpos($filename, '.'));
            $ext      = strtolower(substr(strrchr($filename, '.')));
            $files = glob("{$basename}.*.pdf");

            foreach($files as $file)
            {
                if($ext == ".pdf")
                {
                    $column4 = "{$file} <a href=\"pdf.php?src={$file}\">VIEW</a><br />\n";
                }
                if($ext == ".jpg")
                {
                    $column4 = "{$file} <a href=\"jpg.php?src={$file}\">VIEW</a><br />\n";
                }
            }
        }
        else
        {
            $column4 = "Not Available";
        }
    }
}

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.