Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 加载Blueimp jQuery文件上载时的表单数据_Php_Jquery_File Upload_Blueimp - Fatal编程技术网

Php 加载Blueimp jQuery文件上载时的表单数据

Php 加载Blueimp jQuery文件上载时的表单数据,php,jquery,file-upload,blueimp,Php,Jquery,File Upload,Blueimp,看来我到处都找过了,什么都试过了,但我无法让它发挥作用,任何帮助都将不胜感激!我正在使用Blueimp的jQuery文件上传。我通过以下方式更改了UploadHandler.php,以使此上载能够在系统中使用多个记录和位置。我正在使用PHP和MySQL 添加了handle_file_upload函数,以便将文件路径、文件名、文件类型、文件大小、记录类型和“作业号”上载到我在MySQL中为每个不同文件创建的表中 $file->upload_to_db = $this->add_file

看来我到处都找过了,什么都试过了,但我无法让它发挥作用,任何帮助都将不胜感激!我正在使用Blueimp的jQuery文件上传。我通过以下方式更改了UploadHandler.php,以使此上载能够在系统中使用多个记录和位置。我正在使用PHP和MySQL

添加了handle_file_upload函数,以便将文件路径、文件名、文件类型、文件大小、记录类型和“作业号”上载到我在MySQL中为每个不同文件创建的表中

$file->upload_to_db = $this->add_file($job_num, $recordtype, $file_path, $filename, $type, $file_size);
此作业编号作为get变量以及添加文件时发送给处理程序的隐藏表单字段发送到页面(带有文件上载)。我添加的add_file函数如下所示:

function add_file($job_num, $recordtype, $filepath, $filename, $filetype, $filesize)
{
    $filepath = addslashes($filepath);
    $insert_query = $this->query("INSERT INTO fileupload (job_num, recordtype, file_path, file_name, file_type, file_size) VALUES ('".$job_num."','".$recordtype."','".$filepath."','".$filename."','".$filetype."','".$filesize."')");
    return $insert_query;
}
查询功能:

protected 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;
}
public function get($print_response = true) {
      if ($print_response && isset($_GET['download'])) {
        return $this->download();
    }
    $uploads = $this->query_db();
    return $this->generate_response($uploads, $print_response);
}
public function query_db() {
    $uploads_array = array();
    // error_log("Job Num: ".$this->job_num."\n\n",3,"error_log.txt");
    $select_result = $this->query("SELECT * FROM fileupload WHERE job_num=423424");
    while($query_results = mysql_fetch_object($select_result))
    {   
        $file = new stdClass();
        $file->id = $query_results->id;
        $file->name = $query_results->file_name;
        $file->size = $query_results->file_size;
        $file->type = $query_results->file_type;
        $file->url = "filepath_to_url/".$query_results->file_name;
        $file->thumbnail_url = "filepath_to_thumbnail/".$query_results->file_name;
        $file->delete_url = "";
        $file->delete_type = "DELETE";
        array_push($uploads_array,$file);
    }
    return $uploads_array;
}
我还有一个删除功能。现在,这一切都很好。我添加的每个文件都会保存,路径存储在数据库中。从这里我必须找到一种方法,只显示该记录上传的文件,而不是显示每个记录的所有文件。我修改了get函数:

protected 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;
}
public function get($print_response = true) {
      if ($print_response && isset($_GET['download'])) {
        return $this->download();
    }
    $uploads = $this->query_db();
    return $this->generate_response($uploads, $print_response);
}
public function query_db() {
    $uploads_array = array();
    // error_log("Job Num: ".$this->job_num."\n\n",3,"error_log.txt");
    $select_result = $this->query("SELECT * FROM fileupload WHERE job_num=423424");
    while($query_results = mysql_fetch_object($select_result))
    {   
        $file = new stdClass();
        $file->id = $query_results->id;
        $file->name = $query_results->file_name;
        $file->size = $query_results->file_size;
        $file->type = $query_results->file_type;
        $file->url = "filepath_to_url/".$query_results->file_name;
        $file->thumbnail_url = "filepath_to_thumbnail/".$query_results->file_name;
        $file->delete_url = "";
        $file->delete_type = "DELETE";
        array_push($uploads_array,$file);
    }
    return $uploads_array;
}
查询数据库函数:

protected 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;
}
public function get($print_response = true) {
      if ($print_response && isset($_GET['download'])) {
        return $this->download();
    }
    $uploads = $this->query_db();
    return $this->generate_response($uploads, $print_response);
}
public function query_db() {
    $uploads_array = array();
    // error_log("Job Num: ".$this->job_num."\n\n",3,"error_log.txt");
    $select_result = $this->query("SELECT * FROM fileupload WHERE job_num=423424");
    while($query_results = mysql_fetch_object($select_result))
    {   
        $file = new stdClass();
        $file->id = $query_results->id;
        $file->name = $query_results->file_name;
        $file->size = $query_results->file_size;
        $file->type = $query_results->file_type;
        $file->url = "filepath_to_url/".$query_results->file_name;
        $file->thumbnail_url = "filepath_to_thumbnail/".$query_results->file_name;
        $file->delete_url = "";
        $file->delete_type = "DELETE";
        array_push($uploads_array,$file);
    }
    return $uploads_array;
}

