我正在为一个网站编写脚本,用户可以在该网站上创建帐户并上传数据,以便公众稍后搜索和访问这些数据。作为我的上传类的一部分,我有一个重命名文件的功能,这样我的上传目录中就没有多个同名文件。问题是我需要从类中取出重命名的文件值,以便可以将其放入 mySQL 数据库的 fileName 部分。
真正在类中工作的函数的代码:
class Ds1_Upload {
protected $_uploaded = array();
protected $_destination;
protected $_max = 10485760;
protected $_messages = array();
protected $_permitted = array('audio/mpeg','audio/wav','audio/mpeg3','audio/x-mpeg-3');
protected $_renamed = false;
public function __construct($path) {
if(!is_dir($path) || !is_writable($path)) {
throw new Exception("$path must be a valid, writable directory.");
}
$this->_destination = $path;
$this->_uploaded = $_FILES;
}
public function move($overwrite = false) {
$field = current($this->_uploaded);
$OK = $this->checkError($field['name'], $field['error']);
if($OK) {
$sizeOK = $this->checkSize($field['name'],$field['size']);
$typeOK = $this->checkType($field['name'], $field['type']);
if($sizeOK && $typeOK) {
$name = $this->checkName($field['name'], $overwrite);
$success = move_uploaded_file($field['tmp_name'], $this->_destination.$name);
if($success) {
$message = $field['name']. ' was uploaded successfully';
if ($this->_renamed) {
$message .=" and renamed $name as a result of a conflicting filename in the Sleep Speak database.";
}
$this->_messages[] = $message;
} else {
$this->_messages[] = 'Could not upload ' . $field['name'];
}
$_POST['dream'] = $name;
}
}
}
如您所见,我试图让它设置一个 $_POST 变量等于 $name 的值(这是我需要的值),但是当我尝试使用 $_POST[ 在我的主代码中设置 fileName 列时'dream'],它返回一个显示“列文件名不能为空”的页面。在函数或类中设置超全局变量是否存在问题?如果是这样,我该怎么做才能从类中获取 $name 的值,以便通过 mySQL 查询访问?
这是mySQL代码
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "fileupload")) {
$insertSQL = sprintf("INSERT INTO Dreams (fileName, userName, dreamName, tags, uploadDate) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['dream'], "text"),
GetSQLValueString($_POST['user'], "text"),
GetSQLValueString($_POST['title'], "text"),
GetSQLValueString($_POST['tags'], "text"),
GetSQLValueString($_POST['date'], "text"));
mysql_select_db($database_Sleep_Speak, $Sleep_Speak);
$Result1 = mysql_query($insertSQL, $Sleep_Speak) or die(mysql_error());
}
所有其他 $_POST 值都由正在提交的表单设置。$_POST['dreams'] 由隐藏字段中的表单启动,但实际上并未设置任何内容。谁能帮我弄清楚这里出了什么问题?我是新来的。