How to use pathinfo in php?

2019-08-21 05:28发布

I am working on a php code as shown below on which Line#A prints the following array (shown below php code). My code doesn't seems to go inside switch statement. I am not sure why.

I added print_r($parts) at Line A in order to print the value of $parts.

php code:

<?php
    if (!empty($_POST['id']))
    {
    for($i=0; $i <count($mp4_files); $i++) {
    if($i == $_POST['id']) {
    $f = $mp4_files[$i];
    $parts = pathinfo($f);
    print_r($parts);                    // Line A
    switch ($parts['extension'])
    {
    echo "Hello World";                 // Line B
    case 'mp4' :
    $filePath = $src_dir . DS . $f;
    system('ffmpeg -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination_dir . DS . $parts['filename'] . '.mp3', $result);
    }
    }
    }
    }
?>

Output (Line#A):

Array 
(
    [dirname]  => .
    [basename] => hello.mp4
    [extension] => mp4
    [filename] => hello
)

I have used echo "Hello World" at Line B but for some reasons, its not getting printed and throwing 500 internal server error on console.

Problem Statement:

I am wondering what changes I should make in the php code so that it goes inside switch statement.

1条回答
小情绪 Triste *
2楼-- · 2019-08-21 06:02

The error 500 is caused by echo "Hello World" at Line B which is outside of case mp4.

Your switch statement should be like this.

switch ($parts['extension']) {
    case 'mp4':
        echo "hello world";
        $filePath = $src_dir . DS . $f;
        system('ffmpeg -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination_dir . DS . $parts['filename'] . '.mp3', $result);
        break;
}
查看更多
登录 后发表回答