2012-12-29 60 views
-2

我如何將上傳的文件重命名爲str_replace function?我想刪除space_。任何幫助嗎?我使用下面的代碼到文件上傳文件中的str_replace函數

$file1 = 'order_'.$orderId.'_'.$rowsOrder['image_a']; 
$file2 = 'order_'.$orderId.'_'.$rowsOrder['image_b']; 
$file3 = 'order_'.$orderId.'_'.$rowsOrder['image_c']; 
$file4 = 'order_'.$orderId.'_'.$rowsOrder['image_d']; 
$path1 = 'http://www.yourdomain.com/'.$file1; 
$path2 = 'http://www.yourdomain.com/'.$file2; 
$path3 = 'http://www.yourdomain.com/'.$file3; 
$path4 = 'http://www.yourdomain.com/'.$file4; 

 <tr>"; 
       if($rowsOrder['image_a'] !=''){ 
       $message.="<td align='left' style='font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:#000000;'><a href='http://www.yourdomain.com/files/".$file1."' style='font-family:Times New Roman, Times, serif; font-size:12px; color: #000000; font-weight: normal;' title='http://www.yourdomain.com/'>File1</a></td>";} 
       if($rowsOrder['image_b'] !=''){ 

       $message.= "<td align='left' style='font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:#000000;'><a href='http://www.yourdomain.com/files/".$file2."' style='font-family:Times New Roman, Times, serif; font-size:12px; color: #000000; font-weight: normal;' title='http://www.yourdomain.com/'>File2</a> </td>";} 


      $message.= "</tr><tr>"; 


      if($rowsOrder['image_c'] !=''){ 
       $message.= "<td align='left' style='font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:#000000;'><a href='http://www.yourdomain.com/files/".$file3."' style='font-family:Times New Roman, Times, serif; font-size:12px; color: #000000; font-weight: normal;' title='http://www.yourdomain.com/'>File3</a></td>";} 
       if($rowsOrder['image_d'] !=''){ 
       $message.= "<td align='left' style='font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:#000000;'><a href='http://www.yourdomain.com/files/".$file4."' style='font-family:Times New Roman, Times, serif; font-size:12px; color: #000000; font-weight: normal;' title='http://www.yourdomain.com/'>File4</a> </td>";} 
       $message.= "</tr><tr> 
         <td colspan='2' align='left' style='font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:#000000;font-weight: bolder;'><br><br>Thanks,<br><br> 
          <span style='font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:#000000; font-weight: normal;'><a href='http://www.yourdomain.com/' style='font-family:Times New Roman, Times, serif; font-size:12px; color: #000000; font-weight: normal;' title='http://www.yourdomain.com/'>yourdomain</a></span></td></tr> 

$order_image_a='order_'.$orderId.'_'.$filea; 
if(!empty($filea)) move_uploaded_file($_FILES['FILE1']['tmp_name'], "../files/$order_image_a"); 

$order_image_b='order_'.$orderId.'_'.$fileb; 
if(!empty($fileb)) move_uploaded_file($_FILES['FILE2']['tmp_name'], "../files/$order_image_b"); 

$order_image_c='order_'.$orderId.'_'.$filec; 
if(!empty($filec)) move_uploaded_file($_FILES['FILE3']['tmp_name'], "../files/$order_image_c"); 

$order_image_d='order_'.$orderId.'_'.$filed; 
if(!empty($filed)) move_uploaded_file($_FILES['FILE4']['tmp_name'], "../files/$order_image_d"); 

如果我上傳的圖像或在其名稱間隙內的任何文件上傳,它應該_中上傳缺口位置(例如 - you files.doc應該由you_files.doc上傳)。可能嗎?

+0

這就是'str_replace'或'strtr'的意思......當然,正確的做法是當你爲'move_uploaded_file'生成最終文件名時,而不是在輸出時所述文件名。 – DCoder

+0

用上傳的文件代碼編輯我的問題。你可以請現在建議在哪裏添加功能,使其正常工作? – user1859847

+0

我不是像'_a'到'_d'這樣的複製粘貼代碼複製的巨大粉絲。數組通常更方便。您發佈的代碼不會顯示'$ filea'通過'$ filed'獲取它們的值的方式,因此回答您的問題有點難。 – DCoder

回答

0
function convertSpecialChars($str) { 
    $str = str_replace(" ", "_", $str); 
    return $str; 
} 

$filea = $this->convertSpecialChars($filea); 

$order_image_a='order_'.$orderId.'_'.$filea; 
if(!empty($filea)) move_uploaded_file($_FILES['FILE1']['tmp_name'], "../files/$order_image_a");