Return to Snippet

Revision: 70641
at May 18, 2016 21:53 by aravindnair93


Updated Code
if (!is_dir("directory")) {
        mkdir("directory");
    }

    $max_size = 10000;          // maximum file size, in KiloBytes
    $allowtype = array('pdf');        // allowed extensions

    if (isset($_FILES['fileToUpload']) && strlen($_FILES['fileToUpload']['name']) > 1) {

        $file = $ref_id;         //Name of file when uploaded

        $sepext = explode('.', strtolower($_FILES['fileToUpload']['name']));
        $type = end($sepext);       // gets extension
        // Checks if the file has allowed type and size
        if (!in_array($type, $allowtype)) {
            $_SESSION['status'] = 1;
            echo '<script> window.location.href = "redirect.php"; </script>';
        }
        if ($_FILES['fileToUpload']['size'] > $max_size * 1000) {
            $_SESSION['status'] = 2;
            echo '<script> window.location.href="redirect.php"; </script>';
        }
        $uploadpath = 'directory/' . $file;     // gets the file name
        if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadpath)) {
            
        } else {
            $_SESSION['status'] = 3;
            echo '<script> window.location.href = "redirect.php"; </script>';
        }
    }

Revision: 70640
at May 18, 2016 21:47 by aravindnair93


Initial Code
if (!is_dir("directory")) {
        mkdir("directory");
    }

    $max_size = 10000;          // maximum file size, in KiloBytes
    $allowtype = array('pdf');        // allowed extensions

    if (isset($_FILES['fileToUpload']) && strlen($_FILES['fileToUpload']['name']) > 1) {

        $file = $ref_id;         //Name of file when uploaded

        $sepext = explode('.', strtolower($_FILES['fileToUpload']['name']));
        $type = end($sepext);       // gets extension
        // Checks if the file has allowed type and size
        if (!in_array($type, $allowtype)) {
            $_SESSION['status'] = 1;
            echo '<script> window.location.href = "redirect.php"; </script>';
        }
        if ($_FILES['fileToUpload']['size'] > $max_size * 1000) {
            $_SESSION['status'] = 2;
            echo '<script> window.location.href="redirect.php"; </script>';
        }
        $uploadpath = 'directory/' . $file;     // gets the file name
        // If no errors, upload the image, else, output the errors
        if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadpath)) {
            
        } else {
            $_SESSION['status'] = 3;
            echo '<script> window.location.href = "redirect.php"; </script>';
        }
    }

Initial URL

                                

Initial Description
General file upload script in php. Error set using SESSIONS.

Initial Title
Php File Upload Script

Initial Tags
php, file

Initial Language
PHP