-1

I created a script that gets a MP3 file from a GET statement. I need a way to make sure that if the original name "recording.mp3" does not exist then create it from GET contents, if it does exist then append a number to the file name. What am I doing wrong? the part that does not work thus far is appending the number but getting the file and saving works great. Remove the while loop and program works!

<?php 
$actual_name = pathinfo("PHPAPI/recording.mp3",PATHINFO_FILENAME);
$original_name = $actual_name;
$extension = pathinfo("PHPAPI/recording.mp3",PATHINFO_EXTENSION);

$i = 0;
if ($_GET["RecordingUrl"]){
    while(file_exists("PHPAPI/".$actual_name.".".$extension)){
        $actual_name = $original_name.$i;
        $name = $actual_name.".".$extension;
        $i++;
    }
    file_put_contents($name, file_get_contents($_GET["RecordingUrl"]));
}
elseif ($_GET["RecordingUrl"]){
    file_put_contents($original_name.".".$extension,
    file_get_contents($_GET["RecordingUrl"]));
}
?>
RST
  • 3,769
  • 2
  • 18
  • 32
fixnode
  • 59
  • 3
  • 7
  • 30
  • Can you explain how it doesn't work? What is it not doing? – Sam Jun 07 '17 at 16:27
  • If the file doesn't exist in the first time, it will not enter the while loop, not setting a value for `$name`, ~and should be~ causing _undefined_ warning. Set a value for `$name` before the while. – FirstOne Jun 07 '17 at 16:34
  • @Sam the program isn't even creating the original file if it doesn't exist let alone an appended version. – fixnode Jun 07 '17 at 16:34
  • @FirstOne $actual_name is the original file before the while loop, $name is the appended file. I cannot check $name outside the while loop as it does not exist – fixnode Jun 07 '17 at 16:34
  • You are using the same condition in the if and elseif, this is not going to work – RST Jun 07 '17 at 16:36
  • @FirstOne can you give me an example how to fix this? I tried everything including doing if ($_GET["RecordingUrl"] && file_exists("PHPAPI/".$actual_name.".".$extension)) also there is not undefined error as you say there is – fixnode Jun 07 '17 at 17:17
  • @fixnode maybe they are disabled. [Enable them](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). Try adding `$name = $actual_name.".".$extension;` before the while. BTW, you check if file exists in `PHPAPI/`, but don't upload the file to it. You probably want to have that in the `file_get_contents` too. – FirstOne Jun 07 '17 at 17:22
  • @FirstOne it doesn't work. Keeps overriding the original and doesn't create a new file – fixnode Jun 07 '17 at 19:52

1 Answers1

0

To make your code more organized, use a function

<?php 
$actual_name = pathinfo("PHPAPI/recording.mp3",PATHINFO_FILENAME);
$original_name = $actual_name;
$extension = pathinfo("PHPAPI/recording.mp3",PATHINFO_EXTENSION);

if ($_GET["RecordingUrl"]) {
     if (file_exists("PHPAPI/".$actual_name.".".$extension)) {
        $actual_name = find_new_name($original_name, $extension);
     } else {
        $actual_name = $original_name;
     }
     $name = $actual_name.".".$extension;
     file_put_contents($name, file_get_contents($_GET["RecordingUrl"]));
}

function find_new_name ( $file, $extension ) {
    $name = $file.".".$extension;
    $i = 0;
    while(file_exists("PHPAPI/".$name)){
        $new_name = $file.$i;
        $name = $new_name.".".$extension;
        $i++;
    }
    return $new_name;
}
 ?>
RST
  • 3,769
  • 2
  • 18
  • 32
  • does not create additional files only initial file – fixnode Jun 07 '17 at 18:07
  • it overrides the original file name every new file created – fixnode Jun 07 '17 at 19:02
  • adjusted function arguments, If it doesn't work put some echo commands in the if and else part to see which one is used – RST Jun 07 '17 at 23:29
  • the problem is that the file is coming from a webhook. I don't get a response from it so echo or print_r is out of the question for trial and error. Any other suggestions? – fixnode Jun 08 '17 at 01:04
  • 1
    the thing that needs to be tested is whether the `file_exists` doesn't work, or the generation of the new filename doesn't work. Writing code without being able to debug it is like cutting meat without a knife. See if you can write it to a log – RST Jun 08 '17 at 07:59