-1

I've seen some of the answers here like this one, but due to my reputation, I can't comment.

The problem is somehow the results show me the wrong one. It all outputs the file exists! even though it does not really exists.

function doesFileExist (urlToFile) {
  const xhr = new XMLHttpRequest()
  xhr.open('HEAD', urlToFile, false)
  xhr.send()

  return console.log(xhr.status == 200 ? 'File exists' : 'File does not exist')
}

It outputs:

File Exists

Karma Blackshaw
  • 880
  • 8
  • 20

3 Answers3

2
function doesFileExist(urlToFile)
{
    var xhr = new XMLHttpRequest();
    xhr.open('HEAD', urlToFile, false);
    xhr.send();
    if (xhr.readyState == 4 && xhr.status == 404 ) {
        console.log("File doesn't exist");
        return false;
    } else {
        console.log("File exists");
        return true;
    }
}

doesFileExist('/Framework/views/login/activate_studeasdfnt.php')

Try that. I have tested it. Sorry for taking too long. Also if your function is to test if file exists i would recommend checking for a 200 response. But its up to you.

Mohammad C
  • 1,321
  • 1
  • 8
  • 12
  • 1
    Hello sir ! Sorry i haven't thanked you before, it's due to my low reputation. But now, I express my gratitude that I made it out of that hole. Thank you! – Karma Blackshaw Jan 17 '20 at 07:06
-1

try checking if it's greater than 400

function doesFileExist(urlToFile)
{
    var xhr = new XMLHttpRequest();
    xhr.open('HEAD', urlToFile, false);
    xhr.send();
    if (xhr.status >=400 ) {
        console.log("File doesn't exist");
        return false;
    } else {
        console.log("File exists");
        return true;
    }
}

doesFileExist('/Framework/views/login/activate_studeasdfnt.php')
PHP_Developer
  • 386
  • 3
  • 11
-1
$('#activate_user_level').on('change', function(){
    var user_level = $(this).val(),
        content = $('#content'),
        url = '/views/login/activate_'+ user_level +'.php';

    $.ajax({
        url : controllers('ActivatesController'),
        method : 'POST',
        data : {change_modal_content_activate : 1, url : url},
        success : function(e){
            console.log(e)
        }
    })
})

server

if(isset($_POST['change_modal_content_activate'])){
    $url = dirname(__DIR__) . $init->post('url');
    var_dump(file_exists($url));
}
Karma Blackshaw
  • 880
  • 8
  • 20
  • 1
    Security tip 1: don't pass the full file name, you're disclosing too much info about how your system is built. Just pass the `user_level` string and then build the file name on the server side. – Alex Howansky Oct 18 '18 at 16:20
  • Security tip 2: Don't trust the value passed in for `user_level`. What if somebody passes in a value like `../../../../etc/foo`? Instead of building a file name with the string, check that it exists in a hard-coded array, or build a class name and use `class_exists()`. – Alex Howansky Oct 18 '18 at 16:22
  • 1
    hey @Fear, fixed your original post. – Mohammad C Oct 18 '18 at 16:41
  • what do you mean sir @noyanc – Karma Blackshaw Oct 18 '18 at 17:37
  • check my answer. I have provided a fix to your problem with the code you provided – Mohammad C Oct 18 '18 at 17:39