2013-05-07 66 views
0

我試圖將圖像文件名存儲到SQL數據庫。文件名看起來像「_p4_analyzed__07001447_20121003-4000096925_Class2_reg_EPI.png」,我希望它只是「Class2_reg_EPI.png」。PHP函數縮短文件目錄字符串

我希望這條線將這樣的伎倆:$fileName = str_ireplace($casePath."/","", $file)但不幸的是,當我運行它,它仍然給我的長名與路徑(「_」下劃線充當「/」?)

查看更多信息,請參閱以下代碼:

$casePath=$path."/".$caseID."/Summary/slicesdir"; 
    global $status; 
// print("processImages case path:".$casePath."</br>"); 
    $files = glob($casePath."/*.png"); 

    $connection = getMySqlConnection(); 
    $imageCount= 0; 
    for($i = 0; $i < count($files); $i++) { 
     $file = $files[$i]; 
     $fileName = str_ireplace($casePath."/","", $file); 

     if(strripos($fileName, "grot") === false) 
     { 
      $imageCount ++; 
      //if exists 
      if(!doesImageExist($fileName, $patientID, $caseID)) { 
       $id = uniqid("", true); 
       $sql = "Insert Into images(id,patientid,caseid, image_name,comments,status) VALUES('".mysql_real_escape_string($id) 
        ."','".mysql_real_escape_string($patientID)."','".mysql_real_escape_string($caseID)."', '".mysql_real_escape_string($fileName)."',NULL,".$status[0][0].")"; 
//     print($sql."</br>"); 
       mysql_query("START TRANSACTION", $connection); 
       $result = mysql_query($sql, $connection); 
       if($result) { 
        mysql_query("COMMIT", $connection); 
//     print("Image data inserted </br>"); 
       } else { 
        mysql_query("ROLLBACK", $connection); 
        print("Image Data failed </br>"); 
       } 
      } 
     } 
    } 

任何幫助表示讚賞!提前致謝!

+1

@alchuang的路徑。 。 。爲什麼這個標記的SQL? – 2013-05-07 21:14:05

+0

你必須選擇/定義一個模式,你期望的和期望的輸出。這樣你可以使用正則表達式來重命名你的文件。 – HamZa 2013-05-07 21:21:57

+0

如果您在數據庫中放入與實際文件名不同的名稱,稍後您將如何訪問該文件? – Barmar 2013-05-07 21:44:16

回答

1

如果原文件名中包含路徑信息(例如/path/to/the/file.png),而您只需要在文件名(file.png),您可以刪除使用basename()

$filename = basename('/path/to/file.png'); 

echo $filename; // outputs 'file.png' 
+0

這是如何擺脫'_p4_analyzed__07001447_20121003-4000096925'前綴?這不是目錄信息。 – Barmar 2013-05-07 21:36:53

+0

@Bamar基於OP試圖替換文件名中的'directory'/'path',似乎他試圖刪除路徑(他提到*「'_'下劃線用作'/'」* )。我的答案可能完全是錯誤的方向,只是添加它,以防他尋找明顯的 – thaJeztah 2013-05-07 21:52:25