在上一版本中,如何使用Jquery文件上传PHP和数据库?

在上一版本中,如何使用Jquery文件上传PHP和数据库?,php,jquery,file-upload,multifile-uploader,blueimp,Php,Jquery,File Upload,Multifile Uploader,Blueimp,如何使用PHP和数据库? 我想在上载或删除图像时插入或删除关于图像的行,并且在将图像上载到目录时,每个图像的名称将作为time() 我通过谷歌找到的所有结果都告诉我,我需要编辑才能上传.class.php,但上一个版本 文件UploadHandler.php具有UploadHandler类和代码 public function post($print_response = true) { if (isset($_REQUEST['_method']) && $_R

如何使用PHP和数据库? 我想在上载或删除图像时插入或删除关于图像的行,并且在将图像上载到目录时,每个图像的名称将作为time()

我通过谷歌找到的所有结果都告诉我,我需要编辑才能上传.class.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中插入或删除文件名需要添加哪些行


注意:我使用PHP

我使用docementation,现在可以说file upload.class.PHP需要编辑file UploadHandler.PHP 然后使用下一步:

搜索此行->$this->options=array(

在下一行中添加以下代码:

// mysql connection settings
'database' => '**YOUR DATABASE**',
'host' => '**localhost**',
'username' => '**YOUR USERNAME**',
'password' => '**YOUR PASSWORD**',
// end
因此,现在您必须为SQL查询编写一个函数,例如在handle\u file\u 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;
}
因此,在这个函数中,我们使用夹钳之间的字符串调用函数query

您还可以插入其他详细信息,例如文件大小

至少我们必须调用此函数,在函数handle\u file\u upload的末尾使用以下代码。将以下代码粘贴到此行下方或上方:$file->size=$file\u 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;
}
现在我们必须调用函数,这次是delete函数。 转到delete函数并搜索这一行:if($success){将下面的代码粘贴到这一行

$this->delete_img($file_name);

享受=)

我使用docementation,现在可以说file upload.class.php需要编辑file UploadHandler.php 然后使用下一步:

搜索此行->$this->options=array(

在下一行中添加以下代码:

// mysql connection settings
'database' => '**YOUR DATABASE**',
'host' => '**localhost**',
'username' => '**YOUR USERNAME**',
'password' => '**YOUR PASSWORD**',
// end
因此,现在您必须为SQL查询编写一个函数,例如在handle\u file\u 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;
}
因此,在这个函数中,我们使用夹钳之间的字符串调用函数query

您还可以插入其他详细信息,例如文件大小

至少我们必须调用此函数,在函数handle\u file\u upload的末尾使用以下代码。将以下代码粘贴到此行下方或上方:$file->size=$file\u 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;
}
现在我们必须调用函数,这次是delete函数。 转到delete函数并搜索这一行: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,
));

您应该重载默认方法(如文档中所述),以便添加自定义功能。 如果您修改了原始文件,当插件的新版本(或补丁)发布时,您将有更多的工作要做

出于我自己的目的,我基本上定义了一个自定义类,它扩展了原始类并只修改了/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,
));

洛基,你所说的
把下面的代码粘贴在上面是什么意思?是说用
$this->delete\img($file\u name);
替换
如果($success){
,还是把它放在
如果($success)上面{
???我的解释是:用Loki提供的代码覆盖
if
结构中的代码。最后,它应该是这样的:
if($success){$this->delete_img($file_name);}
他指的是在
if($success){
下面的
$success
函数中的代码($this->options['image_versions']as$version=>$options){
。我正在使用它,它可以工作。你说的
将下面的代码粘贴在上面是什么意思?
。这是指用
$this->delete_img($file_name)
替换
if($success){
吗{???我的解释是:用Loki提供的代码覆盖
if
结构中的代码。最后,它应该是这样的:
if($success){$this->delete_img($file_name);}
他指的是在
if($success){
下面的
$success
函数中的代码($this->options['image\u versions']作为$version=>$options){。我正在使用它,它可以工作