codeigniter + uploadify在mysql中存储文件名

时间:2011-10-05 09:54:17

标签: mysql codeigniter uploadify

Codeigniter 2和uploadify 2工作,但我想将文件名存储在mysql中我做了2个方法

使用ajax的第一种方法

'onComplete'  : function(event, ID, fileObj, response, data) {
   $.ajax({
      type: "POST",
      url: "<?php echo site_url('upload/file_upload'); ?>" ,
      data: {filename: fileObj.name,admin_id: $('#admin_id').val(),client_id: $('#client_id').val(),studio_id: $('#studio_id').val()},
      success: function(data){
            $('#status-message').text(data.filesUploaded + ' files uploaded, ' + data.errors + ' errors.');
      }
   });
  } 

 //CI Controller and Models

  public function file_upload()
    {

          $data = array(
            'admin_id' => '123',//$this->session->userdata('user_id'),
            'filename' =>  $this->input->post('file_name'),
            'studio_id' =>  $this->input->post('studio_id'),
            'client_id' => $this->input->post('client_id')
        );

        $this->file_upload_model->add($data);  

    }

Model 

class File_upload_model{

function add($data){

    $this->db->insert('upload_images',$data);

}
}

第二种方法        if(!empty($ _ FILES)){

$path = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';

 //$client_id = $_GET['client_id'];

 $file_temp = $_FILES['Filedata']['tmp_name'];

 $file_name = prep_filename($_FILES['Filedata']['name']);

 $file_ext = get_extension($_FILES['Filedata']['name']);

 $real_name = $file_name;

 $newf_name = set_filename($path, $file_name, $file_ext);

 $file_size = round($_FILES['Filedata']['size']/1024, 2);

 $file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES['Filedata']['type']);

 $file_type = strtolower($file_type);

 $targetFile =  str_replace('//','/',$path) . $newf_name;

 move_uploaded_file($file_temp,$targetFile);



 $filearray = array();

 $filearray['file_name'] = $newf_name;

 $filearray['real_name'] = $real_name;

 $filearray['file_ext'] = $file_ext;

 $filearray['file_size'] = $file_size;

 $filearray['file_path'] = $targetFile;

 $filearray['file_temp'] = $file_temp;

 //$filearray['client_id'] = $client_id;



 $json_array = json_encode($filearray);

 echo $json_array;
 $dbhost = "localhost";
$dbname = "photoproof";
$dbuser = "root";
$dbpass = "password";           
mysql_connect ( $dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);

mysql_query("INSERT INTO uploaded_images (uploaded_image) VALUES('$file->name') "); 

}else{

echo "1";   

}

我尝试的两种方法仍然没有发生..希望请给出一些想法来解决这个问题..提前感谢...

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用内置的CodeIgniter File Upload Class

答案 1 :(得分:0)

我通过在sa js / uplodify.php

中添加查询解决了这个问题
if (!empty($_FILES)) {
$path = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
 $admin_id = $_POST['admin_id'];
$studio_id = $_POST['studio_id'];
$client_id = $_POST['client_id'];
$file_temp = $_FILES['Filedata']['tmp_name'];
$file_name = prep_filename($_FILES['Filedata']['name']);
$file_ext = get_extension($_FILES['Filedata']['name']);
$real_name = $file_name;
$newf_name = set_filename($path, $file_name, $file_ext);
$file_size = round($_FILES['Filedata']['size']/1024, 2);
$file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES['Filedata']['type']);
$file_type = strtolower($file_type);
$targetFile =  str_replace('//','/',$path) . $newf_name;

 mysql_connect("localhost", "db_username", "db_password");
mysql_select_db("table_name");
 mysql_query("INSERT INTO uploaded_images     (admin_id,file_name,studio_id,client_id)VALUES('.$admin_id.','.$newf_name.','.$studio_id.','.$client_id.')"); 


move_uploaded_file($file_temp,$targetFile);

$filearray = array();
$filearray['file_name'] = $newf_name;
$filearray['real_name'] = $real_name;
$filearray['file_ext'] = $file_ext;
$filearray['file_size'] = $file_size;
$filearray['file_path'] = $targetFile;
$filearray['file_temp'] = $file_temp;
//$filearray['client_id'] = $client_id;

$json_array = json_encode($filearray);
echo $json_array;



}else{
echo "1";   
}