0

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.

flash
  • 1,455
  • 11
  • 61
  • 132
  • For debugging purpose: **1** Move `echo "Hello World";` into the first case, then add a `default` case in which you'll just print out the argument value. This would give you an idea of what you're switching. **2** Try returning just the extension, since that's what you're interested in for now => `$parts = pathinfo( $f, PATHINFO_EXTENSION )`. This will return a string. Print the string, then move on. – maswerdna Jun 10 '19 at 07:46

1 Answers1

-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;
}
Jim.B
  • 426
  • 6
  • 14
  • I have tried inside as well. It’s returning the same error – flash Jun 10 '19 at 03:06
  • Try to enable php error reporting, paste this at the very start of your script. `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);` Then run the script again and paste the error message here. – Jim.B Jun 10 '19 at 03:13