gpt4 book ai didi

php - 在上一个版本中如何将 Jquery-File-Upload PHP 与数据库一起使用?

转载 作者:行者123 更新时间:2023-12-01 06:52:00 29 4
gpt4 key购买 nike

如何使用jQuery-File-Upload与 PHP 和数据库?我想在上传或删除图像时插入或删除有关图像的行,并且每个图像上传到目录时的名称将是 time() 。

我通过谷歌找到的所有结果告诉我,我需要编辑upload.class.php,但最后一个版本has index.php and UploadHandler.php only ...

文件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中插入或删除文件名需要添加哪些行?

附注:我使用 PHP

最佳答案

我使用文档,现在可以说文件 upload.class.php 需要编辑文件 UploadHandler.php然后使用下一个:

搜索这一行 -> $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);

享受=)

关于php - 在上一个版本中如何将 Jquery-File-Upload PHP 与数据库一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13534530/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com