跳过文件名中的非ASCII字符

时间:2015-05-20 14:48:19

标签: php apache youtube-dl

程序youtube-dl本身支持文件名中的非ASCII字符,它在root用户和www-data用户下的网络服务器上完美运行,但是当我尝试使用youtube-dl使用PHP下载视频时,完全跳过非ASCII字符。

例如:Stromae - bâtardStromae - btard.mp4البث الحي保存为.mp4

我正在使用此代码运行CLI命令

function cmd($string) {
  $descriptorspec = array(
     0 => array("pipe", "r"),  // stdin
     1 => array("pipe", "w"),  // stdout
     2 => array("pipe", "w"),  // stderr
  );
  $process = proc_open($string, $descriptorspec, $pipes);
  $stdout = stream_get_contents($pipes[1]);
  fclose($pipes[1]);
  $stderr = stream_get_contents($pipes[2]);
  fclose($pipes[2]);
  $ret = proc_close($process);
  return $stdout;
  }
$value = ('youtube-dl https://some.valid/link');
echo cmd($value);

请告知我应该采取哪些措施来解决这个问题。

2 个答案:

答案 0 :(得分:2)

检查你的phpinfo(); LC_ALL或LC_LANG设置的输出。我怀疑它与PHP无关,但与您使用的shell环境相比,而不是Web服务器正在使用的shell环境。

$value = ('LC_ALL=en_US.UTF-8 youtube-dl https://some.valid/link');
echo cmd($value);

答案 1 :(得分:1)

默认情况下,PHP使用ISO-8859-1 charset。配置PHP以使用UTF-8。您可以通过添加

来实现此目的
mb_internal_encoding("UTF-8");

在剧本开始时

相关问题