图片上传无法在表单上运行

时间:2016-05-16 09:45:07

标签: php class resize

所以我有一个表单,用于注册用户。然后我有一个图像字段,使用resize类,将图像存储在文件夹中,并将名称保存在数据库中。我有两个问题:

图像没有调整大小并放在文件夹上,代码只存储图像名称,而不是扩展名,它也应该存储扩展名。

所以这是我的代码:

try {

    $myValidation = new classes_ValidateForm ('registerUser', $_POST);
    $validationResult = $myValidation->getStatus();
    if (!is_array($validationResult)) {

    $arqName = $_FILES['arquivo']['name'];
    $arqTemp = $_FILES['arquivo']['tmp_name'];
    $pasta = 'img/';

//extensao do arquivo enviado

    $extensaoTemp = explode('.',$arqName);

    $extensao = strtolower(end($extensaoTemp));

//nome do arquivo TimesTamp

    $nome = time(). '.' . $extensao;

    $upload = move_uploaded_file($arqTemp, $pasta . $nome);

    if ($upload === true)

    {



          $ficheiro = $pasta.$nome; 

          $resizeObj = new resize($ficheiro); 

          $resizeObj -> resizeImage(1600, 1200, 'auto');

          $resizeObj -> saveImage("img/$nome", 100);

          $resizethumb = new resize($ficheiro); 

          $resizethumb -> resizeImage(200, 125, 'crop');

          $resizethumb -> saveImage("img/thumbnails/$nome", 100);

    }

    $myUser = new classes_User ($_POST, 'newUser');
        $myUserManager = new classes_UserManager($myControlPanel);

        $userResult = $myUserManager->addUser($myUser, $_POST['password'], $nome);

        echo $userResult;
        die();

}
}

以下是添加用户的功能:

public function addUser($myUser, $password, $image) {

    if (!is_a ($myUser, 'classes_User')) {

        throw new InvalidArgumentException('Invalid Object');
    }

    // 1 Get Username and email
    $username = $myUser->getUsername();
    $email = $myUser->getEmail();

    $query = "SELECT * FROM users WHERE username='$username' OR email='$email'";
    $myDb = $this->_controlPanel->getMyDb();
    $result = $myDb->performQuery($query);

    if ($result == "Query Error") {

        return ("ERROR ADDING USER");
    }

    elseif ($result->num_rows==1) {

        return (false);
    }

    else {

        $name = $myUser->getName();
        $encpassword = md5($password);
        $idtype = $myUser->getUserType();



        $query = "INSERT INTO users (name, username, email, password, id_type, image) VALUES ('$name', '$username', '$email', '$encpassword', '$idtype', '$image')";

        $result = $myDb->performQuery($query);

        if($result != true) {

            return ("Error Adding User:Query");
        }

        else {

            echo "<h2> You have been registered. You will be redirected to the login page.</h2>";
            header( "Refresh:3; url=login.php");
        }
    }
}

我正在使用此调整大小类:

https://gist.github.com/leonelsr/3291939#file-resize-class-php

我在这里做错了什么?它将$ nome存储在数据库中,但不包含照片的扩展名。并且图像也不存储在文件夹中。

1 个答案:

答案 0 :(得分:0)

也许你应该 使用SplFileInfo类 来获取这样的文件扩展名:

    <?php

        try {

            $myValidation       = new classes_ValidateForm ('registerUser', $_POST);
            $validationResult   = $myValidation->getStatus();

            if (!is_array($validationResult)) {
                $arqName        = $_FILES['arquivo']['name'];
                $arqTemp        = $_FILES['arquivo']['tmp_name'];
                $pasta          = 'img/';

                //extensao do arquivo enviado
                // TRY GETTING THE EXTENSION IN A SIMPLE FASHION...
                // SIMPLICITY, SOMETIMES, IS PREFERABLE
                //$extensaoTemp   = explode('.',$arqName);
                //$extensao       = strtolower(end($extensaoTemp));

                // SO WE GET THE FILE EXTENSION USING THE SplFileInfo CLASS LIKE SO:
                $info           = new SplFileInfo($flName);
                $extensao       = $info->getExtension();


                //nome do arquivo TimesTamp
                $nome           = time(). '.' . $extensao;


                // MAYBE CHECK FIRST THAT THE DIRECTORY EXIST BEFORE MOVING IT.
                // AND IF IT DOESN'T YOU MAY WANT TO CREATE IT LIKE SO:                 
                if(!is_dir($pasta)){
                    mkdir($pasta, 0777, true);
                }

                $upload         = move_uploaded_file($arqTemp, $pasta . $nome);

                if ($upload === true) {
                    $ficheiro       = $pasta.$nome;
                    $resizeObj      = new resize($ficheiro);
                    $resizeObj      ->resizeImage(1600, 1200, 'auto');
                    $resizeObj      ->saveImage("img/$nome", 100);
                    $resizethumb    = new resize($ficheiro);
                    $resizethumb    ->resizeImage(200, 125, 'crop');
                    $resizethumb    -> saveImage("img/thumbnails/$nome", 100);
                }

                $myUser         = new classes_User ($_POST, 'newUser');
                $myUserManager  = new classes_UserManager($myControlPanel);
                $userResult     = $myUserManager->addUser($myUser, $_POST['password'], $nome);

                echo $userResult;
                die();

            }
        }catch(Exception $e){

        }