Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

May Have Discovered a Bug; Requesting Feedback

General discussions related to php

Moderators: macek, egami, gesf

May Have Discovered a Bug; Requesting Feedback

Postby DanFord2014 » Fri Aug 29, 2014 1:07 pm

I have been working on a project which unzips and processes a file. The zip filename can be just about anything, so I have to parse carefully. This is some example code I wrote to demo the problem:

Code: Select all
$str1 = "abcdef/abcdef/abcdef.zip";
echo "<p>Original String: ".$str1."</p>";

$strtoltrim = "abcdef/abcdef/";
echo "<p>String to ltrim: ".$strtoltrim."</p>";

$str1 = ltrim($str1, "abcdef/abcdef/");
echo "<p>String after ltrim: ".$str1."</p>";
//output: .zip


As you'll see, if you run this, it strips ALL THREE instances of "abcdef" from the string, leaving only the ".zip" at the end. I actually WANT the last instance of "abcdef" plus the ".zip," so I should see "abcdef.zip as the final result.

In real world terms, here's what I see:

If I upload two zip files, one named "Png_kittens.zip," and one named "png_kittens.zip," (the only difference being the "P" is capitalized in one file name), using ltrim to take off any preceeding directory info works in the case of the capitalized instance, but chops off characters from the uncapitalized instance. This is part of the actual code:

Code: Select all
//__DIR__ = /var/www/html/images

$accountsdir = __DIR__."/accounts/";
$holddir = __DIR__."/wbs_zip_hold/";
$ziptodir = __DIR__."/wbs_zip_hold/wbs_zipto/";
$archivedir = __DIR__."/wbs_zip_hold/wbs_zip_archive/";

//check source processing directory for presence of .zip files
echo "<p>Checking processing directory for unprocessed .Zip files...";
$toprocess = glob($holddir."*.zip");
   if (empty($toprocess)) {  //start empty array check
      echo "<p>No files to process!</p>";
   } else { //else empty array check
      echo "<p>".count($toprocess)." file(s) to process.";
      foreach ($toprocess as $filename) { //start foreach toprocess
         chmod($filename, 0777);
            $zip = new ZipArchive;
         if ($zip->open($filename) === TRUE) { //start file unzip process
            $zip->extractTo($ziptodir);
            $zip->close();
            echo "<p>File (".$filename.") unzipped</p>";
            
            $unzipdir = substr($filename, 0, strlen($filename) - 4); //chops off .zip from right side of string
            echo "<p>Unzip Dir after chopping off .zip: ".$unzipdir."</p>";

            echo "<p>Unzip Dir: ".$unzipdir."<br>
            Hold Dir: ".$holddir."</p>";
            $unzipdir = ltrim($unzipdir, $holddir); //removes old directory from left of string
            echo "<p>Unzip Dir after ltrim: ".$unzipdir."</p>";

            $unzipdir = $ziptodir.$unzipdir;
            echo "<p>Directory to process: ".$unzipdir."</p>";
         } //end file unzip process
      } //end foreach toprocess
   } //end else empty array check


An uncapitalized zip filename results in too many characters being trimmed via the ltrim function, and therefore errors in subsequent processing. However, a capitalized zip filename works OK.

Can anyone explain the logic of this, and tell me if this is a bug?

Any/all feedback would be appreciated! Thanks in advance! (BTW - Labor Day weekend, so I may not see this until I return to work Tuesday. But I *WILL* reply!)

Dan sends...
DanFord2014
New php-forum User
New php-forum User
 
Posts: 2
Joined: Fri Aug 29, 2014 12:51 pm

Re: May Have Discovered a Bug; Requesting Feedback

Postby landi » Fri Aug 29, 2014 2:44 pm

Hi,
<jedi>These are not the functions you are looking for</jedi>

The trim functions are not parsing function. They trim any character in str2 from the beginning and or end of str1. So:
Code: Select all
echo ltrim('a/b/c.zip', 'a/b/c');
will output .zip. So will:
Code: Select all
echo ltrim('a/b/c.zip', 'bca/');
because str2 is just a list of chars, not a regex or anything. The reason it works for a capitalised name is because you don't have the capital letter in the list of chars i.e.:
Code: Select all
echo ltrim('a/b/C.zip', 'a/b/c');
will output C.zip as will:
Code: Select all
echo ltrim('a/b/cccC.zip', 'a/b/c');
but
Code: Select all
echo ltrim('a/b/cccC.zip', 'a/b/c/C');
will output just .zip

Perhaps you wanted basename() or pathinfo() or one of the other PHP filename parsing functions. :)

-Ob1
landi
New php-forum User
New php-forum User
 
Posts: 236
Joined: Thu Mar 15, 2012 3:59 pm

Re: May Have Discovered a Bug; Requesting Feedback

Postby DanFord2014 » Sat Aug 30, 2014 6:30 am

OK, that makes sense!

I absolutely appreciate the help!
DanFord2014
New php-forum User
New php-forum User
 
Posts: 2
Joined: Fri Aug 29, 2014 12:51 pm


Return to PHP General

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.