Hey guys,

I'm trying to make a check to see if a file to be uploaded was added. I have tried the below and a few variations. I imagine I'm having problems because $_FILES is a global variable and is therefore always set. But I still can't figure out what to do instead. Any help would be great.

if (!isset($_FILES['file'])){
			$err = $err + 64;
		}

Dani AI

Generated

Short answer: check the upload error code (and that the temp file is truly an uploaded file) rather than relying on isset($_FILES['file']) or file_exists() on some path. $_FILES/its keys can exist even when no file was chosen; the error value tells you what happened.

As suspected, isset($_FILES['file']) is not reliable for "no file chosen." As @Zagga suggested, file_exists() looks at files already on disk and is not the right test for whether a browser submitted a file.

A practical check looks for UPLOAD_ERR_OK and uses is_uploaded_file() before processing. Example pattern:

if (!empty($_FILES['file']) && isset($_FILES['file']['error'])) {
    if ($_FILES['file']['error'] === UPLOAD_ERR_OK && is_uploaded_file($_FILES['file']['tmp_name'])) {
        // safe to move/process the uploaded file
    } elseif ($_FILES['file']['error'] === UPLOAD_ERR_NO_FILE) {
        // no file was selected by the user
    } else {
        // handle other upload errors (INI size limits, partial upload, etc.)
    }
}

Troubleshooting checklist:

  • Ensure the HTML form uses method="post" and enctype="multipart/form-data" and that the input name matches the key you check.
  • For multiple-file inputs (name="file[]"), inspect the array structure inside $_FILES.
  • Verify PHP settings (file_uploads on, upload_max_filesize, post_max_size) — if post_max_size is too small you may see empty uploads.
  • Always validate file size/type and use move_uploaded_file() to store the file safely.

For details on error codes and best practices, see the PHP manual on upload errors and handling uploaded files: File Upload Errors and is_uploaded_file.

Member Avatar for Member #671080

Hi peck3277,

Try using

if (file_exists($path_to_file)){
   echo "The file exists";
}

obviously setting $path_to_file to the location of the file.


Hope this helps
Zagga

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.