我正在开发一个用户可以创建帐户和上传数据的站点的脚本,以后可以由公众搜索和访问。作为我的上传课程的一部分,我有一个重命名文件的功能,以便在我的uploads目录中没有多个具有相同名称的文件。问题是我需要从类中获取重命名的文件值,以便我可以将其放入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的值(这是我需要的值),但是当我尝试在我的主代码中设置fileName列时$ _POST [' dream'],它返回一个页面,上面写着"列fileName不能为空。"在函数内或类中设置超全局是否存在问题?如果是这样,我该怎么做才能从mySQL查询中获取$ name的值?
这是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']是由隐藏字段中的表单启动的,但它实际上并未设置为任何内容。任何人都可以帮我弄清楚这里有什么问题吗?我是新手。
答案 0 :(得分:1)
如果没有看到完整的代码流,我建议你改变move()函数中的最后一行代码:
$_POST['dream'] = $name;
要:
return $name;
然后像这样捕获返回值:
$filename = $upload->move();
然后,更改此行:
GetSQLValueString($_POST['dream'], "text"),
要:
mysql_real_escape_string($filename),