File Upload Form

 
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>File Upload Form</title>
</head>
<body>
    <h4><u>Select File To Upload:</u></h4>
    <form action="" method="post" enctype='multipart/form-data' accept-charset="UTF-8">
        <p><input type="file" name="test_file"></p>
        <p><input type="submit" value="Upload File"></p>
    </form>

    <h4><u>PHP Settings that affect uploading:</u></h4>
    <?php
    echo '<pre>';
    echo '<div>max_execution_time ' . "\t" . ini_get("max_execution_time") . '</div>';
    echo '<div>max_file_uploads ' . "\t" . ini_get("max_file_uploads") . '</div>';
    echo '<div>max_input_time ' . "\t\t" . ini_get("max_input_time") . '</div>';
    echo '<div>max_input_vars ' . "\t\t" . ini_get("max_input_vars") . '</div>';
    echo '<div>memory_limit ' . "\t\t" . ini_get("memory_limit") . '</div>';
    echo '<div>post_max_size ' . "\t\t" . ini_get("post_max_size") . '</div>';
    echo '<div>upload_max_filesize ' . "\t" . ini_get("upload_max_filesize") . '</div>';
    echo '<div>upload_tmp_dir ' . "\t\t" . ini_get("upload_tmp_dir") . '</div>';
    echo '</pre>';

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if (!empty($_FILES)) {
            if (!empty($_FILES['test_file']['tmp_name'])) {
                echo '<h4><u>Upload Result:</u></h4>';

                echo '<h4>$_FILES Array:</h4>';
                echo '<pre>';
                print_r($_FILES);
                echo '</pre>';

                if (!$_FILES['test_file']['error']) {
                    $uploads_dir = '/my-uploads-folder'; // <-- Enter folder where file will be uploaded to

                    $tmp_name = $_FILES['test_file']['tmp_name'];
                    $name = $_FILES['test_file']['name'];
                    if (move_uploaded_file($tmp_name, "$uploads_dir/$name")) {
                        echo '<div>File Successfully Uploaded!</div>';
                    } else {
                        echo '<div>Error Moving File.</div>';
                    }
                } else {
                    echo '<div>Error Uploading File.</div>';
                }
            } else {
                echo '<div>No File was selected.</div>';
            }
        } else {
            echo '<div>No File Uploaded. Please verify the File\'s size does not exceed <strong>post_max_size</strong>.</div>';
        }
    }
    ?>
</body>
</html>

 

Related Snippets

•  Export/Download data as a csv file
•  Export data to a csv file