简体   繁体   English

在PHP中上传文件后显示错误消息

[英]Error Message shown after uploading file In PHP

I have a problem in my function: 我的功能有问题:

        public uploadfile()
        {
        //$msg=111;
        $instId=Yii::app()->session['loginUser']['intInstId'];
        $userloginId=Yii::app()->session['loginUser']['bintLoginId'];
        $folderId=$_POST['id'];
        $path=DIRECTORYPATH.'/directory/otherAssignment/'.$instId;
        //$tempFolder =$path."/tmp";
        $fileElementName = $folderId;

        if (!empty($_FILES[$fileElementName]['error'])) {
            switch($_FILES[$fileElementName]['error']) {
            case '1':
                $error = 'The uploaded file exceeds the upload_max_filesize "
                    ."directive in php.ini';
                break;
            case '2':
                $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive ".
                    ."that was specified in the HTML form';
                break;
            case '3':
                $error = 'The uploaded file was only partially uploaded';
                break;
            case '4':
                $error = 'No file was uploaded.';
                break;
            case '6':
                $error = 'Missing a temporary folder';
                break;
            case '7':
                $error = 'Failed to write file to disk';
                break;
            case '8':
                $error = 'File upload stopped by extension';
                break;
            case '999': 
            default:
                $error = 'No error code avaiable';
            }
        } elseif (empty($_FILES[$fileElementName]['tmp_name']) 
            || $_FILES[$fileElementName]['tmp_name'] == 'none'
        ) {
            $error = 'No file was uploaded..';
        } else {
            $fileSize=$_FILES[$fileElementName]['size'];
            $status=RepositoryModel::model()
                ->storageSpaceValidation($fileSize);
            if ($status==1) {
                if (!is_dir($path)) {
                    mkdir($path, 0777);
                }

                if (!is_dir($path."/".$userloginId)) {
                    mkdir($path."/".$userloginId, 0777);
                }

                if (!is_dir($path."/".$userloginId."/tmp")) {
                    mkdir($path."/".$userloginId."/tmp", 0777);
                }

                $uploadPath=$path."/".$userloginId."/tmp";
                RepositoryModel::model()
                    ->emptyTempFolder($uploadPath); 
                if (move_uploaded_file(
                $_FILES[$fileElementName]["tmp_name"],
                $uploadPath.'/'.$_FILES[$fileElementName]['name']
                )
                ) {
                    $msg= $_FILES[$fileElementName]['name'];
                }
            } else {
                $error="File can not be uploaded because "
                    ."your Storage spacae capacity is exceed.<br/> "
                    ."Plese Contact with admin!";
            }
        }
    } else {
        $session->destroy();
        $msg= "<script>window.location.href='".SITEURL."';</script>";
    }
    echo "{";
        echo                "error: '" . $error . "',\n";
        echo                "msg: '" . $msg . "'\n";
        echo "}";

}

When I try to upload a file, the file is uploaded but still the message No file was uploaded. 当我尝试上传文件时,该文件已上传,但仍然显示“ No file was uploaded. ”消息No file was uploaded. is displayed. 被陈列。 Can anybody tell me the reason that when I upload a file it was uploaded to the Temp folder but after success it will still show the error message No file was uploaded. 谁能告诉我在我上载文件时将其上载到Temp文件夹的原因,但成功后它仍会显示错误消息“ No file was uploaded. ?

If you're not getting a file uploaded, then the chances are that your form is missing the multipart declaration. 如果您没有上传文件,那么您的表单很可能缺少multipart声明。

Make sure that your <form> element contains the enctype="multipart/form-data" attribute. 确保您的<form>元素包含enctype="multipart/form-data"属性。

Somethng like this: 像这样的东西:

<form action="...your-url-here..." enctype="multipart/form-data" method="post">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM