上传文件时的str_replace函数

时间:2012-12-29 17:25:00

标签: php

如何使用str_replace function重命名上传的文件?我想删除space _。有帮助吗?我在CODE下面使用上传文件

$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)。可能?

1 个答案:

答案 0 :(得分: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");