Php 在本地主机上从word文件上载图像工作正常,但在服务器上无法正常工作
我通过以下步骤完成了这项工作:Php 在本地主机上从word文件上载图像工作正常,但在服务器上无法正常工作,php,file-upload,ms-word,Php,File Upload,Ms Word,我通过以下步骤完成了这项工作: 我编写了读取MS word文件的代码,其中包含图像 从xml代码中提取文件路径后,我成功地将图像上载到本地服务器上。xml文件示例:-/; foreach($rre as$key=>$rrevalue){ 如果($key!=0){ $remaks=爆炸(“备注:”,$rrevalue); $hint=explode(“Exp:”,$remaks[0]);//根据提示进行分解。 $ans=分解(“ans-”,$hint[0]);//从ans分解。 $option=e
/;
foreach($rre as$key=>$rrevalue){
如果($key!=0){
$remaks=爆炸(“备注:”,$rrevalue);
$hint=explode(“Exp:”,$remaks[0]);//根据提示进行分解。
$ans=分解(“ans-”,$hint[0]);//从ans分解。
$option=explode(“^@@@^,$ans[0]);//从带问题的选项中分解。
preg_match_all($pattern2,$hint[1],$hintmatches);
//提示中的所有图像将保存在数组中,然后保存在字段中
if(is_数组($hintmatches)){
foreach($hintmatches[1]作为$key=>$val){
$str=str\u replace(“\\\”、“/”、$val);
如果(文件_存在($str)){
$imag1=rand().basename($str);
$sestination=“../question_image/”;
副本($str,$sestitation.“$imag1);
$images\u hint[]=$imag1;
}
}
if(is_数组($images_hint)){
$imagesHint=内爆(“| |”,$images_hint);
$images\u hint='';
}
}
foreach($选项为$keys=>$valuew){
preg_match_all($pattern2,$valuew,$matches2);
//该部分中的所有图像(如问题和选项)将保存在数组中,然后保存在字段中
if(是_数组($matches2[1])){
foreach($matches2[1]作为$key=>$val){
$str=str\u replace(“\\\”、“/”、$val);
如果(文件_存在($str)){
$imag2=rand().basename($str);
$sestination=“../question_image/”;
副本($str,$sestitation.“$imag2);
$imagesArray[]=$imag2;
}
}
if(是_数组($imagesArray)){
$imag=内爆(“| |,$imagesArray”);
$imagesArray='';
}
}
$valuew=带标签($valuew,“”);
如果($keys==0){
//$str11=substr($valuew,'2');旧的
$newQuesion=explode(“.”,$valuew);
//
$num=0;
如果($newQuesion!=''){
$hint=strip_标记($hint[1],“”);
$quesionId=uploadQuestion($cvsId,$value,$scheduleId,addslashes($newQuesion[1]),$hint,$imag,$imagesHint,$imagesHint,$remaks[1]);//保存问题
$imagesHint='';
}
}否则{
uploadOption($quesionId,addslashes($valuew),$keys,$imag);//保存选项
}
$num++;$imag=“”;
}
如果($ans[1]!=''){
uploadAnswer($quesionId,$ans[1]);//保存答案
}
}
}
}
函数read_file_docx($filename){
$striped_content='';
$content='';
如果(!$filename | |!file_存在($filename))
返回false;
$zip=zip\u open($filename);
如果(!$zip | |是数字($zip))
返回false;
而($zip\u entry=zip\u read($zip)){
如果(zip_entry_open($zip,$zip_entry)==FALSE)继续;
如果(zip\u entry\u name($zip\u entry)!=“word/document.xml”)继续;
$content.=zip_entry_read($zip_entry,zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
}
文件内容('question.xml',$content);
$content2=爆炸(“”,$content);
$pattern=“/(**?/”;
preg_match_all($pattern、$content、$matches);
$var=array_map(“strip_tags_sub”,$matches[1]);
foreach($content2作为$key=>$value)
{
如果($key!=0){
$var1=带标签($var[$key]);
$content=str_replace(“”.$var1.”,$var[$key],$content);
}
}
$content1=爆炸(“”,$content);
$pattern=“/(**?/”;
preg_match_all($pattern、$content、$matchesa);
$var1=数组映射(“strip_tags_sup”,$matchesa[1]);
foreach($content1作为$key=>$value)
{
如果($key!=0){
$varq=strip_标签($var1[$key]);
$content=str_replace(“”.$varq.”,$var1[$key],$content);
}
}
$content=str_replace(“”,$content);
$content=str_replace(“”,$content);
$allData=strip_标签($content',);
$allData=str_replace(‘(Q’,“^@@@^,$allData);//从问题开始计算
$allData=str_replace('1)。“^@@@^,$allData);//从选项开始计算
$allData=str_replace('2)。“^@@@^,$allData);//从选项开始计算
$allData=str_replace('3.),“^@@@^,$allData);//从选项开始计算
$allData=str_replace('4)。“^@@@^,$allData);//从选项开始计算
$allData=str_replace('5)。“^@@@^,$allData);//从选项开始计算
$allData=str_replace('6)。“^@@@^,$allData);//从选项开始计算
返回$allData;
}
这里的问题是,您试图复制()一个在客户端上有路径的文件,但复制()在服务器上运行。
由于服务器上不存在该文件,您将无法复制它。
$pattern2 = '/descr="(.*?)"\/>/';
$str = str_replace('\\', '/', $val);
copy($str,$imagepath);
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 ;
}