加载Blueimp jQuery File Upload时的表单数据



我似乎到处都找过了,什么都试过了,但我不能让它发挥作用,任何帮助都将不胜感激!我正在使用Blueimp的jQuery文件上传。我以以下方式更改了UploadHandler.php,以使此上载能够在我的系统中使用多个记录和位置。我使用的是PHP和MySQL。

添加到handle_file_upload函数,以便将文件路径、文件名、文件类型、文件大小、记录类型和"作业编号"上传到我在MySQL中为每个不同文件创建的表中。

$file->upload_to_db = $this->add_file($job_num, $recordtype, $file_path, $filename, $type, $file_size);

此作业编号作为get变量和隐藏表单字段发送到页面(与文件上载一起),以便在添加文件时发送到处理程序。我添加的add_file函数如下:

function add_file($job_num, $recordtype, $filepath, $filename, $filetype, $filesize)
{
$filepath = addslashes($filepath);
$insert_query = $this->query("INSERT INTO fileupload (job_num, recordtype, file_path, file_name, file_type, file_size) VALUES ('".$job_num."','".$recordtype."','".$filepath."','".$filename."','".$filetype."','".$filesize."')");
return $insert_query;
}

查询功能:

protected function query($query) {
$database = $this->options['database'];
$host = $this->options['host'];
$username = $this->options['username']; 
$password = $this->options['password'];
$Link = mysql_connect($host, $username, $password);
if (!$Link){
die( mysql_error() );
}
$db_selected = mysql_select_db($database);
if (!$db_selected){
die( mysql_error() );
}
$result = mysql_query($query);
mysql_close($Link);
return $result;
}

我还有一个删除功能。现在,这一切都很好。我添加的每个文件都会被保存,路径也会存储在数据库中。从这里开始,我必须找到一种方法,只显示为该记录上传的文件,而不是显示每个记录的所有文件。我修改了get函数:

public function get($print_response = true) {
if ($print_response && isset($_GET['download'])) {
return $this->download();
}
$uploads = $this->query_db();
return $this->generate_response($uploads, $print_response);
}

query_db函数:

public function query_db() {
$uploads_array = array();
// error_log("Job Num: ".$this->job_num."nn",3,"error_log.txt");
$select_result = $this->query("SELECT * FROM fileupload WHERE job_num=423424");
while($query_results = mysql_fetch_object($select_result))
{   
$file = new stdClass();
$file->id = $query_results->id;
$file->name = $query_results->file_name;
$file->size = $query_results->file_size;
$file->type = $query_results->file_type;
$file->url = "filepath_to_url/".$query_results->file_name;
$file->thumbnail_url = "filepath_to_thumbnail/".$query_results->file_name;
$file->delete_url = "";
$file->delete_type = "DELETE";
array_push($uploads_array,$file);
}
return $uploads_array;
}

在我的系统中文件路径正确的位置。同样,这也很好。但是,正如您从query_db函数中的查询中看到的那样,我对job_num进行了硬编码。我需要一种在加载第一页时获取此信息的方法。我一直在寻找一种方法来做到这一点,但没有成功。当我提交/添加其他文件时,我可以使用$_REQUEST['job_num']来获取它,但在加载页面时不能。我对OOP PHP不是很熟悉,所以其中一些对我来说是新的

根据您的评论,您可能希望在类中添加一个job_number属性。然后,当你像一样调用add_file()时,你可以在类中设置这个值

function add_file($job_num, $recordtype, $filepath, $filename, $filetype, $filesize)
{
$this->job_number = $job_num;
$filepath = addslashes($filepath);
$insert_query = $this->query("INSERT INTO fileupload (job_num, recordtype, file_path, file_name, file_type, file_size) VALUES ('".$job_num."','".$recordtype."','".$filepath."','".$filename."','".$filetype."','".$filesize."')");
return $insert_query;
}

然后,您可以在query_db()方法中引用此作业编号,如下所示:

public function query_db() {
$uploads_array = array();
// error_log("Job Num: ".$this->job_num."nn",3,"error_log.txt");
$select_result = $this->query("SELECT * FROM fileupload WHERE job_num=" . $this->job_number);
while($query_results = mysql_fetch_object($select_result))
{   
$file = new stdClass();
$file->id = $query_results->id;
$file->name = $query_results->file_name;
$file->size = $query_results->file_size;
$file->type = $query_results->file_type;
$file->url = "filepath_to_url/".$query_results->file_name;
$file->thumbnail_url = "filepath_to_thumbnail/".$query_results->file_name;
$file->delete_url = "";
$file->delete_type = "DELETE";
array_push($uploads_array,$file);
}
return $uploads_array;
}

注意,我在代码示例中没有考虑数据卫生。您还需要这样做,以防止SQL注入。实际上,您应该考虑使用mysqli_*函数,因为mysql_*已被弃用。您可能还应该将数据库连接传递到您的类中,因为它对类的所有区域都可用(例如add_file方法,在对象中设置作业号之前,最好先转义它)。

也许这对有帮助

protected function get_file_objects() {
$user_id = $this->options['session_id'];
$files = $this->query("SELECT yourname FROM yourname WHERE yourname='$user_id'");
$files_array=array();
while($row = mysql_fetch_assoc($files)){
array_push($files_array,$row['yourname']);
}
return array_values( array_filter( array_map(
array($this, 'get_file_object'),
$files_array
) ) );
}

最新更新