PHP:将文件路径扩展到数组中无法正常工作
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);
?>
我有超过1百万条文件路径记录保存到mysql DB的一列中。 我正在尝试创建一个应用程序来下载可能具有不同路径的类似文件并使用PHP进行排列。 我有数据库所有设置,现在我试图让我的PHP工作,但遇到以下警告,以及它甚至没有正确创建数组,因为你会注意到你看到以下输出并将其与 文件目录,甚至出现了奇怪的段落符号以及文件名混乱。 p>
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 \ test_file1.jpg.meta”
}
code> pre>
这是我的代码:
p>
$ 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);
?>
code> pre>
div>
Just change the double quotes to single quotes and the string wont be parsed.
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.
\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.
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.