问题
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);
回答1:
I might be wrong (so any one let's correct me) if you have an xml and want to allow many people to read it why do you have to make multiple copies of it?!
Isn't the server supposed to do this job serving a file to many peple? If I am wrong and there is something else you try to then this php only works okay. This way you don't have to look for errors in php.
<?php
$fileName = rand().'file.xml';
$fp = fopen($fileName, 'w');
fwrite($fp, 'Hello!');
fclose($fp);
?>
<?php
$handle = fopen($fileName, "rb");
$contents = fread($handle, filesize($fileName));
print_r($contents);
fclose($handle);
?>
var winexml=loadXMLDoc("<?=$randnumb?>wine.xml");
Does <?
work for you? cause wamp asks <?PHP
(must be the php.ini)
Why do you have a second = in the loadxmldoc parameters?! Does this work:
<?PHP
$dbq="\"";
echo 'var winexml=loadXMLDoc(',$dbq,$randnumb,'wine.xml',$dbq,');';
?>
回答2:
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 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;
}
?>
来源:https://stackoverflow.com/questions/7534695/trouble-inserting-variable-as-my-url-in-php-fopen