[英]How use Jquery-File-Upload PHP with database in last release?
如何在PHP和數據庫中使用jQuery-File-Upload ? 我要在上載或刪除圖像時插入或刪除有關圖像的行,並且每張圖像的名稱將在它們上載到目錄時為time()。
我通過google找到的所有結果都告訴我,我需要進行編輯以上傳upload.class.php,但最后一個發行版僅具有index.php和UploadHandler.php ...
文件UploadHandler.php具有帶有代碼的類UploadHandler
public function post($print_response = true) {
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
return $this->delete($print_response);
}
$upload = isset($_FILES[$this->options['param_name']]) ?
$_FILES[$this->options['param_name']] : null;
// Parse the Content-Disposition header, if available:
$file_name = isset($_SERVER['HTTP_CONTENT_DISPOSITION']) ?
rawurldecode(preg_replace(
'/(^[^"]+")|("$)/',
'',
$_SERVER['HTTP_CONTENT_DISPOSITION']
)) : null;
$file_type = isset($_SERVER['HTTP_CONTENT_DESCRIPTION']) ?
$_SERVER['HTTP_CONTENT_DESCRIPTION'] : null;
// Parse the Content-Range header, which has the following form:
// Content-Range: bytes 0-524287/2000000
$content_range = isset($_SERVER['HTTP_CONTENT_RANGE']) ?
preg_split('/[^0-9]+/', $_SERVER['HTTP_CONTENT_RANGE']) : null;
$size = $content_range ? $content_range[3] : null;
$info = array();
if ($upload && is_array($upload['tmp_name'])) {
// param_name is an array identifier like "files[]",
// $_FILES is a multi-dimensional array:
foreach ($upload['tmp_name'] as $index => $value) {
$info[] = $this->handle_file_upload(
$upload['tmp_name'][$index],
$file_name ? $file_name : $upload['name'][$index],
$size ? $size : $upload['size'][$index],
$file_type ? $file_type : $upload['type'][$index],
$upload['error'][$index],
$index,
$content_range
);
}
} else {
// param_name is a single object identifier like "file",
// $_FILES is a one-dimensional array:
$info[] = $this->handle_file_upload(
isset($upload['tmp_name']) ? $upload['tmp_name'] : null,
$file_name ? $file_name : (isset($upload['name']) ?
$upload['name'] : null),
$size ? $size : (isset($upload['size']) ?
$upload['size'] : $_SERVER['CONTENT_LENGTH']),
$file_type ? $file_type : (isset($upload['type']) ?
$upload['type'] : $_SERVER['CONTENT_TYPE']),
isset($upload['error']) ? $upload['error'] : null,
null,
$content_range
);
}
return $this->generate_response($info, $print_response);
}
public function delete($print_response = true) {
$file_name = $this->get_file_name_param();
$file_path = $this->get_upload_path($file_name);
$success = is_file($file_path) && $file_name[0] !== '.' && unlink($file_path);
if ($success) {
foreach($this->options['image_versions'] as $version => $options) {
if (!empty($version)) {
$file = $this->get_upload_path($file_name, $version);
if (is_file($file)) {
unlink($file);
}
}
}
}
return $this->generate_response($success, $print_response);
}
我需要添加哪些行才能在mysql中插入或刪除文件名?
PS:我使用PHP
我使用docementation,現在可以說,文件upload.class.php需要編輯文件UploadHandler.php,然后使用next:
搜索此行 -> $ this-> options = array(
在以下各行中添加以下代碼:
// mysql connection settings
'database' => '**YOUR DATABASE**',
'host' => '**localhost**',
'username' => '**YOUR USERNAME**',
'password' => '**YOUR PASSWORD**',
// end
因此,現在您必須為SQL查詢編寫一個函數,將以下代碼復制並粘貼到例如handle_file_upload函數之后:
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;
}
將文件詳細信息添加到數據庫我通過圖片上傳來解釋此功能,因此在這里,我們將圖片名稱保存到數據庫中。upload.class.php也要添加此功能。
function add_img($whichimg)
{
$add_to_db = $this->query("INSERT INTO yourtable (**yourcolumnone**) VALUES ('".$whichimg."')") or die(mysql_error());
return $add_to_db;
}
因此,在此函數中,我們使用鉗位之間的字符串調用函數查詢。
您也可以插入其他詳細信息,例如文件大小。
至少我們必須調用此函數,並在函數handle_file_upload的末尾添加以下代碼。 將以下代碼粘貼到此行的下方或上方: $ file-> size = $ file_size;
$file->upload_to_db = $this->add_img($file->name);
刪除我們創建的條目刪除之前創建的條目非常容易,我們創建了一個新函數,該函數還通過sql查詢將其刪除。
function delete_img($delimg)
{
$delete_from_db = $this->query("DELETE FROM yourtable WHERE yourcolumnone = '$delimg'") or die(mysql_error());
return $delete_from_db;
}
現在我們必須調用函數,這次是刪除函數。 轉到刪除功能並搜索以下行:if($ success){將以下代碼粘貼在其上。
$this->delete_img($file_name);
享受=)
您應該重載默認方法(如文檔中所述)以添加自定義功能。 如果您修改原始文件,則在發布該插件的新版本(或修補程序)時,您將需要進行更多工作。
出於我自己的目的,我基本上定義了一個自定義類,該類擴展了原始類並僅修改了/server/php/index.php文件:
class myUploadHandler extends UploadHandler {
//do your stuff
//for exemple if you are using a DB:
//overload methods like handle_file_upload() for insertion in a DB,
//set_additional_file_properties() if you need more fields linked to each uploaded file
// or delete() also if you want to remove from DB
}
$upload_handler = new myUploadHandler(array(
'user_dirs' => true,
'download_via_php' => true,
));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.