[英]Form data on load of 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變量發送到頁面(帶有文件上傳),並在添加文件時發送給Handler的隱藏表單字段。 我添加的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."\n\n",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."\n\n",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
) ) );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.