2014-09-27 81 views
1

我所做的工作在以下步驟:上傳圖像正在正確的,但不能在服務器

  1. 我寫的代碼讀取MS Word文件,在它的形象。

  2. 從XML代碼中提取文件路徑後,我在本地服務器上成功上傳圖像。 xml文件示例: - <pic:cnvpr id="0" name="Picture 4" descr="C:\Users\Administrator\Documents\tshirt\14714-yepme-s-275x340-imadpnn23zqpjqzz.jpeg"</pic:cnvpr>

  3. 當我上傳服務器上的word文件並從不同PC上的服務器上下載它時,就會出現與啓動時一樣的圖像文件。

  4. 但是現在,當我從服務器上傳文件文件時,顯示本地服務器和圖像的相同路徑未上傳的文件。

我的問題是:如何上傳服務器上的word文件圖像?

讀取XML文件的描述後,我只是複製圖像像這樣

$pattern2 = '/descr="(.*?)"\/>/'; 
$str = str_replace('\\', '/', $val); 
copy($str,$imagepath); 

換句話說,我想在服務器上的文件從我的電腦上傳的,我有一個文件的本地路徑。

這裏就是我用read_file_docx功能爲docx文件轉換成xml一些分隔符的完整代碼:

if($_FILES["file"]["name"][$value]!='' && ($ext=='docx' || $ext=='doc')){ 
    $rand=time(); 
    $fileName=$scheduleId."_".$value."_".$rand.".doc"; 
    $cvsId=uploadCsvForQuestion($value,$scheduleId,$fileName,$rand); 
    copy(
     $_FILES["file"]["tmp_name"][$value], 
     "../question_csv/" .$fileName 
    ); // file save in folder 

    //extract data from doc file 
    $allData=read_file_docx($_FILES["file"]["tmp_name"][$value],"r"); 

    // explode from question 
    $rre=explode("^@@@^",$allData); 

    //print_r($allData);exit; 

    $pattern2 = '/descr="(.*?)"\/>/'; 

    foreach($rre as $key=>$rrevalue){ 
     if($key!=0){ 
      $remaks=explode("Remarks:",$rrevalue); 
       $hint=explode("Exp:",$remaks[0]); //explode from hint. 

      $ans=explode("Ans-",$hint[0]);//explode from ans. 

      $option=explode("^@@^",$ans[0]);//explode from options with question. 
      preg_match_all($pattern2, $hint[1], $hintmatches); 

      // All images in hint will save in array then save in field 
       if(is_array($hintmatches)){ 
       foreach($hintmatches[1] as $key=>$val){ 
       $str = str_replace('\\', '/', $val); 
       if(file_exists($str)){ 
        $imag1=rand().basename($str); 
        $sestination="../question_image/"; 
        copy($str,$sestination."".$imag1); 
        $images_hint[]=$imag1; 
       } 
      } 
      if(is_array($images_hint)){ 
       $imagesHint=implode("||",$images_hint); 
       $images_hint=''; 
      } 
     } 

     foreach ($option as $keys=>$valuew) { 

      preg_match_all($pattern2, $valuew, $matches2); 
      // All images in that section like question and option will save in array then save in field         

      if(is_array($matches2[1])){ 
           foreach($matches2[1] as $key=>$val){ 
            $str = str_replace('\\', '/', $val); 
            if(file_exists($str)){ 
             $imag2=rand().basename($str); 
             $sestination="../question_image/"; 
             copy($str,$sestination."".$imag2); 
             $imagesArray[]=$imag2; 
            } 
           } 
           if(is_array($imagesArray)){ 
             $imag=implode("||",$imagesArray); 
             $imagesArray=''; 
           } 

          } 
          $valuew = strip_tags($valuew,"<sub><sup>"); 
          if($keys==0){ 
           //$str11= substr($valuew,'2'); old one 
           $newQuesion=explode(".",$valuew); 
           // 
           $num=0; 
           if($newQuesion!=''){ 
            $hint = strip_tags($hint[1],"<sub><sup>"); 
            $quesionId=uploadQuestion($cvsId,$value,$scheduleId,addslashes($newQuesion[1]),$hint,$imag,$imagesHint,$imagesHint,$remaks[1]);//save question 
            $imagesHint=''; 
           } 
          }else{ 

           uploadOption($quesionId,addslashes($valuew),$keys,$imag);//save option 
          } 
          $num++; $imag=""; 

         } 
         if($ans[1]!=''){ 
          uploadAnswer($quesionId,$ans[1]);//save answer 
         } 
        } 
       } 

}   


