PHP check for errors in input, if no errors execute and upload

前端 未结 3 1763
眼角桃花
眼角桃花 2021-01-29 15:17

I\'m currently working on a registration system and ran into some problem.
I\'ll start with pasting a simplified version of the code before:

session_start(         


        
3条回答
  •  暖寄归人
    2021-01-29 16:00

    // error = 0  means no error found you can continue to upload...
    if ($_FILES['file']['error'] == 0) {
    
    }
    

    Here are all of the errors explained: http://php.net/manual/en/features.file-upload.errors.php

    UPLOAD_ERR_OK Value: 0; There is no error, the file uploaded with success.

    UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

    UPLOAD_ERR_FORM_SIZE Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.

    UPLOAD_ERR_PARTIAL Value: 3; The uploaded file was only partially uploaded.

    UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded.

    UPLOAD_ERR_NO_TMP_DIR Value: 6; Missing a temporary folder. Introduced in PHP 5.0.3.

    UPLOAD_ERR_CANT_WRITE Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.

    UPLOAD_ERR_EXTENSION Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0.

    To validate input fields

    if(empty($_POST['name'])&&empty($_POST['password'])){
     //fields empty show error here
    }else if (is_numeric($username[0])){
        echo 'First character must be a letter';
    }
    else if (!preg_match('/^[a-zA-Z0-9]+$/', $username)) {
         echo 'Only letters and numbers are allowed';
    }else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
         echo 'Invalid email address.'; 
    }else if(!preg_match("/^[\pL\s,.'-]+$/u", $name)) {
         echo 'Invalid name.'; 
    }
    

提交回复
热议问题