-1

I send files via fetch and uploading them in PHP with move_uploaded_file() method.The problem is when i use move_uploaded_file() method page automatically refresh and even when i upload multiple files just first one uploading and refresh page again.

PHP Codes

    if($img['error'] == 0 && $img['size'] > 0){
        if (!is_dir('naber')) {
            mkdir('naber', 0777, true);
        }
        
        $allowedExts = array("gif", "jpeg", "jpg", "png");
        // $extension = explode(".", $_FILES["image"]["name"]);
        // $extension = end($extension);
        $extension = pathinfo($img['name'])['extension'];
        if(!(in_array($extension, $allowedExts))){
            $result['status'] = false;
            $result['errmessage'] = 'Image type is invalid';
            exit(json_encode($result));
        }
        $target_path = 'naber/';
        $filename = time().'.'. strtolower($extension);
        $full_path = $target_path."".$filename;
        if(move_uploaded_file($img['tmp_name'], $full_path)){
            $result['status'] = true;
            $result['message'] = 'Image added successfully!';
        }else{
            $result['status'] = false;
            $result['errmessage'] = 'Image type is invalid';
            exit(json_encode($result));
        }
    }
    exit(json_encode($result));

Javascript codes :

if(document.querySelector('#submit')) document.querySelector('#submit').addEventListener('click', (e) =>{
    e.preventDefault();
    ...
    fd = new FormData();
    Array.from(document.querySelectorAll('.test')).some((t) => {
        .......
                tests['test'+i]['img'] = t.querySelector('.input-ifile').files[0];
                fd.append('test'+i+'-img',tests['test'+i]['img']);
        .......
    fetch('handlers/handler.php', { method: 'POST' , body: fd})
    .then(function (response) {
        return response.text();
    })
    .then(function (body) {
        console.log(body)
        return false;
    });
})

Desired outcome is for the file to be uploaded to the server, and index.php not be refreshed.

novruzrhmv
  • 639
  • 5
  • 13
  • Check the browser console for JavaScript errors (set it to keep data beyond a page reload first), check how the server responded in the network panel. – CBroe Mar 17 '21 at 08:04
  • There is no error or something else. PHP script working well so file uploading without any problem but page refresh itself. – novruzrhmv Mar 17 '21 at 08:06
  • So what was the response from the server then? – CBroe Mar 17 '21 at 08:08
  • Actually nothing, i use fetch and before the refreshing i catched respond in the network panel and respond was empty. Refreshing happening because of move_uploaded_file() method but i don't why. – novruzrhmv Mar 17 '21 at 08:11
  • it appears you issue a `fetch` request in each iteration of the `.some` loop but only initialise a single `FormData` object outside that loop – Professor Abronsius Mar 17 '21 at 08:12
  • Your PHP script does not appear to give a sensible response, in case the condition of that first `if` was not fulfilled. You respond with `exit(json_encode($result));` – but did you ever fill the `$result` variable with anything to begin with? – CBroe Mar 17 '21 at 08:15
  • Now i'm getting file uploaded message but it still refresh page and because of it i can't upload multiple files same time in loop. – novruzrhmv Mar 17 '21 at 14:09

1 Answers1

0

Solution : After spending hours to find problem so figured out that problem is not in the codes. It was happening because of Visual Studio Live Server extension. For prevent auto reloading just closeing live server is enough.

novruzrhmv
  • 639
  • 5
  • 13