i have the following php. the script gives me an error telling me that i have not selected a file to upload even when i have done so. if i try to access the $_FILES['minuteFile']['tmp_name'] i don't get anything but the $_FILES['minuteFile']['name'] would be having some. what could be the cause of the $_FILES['minuteFile']['tmp_name'] not being accessible.

<?php
    require_once 'includes/core.php';

    if ( isset( $_POST['submitted'] ) ) { // form submitted
        $errors = array(); // initialize $errors to an empty array
        $minute = new Minute(); // create a new instance of the Minute class

        // validation for the minute file
        if ( is_uploaded_file( $_FILES['minuteFile']['tmp_name'] ) ) { // a file has been selected for uploading
            if ( ( $_FILES['minuteFile']['type'] == 'application/pdf' ) ) { // file is a .pdf file
                $name = $_FILES['minuteFIle']['name'];
                if ( file_exists( ARCHIVE . DS . $name ) ) { // file already exists in archives folder
                    $errors[] = 'Upload a different file. File already exists on server.';
                    unlink( $_FILES['minuteFile']['tmp_name'] ); // delete file from temporary location
                } elseif ( !file_exists( ARCHIVE . DS . $name ) ) { // file doesn't exist in archives folder
                    // move the selected file from the temporary folder to the archives folder
                    $moveResult = move_uploaded_file( $_FILES['minuteFile']['tmp_name'], ARCHIVE . DS . $name );
                    if ( $moveResult ) { // move operation was successful
                        $minute = $name;
                    } elseif ( !$moveResult ) { // move operation not successful
                        unlink( $_FILES['minuteFile']['type'] ); // delete file from temporary location
                    }
                }
            }
        } else { // not file selected for upload
            $errors[] = 'Please select a file to upload.';
        }
    }
?>
<!DOCTYPE html>
    <head>
        <title>Harare City Council: Add New Minutes</title>
        <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" />
    </head>
    <body>
        <div align="center">
            <div id="content">
                <h2>New Minutes Details</h2>
                <?php
                    if ( isset( $errors ) && is_array( $errors ) ) {
                        echo '<ul>';
                        foreach( $errors as $error ) {
                            echo '<li class="error">' . $error . '</li>';
                        }
                        echo '</ul>';
                    }
                ?>
                <form action="" method="post" enctype="multipart/form-data">
                    <table cellpadding="3" cellspacing="3">
                        <tr>
                            <td class="datalabel">Title:</td>
                            <td><input type="text" name="txtMinutesTitle" size="30" maxlength="60" value="<?php if ( isset( $_POST['txtMinutesTitle'] ) ) echo $_POST['txtMinutesTitle']; ?>" /></td>
                        </tr>
                        <tr>
                            <td class="datalabel">File(.pdf):</td>
                            <td><input type="file" name="minuteFile" /></td>
                        </tr>
                        <tr>
                            <td colspan="2" align="right">
                                <input type="submit" name="submitted" value="Save" />&nbsp;&nbsp;
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
        </div>
    </body>
</html>

Dani AI

Generated

Good troubleshooting by and — the print_r output showing an empty tmp_name and error set to 1 is the key. Error code 1 is UPLOAD_ERR_INI_SIZE: PHP rejects the upload because it exceeds upload_max_filesize, so no temporary file is created and is_uploaded_file() returns false. That explains the intermittent behavior (small files succeed, large ones fail).

Action checklist to fix the server side

  • Increase upload_max_filesize (and post_max_size) in php.ini (ensure post_max_size >= upload_max_filesize), then restart PHP/your webserver.
  • Verify file_uploads = On and check upload_tmp_dir exists and is writable.
  • If using Nginx, adjust client_max_body_size; if behind proxies or mod_security, check their size limits too.
  • Use phpinfo() or php -i to confirm current values while testing.

Code and logic fixes in the script

  • Fix the typo $_FILES['minuteFIle']$_FILES['minuteFile'] (array keys are exact).
  • Never call unlink($_FILES['minuteFile']['type']) — that passes a MIME string to unlink. Only unlink a real filepath (and only when appropriate).
  • Check $_FILES['...']['error'] before using tmp_name. Relying on $_FILES['...']['type'] alone for validation is unsafe; use finfo_file() or check the PDF magic bytes.

Example pattern to validate and move safely:

if ($_FILES['minuteFile']['error'] !== UPLOAD_ERR_OK) {
    error_log('Upload error: ' . $_FILES['minuteFile']['error']);
} else {
    $tmp = $_FILES['minuteFile']['tmp_name'];
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $tmp);
    finfo_close($finfo);
    if ($mime === 'application/pdf') {
        $name = basename($_FILES['minuteFile']['name']);
        move_uploaded_file($tmp, ARCHIVE . DS . $name);
    }
}

Quick test workflow: upload a very small PDF, print_r($_FILES), confirm error is 0 and tmp_name is non-empty, then tweak server limits if large files fail.

Recommended Answers

All 6 Replies

Member Avatar for Member #120589

I'd suggest that you add a few echoes in key places to trace the path your code takes.

i added the followig code on line 7

echo $_FILES['minuteFile']['tmp_name'] . " " . $_FILES['minuteFile']['name'] . " " . $_FILES['minuteFile']['type'];

it's still producing inconsistent results. at times the temporary location is displayed but not always

Member Avatar for Member #120589

Try print_r($_FILES) on line7

At other places just echo "echo1" etc to show which branch of the conditionals you're following

Array ( [minuteFile] => Array ( [name] => LadiesFashion_2.pdf [type] => [tmp_name] => [error] => 1 [size] => 0 ) ) 1

that's the output i got but i had selected a file

problem solved. thanks

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.