[英]“Notice (8): Undefined index: ” cakephp
我正在為我的用戶構建一個pdf轉換實用程序。 我在CakePhp
工作,我的控制器正在接收Ajax呼叫。 為什么我收到通知:8錯誤
控制器:
public function convertToPdf() {
$this->autoRender = false;
$pdf = new WkHtmlToPdf;
//$this->RequestHandler->respondAs('json');
// echo $convertData = json_encode($inputVal);
if ($this->RequestHandler->isAjax()) {
$pdfName = uniqid();
if ($_FILES['conversionSourceFile']) {
echo "File";
$pdf->addPage($_FILES['conversionSourceFile']['tmp_name']);
} elseif ($_POST['conversionSourceUrl']) {
echo "Url";
$pdf->addPage($_POST['conversionSourceUrl']);
} elseif ($_POST['conversionSourceHtml']) {
echo "Html";
$pdf->addPage('<html>' . $_POST['conversionSourceHtml'] . '</html>');
}
$saveToPath = 'upload/' . $pdfName . '.pdf';
if ($pdf->saveAs($saveToPath)) {
echo 'upload/' . $pdfName . '.pdf';
}
}
}
錯誤:注意(8):未定義索引:conversionSourceFile [APP / Controller / PdfsController.php,第42行]代碼上下文
if ($this->RequestHandler->isAjax()) {
$pdfName = uniqid();
if ($_FILES['conversionSourceFile']) {
PdfsController::convertToPdf() - APP/Controller/PdfsController.php, line 42
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE/Cake/Controller/Controller.php, line 486
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 187
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 162
[main] - APP/webroot/index.php, line 109
為了避免在代碼中引起notice
,您必須使用isset()
或!empty()
。
使用isset()
和!empty()
可以檢查是否設置了變量並且沒有空值。
例如,
if (isset($_FILES['conversionSourceFile'])) {
// your code
}
要么
if (!empty($_FILES['conversionSourceFile'])) {
// your code
}
只需檢查isset
$ _FILES超全局變量以防止未設置時發出通知。
if (isset($_FILES['conversionSourceFile'])) {
// Do more stuff
}
您需要使用“ isset”或“!empty()”檢查第一個條件,例如:
if(isset($_FILES['conversionSourceFile'])){...}
我解決了錯誤“ Notice(8):未定義索引”,更改了var $使用的順序。
之前:
class GastosController extends AppController {
var $uses = array('Comprobante','Gasto','TipoGasto');
......
后:
class GastosController extends AppController {
var $uses = array('Gasto','Comprobante','TipoGasto');
.. .. ..
首先,輸入與類別(Gasto)相對應的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.