Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

Cannot understand why php code does not work

I have this php code.

<?php
include "db_connect_nb.php";
$pid = $_GET['pid'];
//echo "pid = " . $pid . "<br>";
$str = $_GET['str'];
echo "str = " . $str . "<br>";
$files = explode("~", $_GET['str']);
$nf = count($files);

// get rid of dupes
$fs = array();
$ns = 0;
for ($i = 0; $i < $nf; $i++) {
	echo "files " . $i . " = " . $files[$i] . "<br>";
	if ($files[$i] != "doc_files/" ) {
		if ($ns == 0) {
			$fs[0] = $files[0];
			$ns++;
		} else {
			$found = false;
			for ($j = 0; $j < $ns; $j++) {
				if ($files[$i] == $fs[$j]) {
					$found = true;
					break;
				}
			}
			if (!$found) {
				$fs[$ns] = $files[$i];
				$ns++;
			}
		}
	}
}
for($i = 0; $i < $ns; $i++) {
	echo "files = " . $fs[$i] . "<br>";
}

Open in new window


There is some more beyond that, I can't see they are relevant.

All the echos are to determine why an incoming file in $_GET['str'] that is equal to "doc_files/" is not deleted by the second loop.

See attached for output. Some how one of these gets through & screws up everything after that.

Thanks
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi Richard,

Can you explain exactly what you're trying to do. It seems that you're trying to read in a list of files from the querystring, delimited by the ~ and then remove any duplicates and entries called doc_files/

If that's the case, then there's no need to do all those loops and arrays. You can use array_unique and array_filter.

The code you have does remove any entries that are called doc_files/
There's no attachment, and I'm not sure what your expected output is. I ran this:
<?php
$_GET["str"] = "a~b~c~doc_files/~d";
//echo "pid = " . $pid . "<br>";
$str = $_GET['str'];
echo "str = " . $str . "<br>";
$files = explode("~", $_GET['str']);
$nf = count($files);

// get rid of dupes
$fs = array();
$ns = 0;
for ($i = 0; $i < $nf; $i++) {
	echo "files " . $i . " = " . $files[$i] . "<br>";
	if ($files[$i] != "doc_files/" ) {
		if ($ns == 0) {
			$fs[0] = $files[0];
			$ns++;
		} else {
			$found = false;
			for ($j = 0; $j < $ns; $j++) {
				if ($files[$i] == $fs[$j]) {
					$found = true;
					break;
				}
			}
			if (!$found) {
				$fs[$ns] = $files[$i];
				$ns++;
			}
		}
	}
}
for($i = 0; $i < $ns; $i++) {
	echo "files = " . $fs[$i] . "<br>";
}

Open in new window


...and I get this output:
str = a~b~c~doc_files/~d
files 0 = a
files 1 = b
files 2 = c
files 3 = doc_files/
files 4 = d
files = a
files = b
files = c
files = d

Open in new window

Have a look at this. I think it's what you're after:

$files = isset( $_GET['str'] ) ? explode( "~", $_GET['str'] ) : array();

$uniqueFiles =  array_unique( $files );

$fileList = array_filter( $uniqueFiles, function( $file ) {
    return $file != 'doc_files/';
});

Open in new window

Quick test:

?pid=123&str=file1.doc~file2.doc~doc_files/~file1.doc~file3.doc

// Result:
array (size=3)
  0 => string 'file1.doc' (length=9)
  1 => string 'file2.doc' (length=9)
  4 => string 'file3.doc' (length=9)

You can call array_values() if you really need to flatted the indexes
Avatar of Richard Korts

ASKER

i THOUGHT i ATTACHED THE FILE. hERE IT IS.

I want it to work like yours.
debug-from-dnld_psel.PNG
Maybe the rest of the code below this matters, I can't see how. Attached is the FULL php file.
dnld_psel.php
Chris, I will try that, looks good.

Thanks,

Richard
Right. You're checking that the filename is EXACTLY doc_files/ instead of checking whether it starts with doc_files/

Using my code, just edit the file callback:

$files = isset( $_GET['str'] ) ? explode( "~", $_GET['str'] ) : array();

$uniqueFiles =  array_unique( $files );

$fileList = array_filter( $uniqueFiles, function( $file ) {
   return strncmp( $file, 'doc_files/', 10 ) !== 0;
});

Open in new window

Chris,

I used your code, modified slightly. The entire php file is attached.

What I get from the echos is this:

str = doc_files/~doc_files/~doc_files/~doc_files/~doc_files/LS-910-eTCX-System-Brochure.pdf~doc_files/LS-710-TowerClean-Brochure.pdf~doc_files/LS-910-eTCX-System-Brochure.pdf~doc_files/LS-710-TowerClean-Brochure.pdf~doc_files/LS-911_eTCX_Sample_Spec.doc~doc_files/LS-711-TowerClean-SpecSheet.rtf~doc_files/LS-911_eTCX_Sample_Spec.doc~doc_files/LS-711-TowerClean-SpecSheet.rtf~NP_3512.pdf
files =
files =
files =
files =
files = doc_files/LS-910-eTCX-System-Brochure.pdf

Why are the first 4 array entries empty?

Thanks
dnld_psel.php
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial