Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_exists() not working in php5 inside while loop

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;
}
 ?>
like image 789
fixnode Avatar asked Jun 08 '17 00:06

fixnode


1 Answers

Your issue is with the file_put_contents. You need to specify a full path, and you only specify a file name. Try echoing $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:

  1. Defined a const SAVE_PATH = "/var/www/osbs/";
  2. Use the new constant everywhere. No more relative sometimes and absolute sometimes, it's all absolute.
  3. Used the constant in file_put_contents (THIS IS THE ACTUAL FIX, YOU NEED A FULL PATH HERE)
  4. Added an additional check to make sure RecordingUrl isset, otherwise you get a PHP warning when it's not set.
like image 85
Growiel Avatar answered Sep 21 '22 17:09

Growiel