1

It seems that I have looked everywhere and tried everything and I can not get this to work, any help would be much appreciated! I am using Blueimp's jQuery File Upload. I have changed the UploadHandler.php in the following ways to make this upload work with multiple records and places within my system. I am using PHP and MySQL.

Added to handle_file_upload function so that the file path, file name, file type, file size, the record type, and the "job number" is uploaded to a table I created in MySQL for each different file.

$file->upload_to_db = $this->add_file($job_num, $recordtype, $file_path, $filename, $type, $file_size);

This job number is sent to the page (with the file upload) as a get variable as well as a hidden form field to send to the Handler when adding a file. The add_file function I added is as follows:

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;
}

The query function:

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;
}

I have a function for the delete as well. Now, this all works fine. Every file I add is saved and the path is stored in the database. From here I had to find a way to only show the files uploaded for that record instead of showing all files for every record. I modified the get function:

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);
}

The query_db function:

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;
}

Where the filepath is correct on my system. This, again, works just fine. But as you can see from the query in the query_db function I have the job_num hard coded. I need a way to get this when the first page is loaded. I have looked for a way to do this and nothing has worked. When I submit/add other files I can use the $_REQUEST['job_num'] to get it, but not when the page is loaded. I am not very familiar with OOP PHP so some of this is new to me.

4

2 回答 2

0

也许这会有所帮助

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
    ) ) );
}
于 2013-01-25T18:30:14.947 回答
0

根据您的评论,您可能希望向job_number您的班级添加一个属性。add_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_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将作业编号设置到对象中之前最好转义作业编号的方法)。

于 2012-12-10T18:21:09.810 回答