我們先來看程式碼:
先看一下View部分:
<form action="<?= Url::to(['default/datafile']) ?>" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="myFile" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form>
上面的action是用YII的助手類別建構了一個內部可以辨識的url,就是DeaufaultController.php中的actionDatafile()
(推薦教學:yii框架)
public function actionDatafile(){ if(empty($_FILES)){ $status = 1; $info = '没有文件上传'; } if($_FILES['myFile']['error'] === 0 || $_FILES['myFile']['error'] === '0' ){ //文件上传成功 $tmp = pathinfo($_FILES['myFile']['name']); $new_fname = $tmp['filename'].'_'.rand(1000000,9999999).'.'.$tmp['extension']; echo $new_fname; if(!move_uploaded_file($_FILES['myFile']['tmp_name'], '../runtime/file/'.$new_fname)){ $status = 1; $info = '上传(移动)失败'; }else{ $status = 0; $info = '上传成功'; } } else { //文件上传失败 $info = '文件上传失败'; switch($_FILES['myFile']['error']){ case 1: $info = '上传文件超过php.ini中upload_max_filesize配置参数'; break; case 2: $info = '上传文件超过表单MAX_FILE_SIZE选项指定的值'; break; case 3: $info = '文件只有部份被上传'; break; case 4: $info = '没有文件被上传'; break; case 5: $info = '上传文件大小为0'; break; } $status = 1; } return $info; }
執行後發現
解決方法:
1、檢查設定(php.ini)
file_uploads、upload_max_filesize、post_max_size、upload_tmp_dir是否已設定。
2、檢查參數
發現參數中的crsf,這個參數是yii框架驗證所帶,提及驗證,便和錯誤提示相仿,添加取消驗證程式碼,如下:
public function beforeAction($action) { if ($action->id == 'datafile') { $this->enableCsrfValidation = false; } return parent::beforeAction($action); }
更多程式相關內容,請關注php中文網程式設計入門欄位!
以上是yii2上傳檔案失敗的詳細內容。更多資訊請關注PHP中文網其他相關文章!

tobuildRobustWebapplicationswithyii,MasterTheSeskills:1)MvCarchitectureForstructuringApplications,2)ActivereCordForefifficdataBaseOperations,3)widgetsystemporreusableReusableSueuiComponents,4)驗證和驗證和驗證,5)cachingforpermificatization cachingforpermifications

TobecomeasuccessfulYiideveloper,youneed:1)PHPmastery,2)understandingofMVCarchitecture,3)Yiiframeworkproficiency,4)databasemanagementskills,5)front-endknowledge,6)APIdevelopmentexpertise,7)testinganddebuggingcapabilities,8)versioncontrolproficiency,9)

theSostCommonErrorsinyiiframeWorkare“ unknownproperty”,“無效configuration”,“ classNotfound”和“ valianationerationerrors” .1。 “ Unknownerproperty” errorSoccurWhenAccessingNon-existentSistentProperties; SusePropertiesexi; Susepropertiesexi;

歐洲Yii開發者需具備的關鍵技能包括:1.Yii框架精通,2.PHP熟練度,3.數據庫管理,4.前端技能,5.RESTfulAPI開發,6.版本控制系統,7.測試與調試,8.安全知識,9.敏捷方法論,10.軟技能,11.本地化與國際化,12.持續學習,這些技能使開發者在歐洲市場中脫穎而出。

Yes,theYiicommunityisstillactiveandvibrant.1)TheofficialYiiforumremainsaresourcefordiscussionsandsupport.2)TheGitHubrepositoryshowsregularcommitsandpullrequests,indicatingongoingdevelopment.3)StackOverflowcontinuestohostYii-relatedquestionsandhigh-qu

crigatingalaravel projectToyiiishallingButachieffable withiefleflant.1)mapoutlaravel組件likeoutes,控制器和模型。 2)Translatelaravel's sartisancancancommandeloequorentoottooyii的giiandeteverecordeba

軟技能對Yii開發者至關重要,因為它們促進團隊溝通和協作。 1)有效溝通確保項目進展順利,如通過清晰的API文檔和定期會議。 2)協作通過Yii的工具如Gii增強團隊互動,提高開發效率。

laravel'smvCarchitectureOfferSenhancedCodeorganization,改善確定性,andarobustseParefofConcerns.1)ItkeepscodeOdeOrganized,makenavigationNavigationAnvigationAndTeamWorkeer.2)itcompartmentalizestHeaplication,簡化了tompertalizestHeaplication,簡化了tlubloublyingttrublyingtimpertinging和maintenance.3)itse.3)itse


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

記事本++7.3.1
好用且免費的程式碼編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!