简体   繁体   English

如何在最新版本的数据库中使用Jquery-File-Upload PHP?

[英]How use Jquery-File-Upload PHP with database in last release?

How to use jQuery-File-Upload with PHP and database? 如何在PHP和数据库中使用jQuery-File-Upload I want to insert or delete rows about images when I upload or delete images and that name of each image will be as time() when they are uploaded to the directory. 我要在上载或删除图像时插入或删除有关图像的行,并且每张图像的名称将在它们上载到目录时为time()。

All result I found through google tell me that I need edit to upload.class.php but the last release has index.php and UploadHandler.php only ... 我通过google找到的所有结果都告诉我,我需要进行编辑以上传upload.class.php,但最后一个发行版仅具有index.php和UploadHandler.php ...

file UploadHandler.php has class UploadHandler with code 文件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);
        }

What rows do I need to add to insert or delete file names in mysql? 我需要添加哪些行才能在mysql中插入或删除文件名?

PS: I use PHP PS:我使用PHP

I use docementation and now can said that instead file upload.class.php need edit file UploadHandler.php and than use next: 我使用docementation,现在可以说,文件upload.class.php需要编辑文件UploadHandler.php,然后使用next:

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

Add the following Code in the next lines : 在以下各行中添加以下代码:

// mysql connection settings
'database' => '**YOUR DATABASE**',
'host' => '**localhost**',
'username' => '**YOUR USERNAME**',
'password' => '**YOUR PASSWORD**',
// end

So now you have to write a function for the SQL Query, copy & paste the following code for example after the handle_file_upload function : 因此,现在您必须为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;
}

Add file details to database I explain this function with a picture upload, so here we save the picture name to the database Add this function also too the upload.class.php 将文件详细信息添加到数据库我通过图片上传来解释此功能,因此在这里,我们将图片名称保存到数据库中。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;
}

so in this function we call the function query with the string between the clamps. 因此,在此函数中,我们使用钳位之间的字符串调用函数查询。

You could also insert other details too, for example, the file size. 您也可以插入其他详细信息,例如文件大小。

At least we have to call this function, with the following code at the end of the function handle_file_upload. 至少我们必须调用此函数,并在函数handle_file_upload的末尾添加以下代码。 Paste the following code underneath or over this line : $file->size = $file_size; 将以下代码粘贴到此行的下方或上方: $ file-> size = $ file_size;

$file->upload_to_db = $this->add_img($file->name);

Delete the entry we created Deleting the entry we made before is very easy, we create a new function which makes also an sql query to delete it. 删除我们创建的条目删除之前创建的条目非常容易,我们创建了一个新函数,该函数还通过sql查询将其删除。

function delete_img($delimg)
{
$delete_from_db = $this->query("DELETE FROM yourtable WHERE yourcolumnone = '$delimg'") or die(mysql_error());
return $delete_from_db;
}

Now we must call the function, this time of the delete function. 现在我们必须调用函数,这次是删除函数。 Go to the delete function and search this line : if ($success) { paste the following code over this. 转到删除功能并搜索以下行:if($ success){将以下代码粘贴在其上。

$this->delete_img($file_name);

Enjoy=) 享受=)

You should overload the default methods (as described in the documentation) in order to add your custom features. 您应该重载默认方法(如文档中所述)以添加自定义功能。 If you modify original files, you'll have more work when a new version (or a fix) of the plugin is released. 如果您修改原始文件,则在发布该插件的新版本(或修补程序)时,您将需要进行更多工作。

For my own purposes, I basically defined a custom class which extends the original one and modified only the /server/php/index.php file: 出于我自己的目的,我基本上定义了一个自定义类,该类扩展了原始类并仅修改了/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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM