user2548054
user2548054

Reputation:

PHP: Exploding file path into an array is not working properly

I have over 1million file path records saved into a column in a mysql DB. I am trying to make an application to pull down similar files that may have different path and arrange them with PHP. I have the DB all set up and now Im trying to get my PHP to work but come across the following warnings and as well as that its not even creating the array properly as you will notice if you look at the following output and compare it to the file directory, there is even weird paragraph symbols showing up as well as the file name getting jumbled up.

array(4) { 
    [0]=> string(42) "2013-01-17-141325589_2013-01-18-j-CP08.45" 
    [1]=> string(3) "ar¶" 
    [2]=> string(10) "loc¶66hygh" 
    [3]=> string(23) "dfgh  est_file1.jpg.meta" 
}

Here is my code:

$filepath = "2013-01-17-141325589_2013-01-18-j-CP08\45645\5\ar\666\loc\66666hygh\dfgh\test_file1.jpg.meta";

$pieces = explode("\\", $filepath);

var_dump($pieces);
?>

Upvotes: 1

Views: 941

Answers (4)

zekus
zekus

Reputation: 848

Just change the double quotes to single quotes and the string wont be parsed.

Upvotes: 0

fehnomenal
fehnomenal

Reputation: 509

By using double quotes PHP goes into template mode. For example \456 will be treated as an octal value.

Just use single quotes ' to get rid of your problem.

Upvotes: 1

Marc B
Marc B

Reputation: 360762

\xxx where the xxx are digits in the range 0-7 are interpreted as octal numbers. So, for instance the \666 in your code is seen as octal 666 and is treated as hex 0x1B6/decimal 438, which is the char.

Upvotes: 0

IanPudney
IanPudney

Reputation: 6031

The problem with your array generation is that you must escape the backslashes in the $filepath. The following code works as expected:

<?php
$filepath = "2013-01-17-141325589_2013-01-18-j-CP08\\45645\\5\\ar\\666\\loc\\66666hygh\\dfgh\\test_file1.jpg.meta";
$pieces = explode("\\", $filepath);
var_dump($pieces);
?>

When the backslashes aren't escaped, they're acting as escape characters themselves, which causes unexpected behavior in your path.

You may, however, find the pathinfo() function more relevant to what you're doing.

Upvotes: 0

Related Questions