How to use pathinfo in php?

a 夏天 提交于 2019-12-25 01:24:54

问题


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


来源:https://stackoverflow.com/questions/56519616/how-to-use-pathinfo-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!