如何在上一个版本中使用Jquery-File-Upload PHP与数据库? [英] How use Jquery-File-Upload PHP with database in last release?

查看:240
本文介绍了如何在上一个版本中使用Jquery-File-Upload PHP与数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用PHP和数据库使用 jQuery-File-Upload
当我上传或删除图像时,我想插入或删除图像的行,每个图像的名称将被作为时间()上传到目录。



我通过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;
//解析Content-Disposition头,如果可用:
$ file_name = isset($ _ SERVER ['HTTP_CONTENT_DISPOSITION'])?
rawurldecode(preg_replace(
'/(^ [^] +)|($)/',
'',
$ _SERVER ['HTTP_CONTENT_DISPOSITION'] $ (b $ b)):null;
$ file_type = isset($ _ SERVER ['HTTP_CONTENT_DESCRIPTION'])?
$ _SERVER ['HTTP_CONTENT_DESCRIPTION']:null;
//解析Content-范围标题,格式如下:
// 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是一个数组标识符,比如files [],
// $ _FILES是一个多维数组:
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
);

$ b} else {
// param_name是单个对象标识符,比如file,
// $ _FILES是一维数组:
$ 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);



$ b 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]!=='。'&&取消链接($ 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);



$ b return $ this-> generate_response($ success,$ print_response);





$ b

我需要添加哪些行来插入或删除mysql中的文件名?



PS:我使用PHP

解决方案

可以这样说,而不是文件upload.class.php需要编辑文件UploadHandler.php
而不是使用next:

搜索这一行 - > $ this-> options = array($ / b>

在下一行添加下面的代码:

<$ p $ // mysql连接设置
'database'=>'**您的数据库**',
'host'=>'** localhost **',
'username'=>'**您的用户名**',
'密码'=>'**您的密码**',
//结束

所以现在你必须为SQL Query编写一个函数,例如在handle_file_upload函数之后拷贝和粘贴下面的代码:

 函数查询($ query){
$ database = $ this->选项s ['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);
返回$ result;







$上传,所以这里我们把图片名保存到数据库中,加上这个函数也是upload.class.php

$ p $函数add_img $ INSERT INTO yourtable(** yourcolumnone **)VALUES('。$ whichimg。'))或死(mysql_error)

$ add_to_db = $ this-> ());
返回$ add_to_db;





$ b

所以在这个函数中,我们使用夹子之间的字符串来调用函数查询。

您也可以插入其他的细节,例如文件大小。



至少我们必须调用此函数,在函数handle_file_upload的末尾添加以下代码。将下面的代码粘贴到这行下面或之上: $ file-> size = $ file_size;

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

删除我们创建的条目
删除我们之前创建的条目非常简单,我们创建一个新的函数,也使一个SQL查询删除它。

pre $函数delete_img($ delimg)
{
$ delete_from_db = $ this-> query(DELETE FROM yourtable WHERE yourcolumnone ='$ delimg')或die(mysql_error());
返回$ delete_from_db;

$ / code>

现在我们必须调用函数,这次是删除函数。
转到删除功能,并搜索这一行:if($ success){粘贴下面的代码在这个。

  $这 - > delete_img($ FILE_NAME); 

Enjoy =)

How to use jQuery-File-Upload with PHP and database? 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.

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...

file UploadHandler.php has class UploadHandler with code

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?

P.S.: I use PHP

解决方案

I use docementation and now can said that instead file upload.class.php need edit file UploadHandler.php and than use next:

Search this line - > $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 :

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

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. Paste the following code underneath or over this line : $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.

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.

$this->delete_img($file_name);

Enjoy=)

这篇关于如何在上一个版本中使用Jquery-File-Upload PHP与数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