Richard Korts
asked on
Cannot understand why php code does not work
I have this php code.
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
<?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>";
}
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
There's no attachment, and I'm not sure what your expected output is. I ran this:
...and I get this output:
<?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>";
}
...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
Have a look at this. I think it's what you're after:
?pid=123&str=file1.doc~fil e2.doc~doc _files/~fi le1.doc~fi le3.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
$files = isset( $_GET['str'] ) ? explode( "~", $_GET['str'] ) : array();
$uniqueFiles = array_unique( $files );
$fileList = array_filter( $uniqueFiles, function( $file ) {
return $file != 'doc_files/';
});
Quick test:?pid=123&str=file1.doc~fil
// 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
ASKER
ASKER
Maybe the rest of the code below this matters, I can't see how. Attached is the FULL php file.
dnld_psel.php
dnld_psel.php
ASKER
Chris, I will try that, looks good.
Thanks,
Richard
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:
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;
});
ASKER
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/~do c_files/LS -910-eTCX- System-Bro chure.pdf~ doc_files/ LS-710-Tow erClean-Br ochure.pdf ~doc_files /LS-910-eT CX-System- Brochure.p df~doc_fil es/LS-710- TowerClean -Brochure. pdf~doc_fi les/LS-911 _eTCX_Samp le_Spec.do c~doc_file s/LS-711-T owerClean- SpecSheet. rtf~doc_fi les/LS-911 _eTCX_Samp le_Spec.do c~doc_file s/LS-711-T owerClean- SpecSheet. rtf~NP_351 2.pdf
files =
files =
files =
files =
files = doc_files/LS-910-eTCX-Syst em-Brochur e.pdf
Why are the first 4 array entries empty?
Thanks
dnld_psel.php
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 =
files =
files =
files =
files = doc_files/LS-910-eTCX-Syst
Why are the first 4 array entries empty?
Thanks
dnld_psel.php
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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/