其中文件路径在我的系统上是正确的。同样,这种方法效果很好。但正如您从query\u db函数中的查询中所看到的,我对job\u num进行了硬编码。我需要一种方法来获得这当第一页加载。我一直在寻找一种方法来做到这一点,但没有任何效果。当我提交/添加其他文件时,我可以使用$\u请求['job\u num']获取它,但在加载页面时不能。我对OOP PHP不是很熟悉,所以其中一些对我来说是新的

根据您的评论,您可能希望在类中添加一个
job\u number
属性。然后,当您像这样调用
add\u file()
时,可以在类中设置此值

function add_file($job_num, $recordtype, $filepath, $filename, $filetype, $filesize)
{
    $this->job_number = $job_num;
    $filepath = addslashes($filepath);
    $insert_query = $this->query("INSERT INTO fileupload (job_num, recordtype, file_path, file_name, file_type, file_size) VALUES ('".$job_num."','".$recordtype."','".$filepath."','".$filename."','".$filetype."','".$filesize."')");
    return $insert_query;
}
然后,您可以在
query\u db()
方法中引用此作业编号,如下所示:

public function query_db() {
    $uploads_array = array();
    // error_log("Job Num: ".$this->job_num."\n\n",3,"error_log.txt");
    $select_result = $this->query("SELECT * FROM fileupload WHERE job_num=" . $this->job_number);
    while($query_results = mysql_fetch_object($select_result))
    {   
        $file = new stdClass();
        $file->id = $query_results->id;
        $file->name = $query_results->file_name;
        $file->size = $query_results->file_size;
        $file->type = $query_results->file_type;
        $file->url = "filepath_to_url/".$query_results->file_name;
        $file->thumbnail_url = "filepath_to_thumbnail/".$query_results->file_name;
        $file->delete_url = "";
        $file->delete_type = "DELETE";
        array_push($uploads_array,$file);
    }
    return $uploads_array;
}

注意:我在代码示例中没有考虑数据卫生。您还需要这样做以防止SQL注入。实际上,您应该考虑使用
mysqli.*
函数,因为
mysql.*
已被弃用。您可能还应该将数据库连接传递到您的类中,您可以将其用于类的所有区域(如
add_file
方法,在对象中设置作业编号之前,最好先将其转义)。

这可能会有所帮助

protected function get_file_objects() {

    $user_id = $this->options['session_id'];
    $files = $this->query("SELECT yourname FROM yourname WHERE yourname='$user_id'");
    $files_array=array();
    while($row = mysql_fetch_assoc($files)){
        array_push($files_array,$row['yourname']);
    }
    return array_values( array_filter( array_map(
        array($this, 'get_file_object'),
        $files_array
    ) ) );
}

我不确定我是否在关注这个问题,因为你似乎发布了一堆工作代码,并没有真正发现不工作的地方。据我所知,您在
$job\u num
中有作业编号。为什么不把这个值传递给
query\u db()
函数?$job\u num在handle\u file\u upload函数中。该变量仅在上载文件时设置。为了设置该变量,我从传递给类的隐藏表单字段中使用了$u REQUEST['job\u num']。我不知道在页面实际加载时如何将其传递给类。我发布了很多有用的代码,因为我所有的东西都是用硬编码的。我需要一种在页面第一次加载时传递它的方法,但我不知道如何传递。如何将$job_num属性添加到类中?在index.php(与UploadHandler.php目录相同)中,需要处理程序和$upload_handler=new UploadHandler();我不知道这个页面是如何命名的,但当我把这两行注释掉时,一切都不起作用。我尝试将它们移动到包含文件上载的脚本,以便我可以执行新的UploadHandler($job_num),但这不起作用。我得到了一个“No database selected”错误。我在文件搜索中查找index.php,它没有找到的地方,但不知何故它被调用了,这就是创建对象的地方。那么,如果我不能在index.phpYou中添加属性,我该如何添加属性呢?你在哪里添加了这些方法并复制粘贴了你发布的代码?这个类就是你添加额外属性的地方。我想我不确定你所说的属性是什么意思。我有$upload\u handler=newuploadhandler($job\u num);以及在uu构造中。但是$job_num在index.php中是空的,因为我不理解blueimp是如何设置的。I属性是存储在类中的变量。如果您已经将$job_num传递给构造函数,只需在构造函数中设置
$this->job_number
,并在需要时在该类实例中引用它。我对BlueImp或您正在使用的东西一无所知,并且您还没有在index.php中显示代码,您可能会理解它是如何填充的。