【问题标题】:PHP callback containsPHP回调包含
【发布时间】:2014-06-12 22:33:25
【问题描述】:

我在 PHP 中创建了一个类:

<?php

class _new_model extends model {

/**
 * NEW contruct
 */
public function __construct() {
    parent::__construct();
}

function newuser(){
    $password = trim($_POST['psw']);
    $password2= trim($_POST['psw1']);
    if($password === $password2){
        $hashpsw = hash('sha512', $password);
        $salt = $this->createSalt();
        $psw = hash('sha512', $salt . $hashpsw);
        $query = $this->db->prepare('INSERT INTO users (username, password, salt) VALUES (?,?,?)');
        $query->bindValue(1, $_POST['user'], PDO::PARAM_STR);
        $query->bindValue(2, $psw, PDO::PARAM_STR);
        $query->bindValue(3, $salt, PDO::PARAM_STR);
        try{
            if($query->execute()){
                echo 'ჩაიწერა!';
            }else{
                echo 'მოხდა შეცდომა!';
            }
        }catch(PDOException $e){
            die($e->getMessage());
        }
    }else{
        echo 'პაროლები ერთმანეთს არ ემთხვევა!';
    }
}

/**
 * @return string
 */
function createSalt(){
    $text = md5(uniqid(rand(), TRUE));
    $salt = substr($text, 0, 32);
    return $salt;
}
function newlanguage(){
    $lang = trim($_POST('language'));
    $checklang = $this->checklang($lang);
    if($checklang == true){
        echo 'ეს ენა უკვე არსებობს';
    }else{
        if(is_uploaded_file($_FILES['flag']['tmp_name'])){
            $maxsize=100000;
            $size=$_FILES['flag']['size'];
            $imgdetails = getimagesize($_FILES['flag']['tmp_name']);
            $mime_type = $imgdetails['mime'];
            // checking for valid image type
            if(($mime_type=='image/jpeg')||($mime_type=='image/gif')||($mime_type=='image/bmp')||($mime_type=='image/png')){
                // checking for size again
                if($size<$maxsize){
                    $filename=$_FILES['flag']['name'];
                    $imgData =addslashes (file_get_contents($_FILES['flag']['tmp_name']));
                    $query = $this->db->prepare('INSERT INTO lang (name,mokled,flag,flagname,flagtype,flagsize) VALUES (?,?,?,?,?,?)');
                    $query->bindValue(1, $lang, PDO::PARAM_STR);
                    $query->bindValue(2, trim($_POST('shortlang')), PDO::PARAM_STR);
                    $query->bindValue(3, $imgData, PDO::PARAM_LOB);
                    $query->bindValue(4, $filename, PDO::PARAM_STR);
                    $query->bindValue(5, $mime_type, PDO::PARAM_STR);
                    $query->bindValue(6, addslashes($imgdetails[3]), PDO::PARAM_STR);
                    try{
                        if($query->execute()){
                            echo 'yes';
                        }else{
                            echo 'no query-ის გაშვებისას';
                        }
                    }catch (PDOException $e){
                        die($e->getMessage());
                    }
                }else{
                    echo "<span class='error'>Image to be uploaded is too large..Error uploading the image!!</span>";
                }
            }else{
                echo "<span class='error'>Not a valid image file! Please upload jpeg or gif image.</span>";
            }
        }else{
            switch($_FILES['flag']['error']){
                case 0: //no error; possible file attack!
                    echo "<span class='error'>There was a problem with your upload.</span>";
                    break;
                case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
                    echo "<span class='error'>The file you are trying to upload is too big.</span>";
                    break;
                case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
                    echo "<span class='error'>The file you are trying to upload is too big.</span>";
                    break;
                case 3: //uploaded file was only partially uploaded
                    echo "<span class='error'>The file you are trying upload was only partially uploaded.</span>";
                    break;
                case 4: //no file was uploaded
                    echo "<span class='error'>You must select an image for upload.</span>";
                    break;
                default: //a default error, just in case!
                    echo "<span class='error'>There was a problem with your upload.</span>";
                    break;
            }
        }
    }
}

/**
 * @param $name
 * @return bool
 */
function checklang($name){
    $query = $this->db->prepare('SELECT id FROM lang WHERE name = ?');
    $query->bindValue(1, $name, PDO::PARAM_STR);
    try{
        $query->execute();
        if($query->rowCount() > 0){
            return true;
        }else{
            return false;
        }
    }catch (PDOException $e){
        die($e->getMessage());
    }
}}

并且有这个 HTML 表单:

<form enctype="multipart/form-data" action="<?php echo Domain; ?>_new/newlanguage/" name="newlanguage" method="post" id="newlanguage" >
<input type="text" name="language" id="language" placeholder="New Language">
<input type="text" name="shortlang" id="shortlang" placeholder="short name"><br>
<input type="file" name="flag" id="flag" placeholder="Flag"><br>
<input type="submit" name="submit" value="submit"></form>

提交表单时,我收到此错误:

数组回调必须在第 49 行的 _new_model.php 中包含索引 0 和 1 在这一行上写着 返回 $salt;

这是一个名为 create salt 的函数。我不明白当我调用 newlanguage 函数时,为什么它会扫描 othere 函数?

请帮忙

【问题讨论】:

标签: php mysql arrays callback


【解决方案1】:

$_POST('language') 应该是$_POST['language']

【讨论】:

  • 是的。这是一个机械错误。但现在我有另一个错误。未定义索引:第 54 行和第 88 行上的标志。在该行中写入以下行: if(is_uploaded_file($_FILES['flag']['tmp_name'])){ 和 switch($_FILES['flag']['error ']){ 请帮忙
  • 这意味着他们没有在flag 输入中上传文件。
  • 你需要测试if(isset($_FILES['flag']))来测试他们是否上传了任何东西。
  • 我把这段代码连线 if(isset($_FILES['flag'])){ echo 'yes'; }else{ 回声“不”; } 它对我说不
  • 当我m writting only this code: if(is_uploaded_file($_FILES['flag']['tmp_name'])){ echo 'yes'; }else{ echo 'no'; } Im 遇到同样的错误时
猜你喜欢
  • 1970-01-01
  • 2013-11-13
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 2016-04-06
  • 2013-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多