function read_file_docx($filename){ 
    $striped_content = ''; 
    $content = ''; 
     if(!$filename || !file_exists($filename)) 
     return false; 
     $zip = zip_open($filename); 
      if (!$zip || is_numeric($zip)) 
     return false; 
    while ($zip_entry = zip_read($zip)) { 
      if (zip_entry_open($zip, $zip_entry) == FALSE) continue; 
      if (zip_entry_name($zip_entry) != "word/document.xml") continue; 
      $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); 
      zip_entry_close($zip_entry); 

     } 
     file_put_contents('question.xml', $content); 
     $content2 = explode('<w:vertAlign w:val="subscript"/></w:rPr><w:t>', $content); 
     $pattern = "/<w:vertAlign w:val=\"subscript\"\/><\/w:rPr><w:t>(.*?)<\/w:t><\/w:r>/"; 
     preg_match_all($pattern, $content, $matches); 
     $var=array_map("strip_tags_sub",$matches[1]); 
     foreach($content2 as $key=>$value) 
     { 
      if($key!=0){ 
       $var1=strip_tags($var[$key]); 
       $content=str_replace('<w:vertAlign w:val="subscript"/></w:rPr><w:t>'.$var1."</w:t></w:r>", $var[$key],$content); 
      } 
     } 
     $content1 = explode('<w:vertAlign w:val="superscript"/></w:rPr><w:t>', $content); 
     $pattern = "/<w:vertAlign w:val=\"superscript\"\/><\/w:rPr><w:t>(.*?)<\/w:t><\/w:r>/"; 
     preg_match_all($pattern, $content, $matchesa); 
     $var1=array_map("strip_tags_sup",$matchesa[1]); 
     foreach($content1 as $key=>$value) 
     { 
      if($key!=0){ 
       $varq=strip_tags($var1[$key]); 
       $content=str_replace('<w:vertAlign w:val="superscript"/></w:rPr><w:t>'.$varq."</w:t></w:r>", $var1[$key],$content); 
      } 
     } 
     $content = str_replace('</w:r></w:p>', " ", $content); 
     $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content); 
     $allData = strip_tags($content,'<pic:cNvPr><sub><sup>'); 
     $allData = str_replace('(Q', "^@@@^", $allData);//saperate from question 
     $allData = str_replace('1).', "^@@^", $allData);//saperate from option 
     $allData = str_replace('2).', "^@@^", $allData);//saperate from option 
     $allData = str_replace('3).', "^@@^", $allData);//saperate from option 
     $allData = str_replace('4).', "^@@^", $allData);//saperate from option 
     $allData = str_replace('5).', "^@@^", $allData);//saperate from option 
     $allData = str_replace('6).', "^@@^", $allData);//saperate from option 
     return $allData ; 
    } 
+1

您將需要顯示您正在使用的代碼。 – 2014-09-28 16:13:55

+0

你可以發佈你的代碼嗎? – 2014-10-06 06:55:47

+0

我用完整的代碼編輯了這個問題 – vinod 2014-10-06 07:43:35

回答

0

這裏的問題是,你試圖複製()一個文件的路徑在客戶端上,但copy()在服務器上運行。 由於該文件不存在於服務器上,因此您將無法複製該文件。它在本地主機上工作,因爲客戶端和服務器只是相同的文件系統。

你可以用Javascript這樣的客戶端代碼來實現它,在這個代碼中你將提交一個帶有你提取的本地文件路徑的上傳表單,服務器端PHP代碼將處理上傳的圖像。

+0

我在編碼方面要求解決方案。我知道這個問題就是這個。但是如果在問題的每個選項中出現多個圖像,如何克服? – vinod 2014-10-11 11:34:23

+0

您必須單獨處理每個圖像路徑並上傳到服務器 - 您可以讀取該文件並將其以base64編碼發送到帶有js的服務器,例如 – omercnet 2014-10-13 09:05:35

1

從localhost路徑更改二進制數據中的圖像,然後從服務器上的二進制數據中獲取圖像。

+0

是的它看起來很酷.. :) – vinod 2014-10-11 12:01:08

+1

您仍然必須實現這在本地主機上而不在服務器上 – omercnet 2014-10-13 09:04:27

相關問題