The pathauto function doesn't return the filename field for php versions older than 5.2. This breaks the filefield-onlyname token replacement.

I have attached the patch I am using for this.

Comments

Status:Active» Needs work

Sounds like this may be a problem in pathauto or the token replacement functions. Rather than adding a work-around to FileField's token handling, we should just make sure that the correct tokens get passed in to begin with.

I don't think so. In the php docs (http://php.net/manual/en/function.pathinfo.php) it says the return value for pathinfo is:

The following associative array elements are returned: dirname, basename, extension (if any), and filename.

In the changelog:

Version Description
5.2.0 The PATHINFO_FILENAME constant was added.

In the source for filefield.token.inc, line 60:

$info = pathinfo($item['filename']);
$tokens['filefield-onlyname'] = $info['filename'];
$tokens['filefield-extension'] = $info['extension'];

For PHP version older than 5.2, the $info['filename'] will not be set, as per PHP docs. Hence, the patch. I found that this is the similar approach that has been used in other modules due to this problem. E.g. such as filefield paths (see #515044: Add support for the [filefield-onlyname-original] token in PHP 5.1 (and lower)), although I am not using that module myself.

Whoops. It appears that in my original posting, I mistakenly said "pathauto" rather than "pathinfo" thus the confusion. The problem lies within the php built-in function pathinfo, not the pathauto module.

Ah, okay that makes much more sense. pathinfo() definitely did change and I've seen this problem elsewhere. Rather than putting an IF statement in there though, let's just use basename($info['filepath']) instead when we set filename and do the same thing for all versions of PHP.

Sure, a solution independent of PHP version would be great. But with basename, you'd get the filename+extension, not just the filename, which is what the onlyname token was. So wouldn't you still have to do a substr to extract filename, or am I missing something?

Ah, yes you have to use basename($info['filepath'], '.' . $info['extension']).

StatusFileSize
new175 bytes

Cool. For completeness I have attached a new patch.

Status:Needs work» Needs review
StatusFileSize
new921 bytes

Here's a patch that produces the same filefield-onlyname token regardless of php version - and also fixes a PHP Notice: Undefined Variable issue using php 5.1.6 and the current version of filefield.

The code change is:

      $tokens['filefield-onlyname'] = $info['filename'];

to

      if (isset($info['filename'])) {
        $tokens['filefield-onlyname'] = $info['filename'];
      }
      else {
        $tokens['filefield-onlyname'] = drupal_substr($info['basename'], 0, drupal_strlen($info['basename']) - drupal_strlen($info['extension']) - 1);
      }

StatusFileSize
new787 bytes

Doh! Now reading the issue queue fully (when will I learn?) seems like quicksketch wants to do the same regardless of php version... patch attached to do just that.

A faster way (or at least shorter) is:

$info['filename'] = basename($info['basename'], '.' . $info['extension);

StatusFileSize
new730 bytes

Well here's a new patch based on quicksketch's more elegant line of code that bears a remarkable resemblance to #7 :) - I've tested this on php 5.1.6 and it works great.

Status:Needs review» Fixed

Finally committed. Thanks alexpott.

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.