0

why does it not work? i want to check whether the file I want to upload already exists.

<?php
if (isset($_POST['submit'])) {
    $filename = $_POST['name'];
    if(!file_exists('fileuploadsfolder/'.$filename)) {
        move_uploaded_file($_FILES['file']['tmp_name'],'fileuploadsfolder/'.$_FILES['file']['name']);
        header("Location:../");
    }else{
        echo "File already exists";
    }
}
?>

here is an excerpt from my index file.

<form action="fileupload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="name">
<button type="submit" name="submit">Upload</button>
</form>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Jonas
  • 11
  • There would be a missing `}` to terminate the outer IF – RiggsFolly Oct 31 '21 at 18:46
  • 1
    There doesn't appear to be a text input with the name `name`, what value are you expecting to see in `$_POST['name']`? – TheGentleman Oct 31 '21 at 18:47
  • The real cause is that on your form `name` is name of file input. And there's no input with `name=file`. – u_mulder Oct 31 '21 at 18:48
  • Just goes to show Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Oct 31 '21 at 18:48
  • `$filename = $_POST['name'];` I think you shoudl be looking at `$_FILES['file']['name']` there – RiggsFolly Oct 31 '21 at 18:49
  • it works "RiggsFollly" made it. :D thank you every much, happy halloween. – Jonas Oct 31 '21 at 18:59

0 Answers0