file_exists isn't working. I've looked at a few examples and still no go. Program does not detect the file. The path of my file is /var/www/osbs/PHPAPI/recording.mp3 and the website root is inside osbs. The location of this file is inside PHPAPI that is why I do not put full path in file_put_contents. The program is able to make the original recording.mp3 but not any appended versions of it.
<?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("/var/www/osbs/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("/var/www/osbs/PHPAPI/".$name)){
$new_name = $file.$i;
$name = $new_name.".".$extension;
$i++;
}
return $new_name;
}
?>
Your issue is with the file_put_contents
. You need to specify a full path, and you only specify a file name. Try echo
ing $name
just before using it, you'll see it's not a path, just a filename.
I would recommend you to set a constant at the begining of the file with the path instead of sometimes relying on relative paths and sometimes relying on absolute paths.
<?php
const SAVE_PATH = "/var/www/osbs/";
$actual_name = pathinfo(SAVE_PATH."PHPAPI/recording.mp3",PATHINFO_FILENAME);
$original_name = $actual_name;
$extension = pathinfo(SAVE_PATH."PHPAPI/recording.mp3",PATHINFO_EXTENSION);
if (isset($_GET["RecordingUrl"]) && $_GET["RecordingUrl"]) {
if (file_exists(SAVE_PATH."PHPAPI/".$actual_name.".".$extension)) {
$actual_name = find_new_name($original_name, $extension);
}
else {
$actual_name = $original_name;
}
$name = $actual_name.".".$extension;
file_put_contents(SAVE_PATH.'PHPAPI/'.$name, file_get_contents($_GET["RecordingUrl"]));
}
function find_new_name ( $file, $extension ) {
$name = $file.".".$extension;
$i = 0;
while(file_exists(SAVE_PATH."PHPAPI/".$name)){
$new_name = $file.$i;
$name = $new_name.".".$extension;
$i++;
}
return $new_name;
}
?>
What I changed:
const SAVE_PATH = "/var/www/osbs/";
file_put_contents
(THIS IS THE ACTUAL FIX, YOU NEED A FULL PATH HERE)RecordingUrl
isset, otherwise you get a PHP warning when it's not set.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With