Search Webmaster Help and Solution

Strange problem with characters - possible string corruption

Hi there,

Im writing a FTP wrapper class, and Im currently working on some functions which extract information from a ftp_rawlist() output.

PHP Code: function message($message) {
  echo $message . <br /> ;
}

function extractItemProperties($string) {
  $string = (string)$string;
  message($string in extractItemProperties():  . $string . );
  $item = explode( , $string, 9);
  //foreach ($item as $property) message($i . :  . $property);
  return $item;
}

//takes an array returned by extractItemProperties and outputs some information of the item, for example
//--- [(array size = 9), -rw-r--r--|1|0|root|341403|May|2|2003|RainSentinel.jpg|] (File) RainSentinel.jpg [341403 bytes] ---
//(size of array, array elements, and then the important stuff (file or folder, name of file/folder, and size, if its a file)
function evaluateItemProperties($item) {
  $report = [(array size =  . count($item) . ), ;
  for ($i=0; $i<count($item); $i++) {
    $report = $report . $item[$i] . |;
  }
  $report = $report . ] ;
  $fileorfolder = substr($item[0], 0, 1);
  if ($fileorfolder == d) $report = $report . (Folder) ;
  else $report = $report . (File) ;
  $report = $report . $item[8];
  if ($fileorfolder != d) $report = $report .  [ . $item[4] .  bytes];
  message(---  . $report .  ---);  



Some example parameter to extractItemProperties():
drwxr-xr-x 5 0 root 4096 Oct 9 20:25 disk1
-rw-r--r-- 1 0 root 152 Oct 19 11:05 welcome.msg
drw-rw-rw- 1 user group 0 Oct 29 18:04 Vids

The problems occur in the extractItemProperties() function. Basically, the spaces ( ) in $string arent properly recognised, out of some very weird reason. The $string parameters seem to be valid (juding by what I see when I output its value), but the function doesnt like it. It doesnt explode() the $string properly and thus the array it returns ($item) is b0rk3d.

The strange thing is that when I pass a string to evaluateItemProperties() manually, it works. The problems only occur when I pass strings which I get from my array which got its values from ftp_rawlist.

Heres some code to explain what I mean:

PHP Code: //basically stores what ftp_rawlist returned into $dir...
$dir = $ftp->getDir();

for ($i=0; $i<count($dir); $i++) {
  $string = $dir[$i];
  message($string in index.php:  . $string . );
  $item = extractItemProperties($string);
  evaluateItemProperties($item);
}

echo message(<br />Now using manually passed parameter to extractItemProperties...<br />);

$zitem = -rw-r--r-- 1 0 root 341403 May 2 2003 RainSentinel.jpg;
$rr = extractItemProperties($zitem);
evaluateItemProperties($rr); 


Heres some example output I get:

Quote: $string in index.php: "-rw-r--r-- 1 0 root 341403 May 2 2003 RainSentinel.jpg"
$string in extractItemProperties(): "-rw-r--r-- 1 0 root 341403 May 2 2003 RainSentinel.jpg"
--- [(array size = 9), -rw-r--r--|||1|0|||| root 341403 May 2 2003 RainSentinel.jpg|] (File) root 341403 May 2 2003 RainSentinel.jpg [0 bytes] ---
$string in index.php: "-rw-r--r-- 1 0 root 966470 Sep 5 16:26 RainStationMultiMons.jpg"
$string in extractItemProperties(): "-rw-r--r-- 1 0 root 966470 Sep 5 16:26 RainStationMultiMons.jpg"
--- [(array size = 9), -rw-r--r--|||1|0|||| root 966470 Sep 5 16:26 RainStationMultiMons.jpg|] (File) root 966470 Sep 5 16:26 RainStationMultiMons.jpg [0 bytes] ---
$string in index.php: "drwxr-xr-x 5 0 root 4096 Oct 9 20:25 disk1"
$string in extractItemProperties(): "drwxr-xr-x 5 0 root 4096 Oct 9 20:25 disk1"
--- [(array size = 9), drwxr-xr-x|||5|0|||| root 4096 Oct 9 20:25 disk1|] (Folder) root 4096 Oct 9 20:25 disk1 ---
$string in index.php: "-rw-r--r-- 1 0 root 152 Oct 19 11:05 welcome.msg"
$string in extractItemProperties(): "-rw-r--r-- 1 0 root 152 Oct 19 11:05 welcome.msg"
--- [(array size = 9), -rw-r--r--|||1|0|||| root 152 Oct 19 11:05 welcome.msg|] (File) root 152 Oct 19 11:05 welcome.msg [0 bytes] ---

Now using manually passed parameter to extractItemProperties...

$string in extractItemProperties(): "-rw-r--r-- 1 0 root 341403 May 2 2003 RainSentinel.jpg"
--- [(array size = 9), -rw-r--r--|1|0|root|341403|May|2|2003|RainSentinel.jpg|] (File) RainSentinel.jpg [341403 bytes] ---


The evaluateItemProperties() function outputs the elements of the array it gets as a parameter, and sepparates them by a |. As you can see, the arrays it gets werent properly split up by the extractItemProperties() function. The string it gets are somehow messed up, as when I added some code which counted the spaces ( characters) in the string, I got pretty high numbers (20-26, although there are only 8). Casting the string as a (string) didnt help either.

I have no clue why the extractItemProperties() function does this, since I can copy paste the strings which it apparently cant handle and feed them in manually, and then it works...

Do any of you spot any bug in my code, or have a clue what I could do to fix this?

Thanks in advance... Hi there,

Im writing a FTP wrapper class, and Im currently working on some functions which extract information from a ftp_rawlist() output.

PHP Code: function message($message) {
  echo $message . <br /> ;
}

function extractItemProperties($string) {
  $string = (string)$string;
  message($string in extractItemProperties():  . $string . );
  $item = explode( , $string, 9);
  //foreach ($item as $property) message($i . :  . $property);
  return $item;
}

//takes an array returned by extractItemProperties and outputs some information of the item, for example
//--- [(array size = 9), -rw-r--r--|1|0|root|341403|May|2|2003|RainSentinel.jpg|] (File) RainSentinel.jpg [341403 bytes] ---
//(size of array, array elements, and then the important stuff (file or folder, name of file/folder, and size, if its a file)
function evaluateItemProperties($item) {
  $report = [(array size =  . count($item) . ), ;
  for ($i=0; $i<count($item); $i++) {
    $report = $report . $item[$i] . |;
  }
  $report = $report . ] ;
  $fileorfolder = substr($item[0], 0, 1);
  if ($fileorfolder == d) $report = $report . (Folder) ;
  else $report = $report . (File) ;
  $report = $report . $item[8];
  if ($fileorfolder != d) $report = $report .  [ . $item[4] .  bytes];
  message(---  . $report .  ---);  



Some example parameter to extractItemProperties():
drwxr-xr-x 5 0 root 4096 Oct 9 20:25 disk1
-rw-r--r-- 1 0 root 152 Oct 19 11:05 welcome.msg
drw-rw-rw- 1 user group 0 Oct 29 18:04 Vids

The problems occur in the extractItemProperties() function. Basically, the spaces ( ) in $string arent properly recognised, out of some very weird reason. The $string parameters seem to be valid (juding by what I see when I output its value), but the function doesnt like it. It doesnt explode() the $string properly and thus the array it returns ($item) is b0rk3d.

The strange thing is that when I pass a string to evaluateItemProperties() manually, it works. The problems only occur when I pass strings which I get from my array which got its values from ftp_rawlist.

Heres some code to explain what I mean:

PHP Code: //basically stores what ftp_rawlist returned into $dir...
$dir = $ftp->getDir();

for ($i=0; $i<count($dir); $i++) {
  $string = $dir[$i];
  message($string in index.php:  . $string . );
  $item = extractItemProperties($string);
  evaluateItemProperties($item);
}

echo message(<br />Now using manually passed parameter to extractItemProperties...<br />);

$zitem = -rw-r--r-- 1 0 root 341403 May 2 2003 RainSentinel.jpg;
$rr = extractItemProperties($zitem);
evaluateItemProperties($rr); 


Heres some example output I get:

Quote: $string in index.php: "-rw-r--r-- 1 0 root 341403 May 2 2003 RainSentinel.jpg"
$string in extractItemProperties(): "-rw-r--r-- 1 0 root 341403 May 2 2003 RainSentinel.jpg"
--- [(array size = 9), -rw-r--r--|||1|0|||| root 341403 May 2 2003 RainSentinel.jpg|] (File) root 341403 May 2 2003 RainSentinel.jpg [0 bytes] ---
$string in index.php: "-rw-r--r-- 1 0 root 966470 Sep 5 16:26 RainStationMultiMons.jpg"
$string in extractItemProperties(): "-rw-r--r-- 1 0 root 966470 Sep 5 16:26 RainStationMultiMons.jpg"
--- [(array size = 9), -rw-r--r--|||1|0|||| root 966470 Sep 5 16:26 RainStationMultiMons.jpg|] (File) root 966470 Sep 5 16:26 RainStationMultiMons.jpg [0 bytes] ---
$string in index.php: "drwxr-xr-x 5 0 root 4096 Oct 9 20:25 disk1"
$string in extractItemProperties(): "drwxr-xr-x 5 0 root 4096 Oct 9 20:25 disk1"
--- [(array size = 9), drwxr-xr-x|||5|0|||| root 4096 Oct 9 20:25 disk1|] (Folder) root 4096 Oct 9 20:25 disk1 ---
$string in index.php: "-rw-r--r-- 1 0 root 152 Oct 19 11:05 welcome.msg"
$string in extractItemProperties(): "-rw-r--r-- 1 0 root 152 Oct 19 11:05 welcome.msg"
--- [(array size = 9), -rw-r--r--|||1|0|||| root 152 Oct 19 11:05 welcome.msg|] (File) root 152 Oct 19 11:05 welcome.msg [0 bytes] ---

Now using manually passed parameter to extractItemProperties...

$string in extractItemProperties(): "-rw-r--r-- 1 0 root 341403 May 2 2003 RainSentinel.jpg"
--- [(array size = 9), -rw-r--r--|1|0|root|341403|May|2|2003|RainSentinel.jpg|] (File) RainSentinel.jpg [341403 bytes] ---


The evaluateItemProperties() function outputs the elements of the array it gets as a parameter, and sepparates them by a |. As you can see, the arrays it gets werent properly split up by the extractItemProperties() function. The string it gets are somehow messed up, as when I added some code which counted the spaces ( characters) in the string, I got pretty high numbers (20-26, although there are only 8). Casting the string as a (string) didnt help either.

I have no clue why the extractItemProperties() function does this, since I can copy paste the strings which it apparently cant handle and feed them in manually, and then it works...

Do any of you spot any bug in my code, or have a clue what I could do to fix this?

Thanks in advance...

View Complete Thread with Replies

Related Items

Query failed: connection to localhost:3354 failed (errno=111, msg=Connection refused).