我正在使用下面的 HTML 表单将文件传递给 PHP 进行处理。但是 PHP 说索引是未定义的。
同样,我在 config.php 中定义的变量显示为未定义。
我在这里想念什么?
类.php
<?php
require('config.php');
error_reporting(E_ALL);
/*
* Classes required by the script
*
*/
class database extends PDO
{
public $conURL;
public function __construct($config) {
$conURL = "mysql:host=" . $config['host'] . ";dbname=" . $config['db'];
try {
parent::__construct($conURL, $config['user'], $config['pass']);
} catch (PDOException $e) {
$e->getMessage();
}
}
}
class upload
{
public $_FILES;
public function uploadFile() {
if ($_FILES['file']['size'] >= 2000000) {
echo "File is too large!";
}
elseif (isset($_FILES['file'])) {
$stmt = $this->prepare("INSERT INTO upload (name, type, size, content) VALUES (?, ?, ?, ?)");
$stmt->execute(array($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $_FILES['file']['file']));
}
}
}
配置文件
<?php
$config = array(
'host' => 'localhost', // db host
'user' => 'root', // db user
'pass' => 'mypassword', //db pass
'db' => 'files' // db name
);
上传.php
<?php
error_reporting(E_ALL);
require('config.php');
require('classes.php');
$dbh = new database($config);
$upload = new upload();
$upload->uploadFile();
HTML 表单
<form name="uploaddb" action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<input type="file" name="file" />
<br/>
<input type="submit" name="submit" value="Upload!" />
</form>
供参考,以下是错误:
注意:未定义的变量:第 28 行 /Applications/MAMP/htdocs/files/classes.php 中的配置
注意:未定义的索引:第 34 行 /Applications/MAMP/htdocs/files/classes.php 中的文件