2016-06-08 14 views
0
$uniqId = $this->input->post('U_I_T_Roll_No'); 
$config['upload_path'] = "./uploads/ProfileImages/"; 
     $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg'; 
     $config['max_size'] = '1024'; 
     $config['max_width'] = '1000'; 
     $config['max_height'] = '1000'; 
     /* Load the upload library */ 
     $this->load->library('upload', $config); 
     /* Create the config for image library */ 
     /* (pretty self-explanatory) */ 
     $configThumb = array(); 
     $configThumb['image_library'] = 'gd2'; 
     $configThumb['source_image'] = ''; 
     $configThumb['create_thumb'] = FALSE; 
     $configThumb['maintain_ratio'] = TRUE; 
     /* Set the height and width or thumbs */ 
     /* Do not worry - CI is pretty smart in resizing */ 
     /* It will create the largest thumb that can fit in those dimensions */ 
     /* Thumbs will be saved in same upload dir but with a _thumb suffix */ 
     /* e.g. 'image.jpg' thumb would be called 'image_thumb.jpg' */ 
     $configThumb['width'] = 400; 
     $configThumb['height'] = 400; 
     /* Load the image library */ 
     $this->load->library('image_lib'); 

    /* We have 5 files to upload 
    * If you want more - change the 6 below as needed 
    */ 
    for($i = 1; $i < 5; $i++) { 
    /* Handle the file upload */ 
    $upload = $this->upload->do_upload('image'.$i); 
    /* File failed to upload - continue */ 
    if($upload === FALSE) continue; 
    /* Get the data about the file */ 
    $data = $this->upload->data(); 

    $uploadedFiles[$i] = $data; 
    /* If the file is an image - create a thumbnail */ 
    if($data['is_image'] == 1) { 
     $configThumb['source_image'] = $data['full_path']; 
     $this->image_lib->initialize($configThumb); 
     $this->image_lib->resize(); 
    } 
    } 
    $S_image = $_FILES['image1']['name']; 
    $F_image = $_FILES['image2']['name']; 
    $M_image = $_FILES['image3']['name']; 
    $LG_image = $_FILES['image4']['name']; 
    $data5=array (
    'Uniq_Id' => $this->input->post('U_I_T_Roll_No'), 

    'S_image' => $S_image, 
    'F_image' => $F_image, 
    'M_image' => $M_image, 
    'LG_unique1' => $LG_image, 
    ); 


    $this->InsertData->studentimageupload($data5); 

Der obige Code funktioniert gut. Ich habe 4 Bild zum Hochladen und Hochladen richtig und den Namen des Bildes speichern in der Datenbank. das Problem ist die.Dateiname beim Hochladen umbenennen

Ich möchte Bildname nach mir hochladen und den Namen des Bildes in der Datenbank speichern.

Like: S $ uniqid, F $ uniqid, M $ uniqid, LG $ uniqid

Antwort

0

Sie so etwas wie dies vor $this->load->library('upload', $config);

ändern $this->load->library('upload', $config);-$this->load->library('upload'); und siehe unten

for($i = 1; $i < 5; $i++) { 
    $img=$_FILES['image'.$i]['name']; 
    $new_name=NEW_NAME; 
    $ext = strtolower(substr($img, strpos($img,'.'), strlen($img)-1)); 
    $file_name=$new_name.$ext; 
    $config['file_name'] = $file_name; 
$this->upload->initialize($config); 
/* Handle the file upload */ 
$upload = $this->upload->do_upload('image'.$i); 
/* File failed to upload - continue */ 
if($upload === FALSE) continue; 
/* Get the data about the file */ 
$data = $this->upload->data(); 

$uploadedFiles[$i] = $data; 
/* If the file is an image - create a thumbnail */ 
if($data['is_image'] == 1) { 
    $configThumb['source_image'] = $data['full_path']; 
    $this->image_lib->initialize($configThumb); 
    $this->image_lib->resize(); 
    } 
    } 
+0

eigentlich habe ich 4 Bilder zum hochladen. Kannst du es in Array .. und wie man alle fünf Bilder benennen .. Sie haben den Code für nur ein Bild hochgeladen. ich muss e mal schreiben .. –

+0

mache das selbe für alle 5 oder mache eine funktion. – Vinie

+0

Siehe aktualisierte Antwort – Vinie

0

sollten Sie in der Lage sein, den 'file_name' angeben, nachdem die Bibliothek Laden & direkt vor dem Hochladen Datei ..

//make your config array. 
$this->load->library('upload', $config); 

for($i=1; $i < 5; $i++) 
{ 
    $this->upload->set_filename($path, $filename); 
    $upload = $this->upload->do_upload('image'. $i); 
    //do whatever you want to do with the file. 
} 

Ich habe es in keinem meiner Projekte bisher verwendet, aber es sollte das gewünschte Ergebnis produzieren. Lass es mich wissen, wenn es nicht funktioniert ..

+0

können Sie den Code bitte bearbeiten .. –

+0

Ich habe meine Antwort mit Beispielcode aktualisiert .. versuchen Sie es. –

0

Der Name verwendet wird, was auch immer in $_FILES['image1']['name'] vorhanden ist. Wenn Sie es ändern möchten, können Sie es im Upload-Formular selbst ändern. Ist dies nicht erwünscht ist, haben Sie rename() rufen und dann aktualisieren

$S_image = $_FILES['image1']['name']; 
$F_image = $_FILES['image2']['name']; 
$M_image = $_FILES['image3']['name']; 
$LG_image = $_FILES['image4']['name']; 

, was auch immer Sie die Dateien umbenannt haben, bevor in der Datenbank zu speichern.

0

Ich denke, Sie können das Umbenennen Problem lösen, indem Sie die Konfiguration jedes Mal initialisieren, wenn Sie die Methode do_upload aufrufen.

$config['file_name'] = YOUR_OWN_NAME

und initialisieren dann in Ihre for-Schleife, kurz vor Ihrem do_upload Anruf

$this->upload->initialize($config);

+0

können Sie bitte den Code bearbeiten und zeigen Sie mir heiß zu tun. Es gibt eine for-Schleife, ich kann nicht verstehen, wie man mehr als ein Bild umbenennt. –