How to use pathinfo in php?

2019-08-21 05:58发布

问题:

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:

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;
}