无法在PHP fopen()中将变量作为我的URL插入 [英] Trouble inserting variable as my URL in PHP fopen()

查看:67
本文介绍了无法在PHP fopen()中将变量作为我的URL插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的xml文件URL动态化,以便多个人可以使用我的网站并一次查询数据.我要这样做,是在xmlfile前面插入一个随机数php变量.由于某种原因,在尝试使用此变量时,我在创建和写入xml文件时遇到问题.当我使用"wine.xml"之类的静态URL时,效果很好.

I'm trying to make my xml file URL dynamic so multiple people can be using my site and querying data at once. I order to do so I'm inserting a random number php variable in front of my xmlfile. For some reason I'm having issues creating and writing xml files when trying to use this variable. When I use a static URL like 'wine.xml' it works fine.

          $fp = fopen($randnumb.'wine.xml', 'w');
           fwrite($fp, $string);
           fclose($fp);

推荐答案

好,我知道了.我不知道您对最终xml文件显示的偏好是什么,但是此脚本中的内容可能会让您完成工作,只需根据需要进行调整即可.

Okay I see. I don't know what is you preference of final xml file display, however this scripts has stuff that might let you have your job done, just adjust it to your needs.

index.html和getXml.php

index.html and getXml.php

<html>
<head>
<script type="text/javascript">

var request = false;
try { 
  request = new XMLHttpRequest(); 
} catch (trymicrosoft) {                         
  try { 
    request = new ActiveXObject("Msxml2.XMLHTTP"); 
  } catch (othermicrosoft) {
    try {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {                  
      request = false;       
    }
  }
}

if (!request) 
  alert("Error initializing XMLHttpRequest!"); 
</script>

<script type="text/javascript">

 var fileOption;
 var fileName;

   function runPhp(makeFile) 
   {  
        var url = "getXml.php"; 
        fileOption = makeFile;
        var params = "makeFile=" +makeFile+"";
        request.open("POST", url, true);  

        //Some http headers must be set along with any POST request.
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
        request.setRequestHeader("Content-length", params.length);
        request.setRequestHeader("Connection", "close");

        request.onreadystatechange = updatePage;
        request.send(params);

   }////////////////////

   function getXml( ) 
   {
        if(fileName==null){alert('please click create file first');return;}
        var url = fileName; 
        var params = null; 
        request.open("POST", url, true);     
        request.setRequestHeader("Connection", "close");    
        request.onreadystatechange = displayFile;
        request.send(params); 
   }////////////////////

   //You're looking for a status code of 200 which simply means okay.
   function updatePage() {
     if (request.readyState == 4) {
       if (request.status == 200) 
       {   
            if(fileOption==1)  
                {fileName=request.responseText;  return;}
            document.getElementById('divResults').innerHTML=request.responseText;
            document.getElementById('textareaResults').innerHTML=request.responseText;   
       } 
       else{
         //alert("status is " + request.status);
         }
     }
   }

      function displayFile() {
     if (request.readyState == 4) {
       if (request.status == 200) 
       {    
            document.getElementById('textareaResults').innerHTML=request.responseText;
            document.getElementById('divResults').innerHTML='File loaded in text area above.';
       } 
       else{
         //alert("status is " + request.status);
         }
     }
   }

</script>
</head>
<body >


<span style="background-color:blue;color:yellow;"  
onClick="runPhp(0)"/>
Click for Xml Results.<br>
(<font color=pink>I prefer this one!!!</font>)
</span><br><br>

<span style="background-color:blue;color:yellow;"  
onClick="runPhp(1)"/>
Click to create an xml file.<br> 
</span>

<span style="background-color:blue;color:yellow;"  
onClick="getXml(1)"/>
Click to read the xml file.<br> 
</span>

<textarea rows="10" cols="88"  id="textareaResults">
</textarea>
 <br><br>
<pre><div    id="divResults"></div></pre>
<br><br>

</body>
</html>


<?PHP
mysql_connect('localhost', 'root',''); 
mysql_select_db("mysql");
$query="select * from help_category;"; 

$resultID = mysql_query($query ) or die("Data not found."); 

  $xml_output = "<?xml version=\"1.0\"?>\n"; 
$xml_output .= "<records>\n"; 

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 
    $row = mysql_fetch_assoc($resultID); 
    $xml_output .= "\t<record>\n"; 
    $xml_output .= "\t\t<help_category_id>" . $row['help_category_id'] . "</help_category_id>\n";  
    $xml_output .= "\t\t<name>" . $row['name'] . "</name>\n";  
    $xml_output .= "\t\t<parent_category_id>" . $row['parent_category_id'] . "</parent_category_id>\n"; 
    $xml_output .= "\t</record>\n"; 
} 

$xml_output .= "</records>"; 

if($_POST['makeFile']==0)
    echo $xml_output;
else
    {
$fileName = rand().'file.xml';
$fp = fopen($fileName, 'w');
fwrite($fp,  $xml_output); 
fclose($fp);
$dbq="\"";  
echo $fileName;
}

?> 

这篇关于无法在PHP fopen()中将变量作为我的URL插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