I have a PHP script to generate a sitemap: http://w3db.org/sitemapx.php

It's job is to make the sitemap1.xml file in the root directory, but it just shows the number of urls added.

It does not give an error. The script is OK but there is something wrong with permissions. I tried it using 777 but it is still not able to create the sitemap1.xml file in root directory.

sitemapx.php:

<?php

  global $handle, $counter, $file_number, $filename ,$countx;

  $counter = 0;
  $countx = 0;
  $file_number = 1;

  include('config.php');    

  $con = mysql_connect("$db_host","$db_username","$db_password");
  mysql_select_db("$database", $con);

  $result = mysql_query("SELECT * FROM table_name");
  while($row = mysql_fetch_array($result)) {
    $domx = $row["domain"];
    $url = "http://www.w3db.org/www.".$domx."";
    write_xml($url);
    $countx++;
  }
  fwrite($handle,"</urlset>\n");

  echo "Url to Sitemap Added : ".$countx;

  function write_xml($url,$priority = 0.7) {
    global $handle, $counter, $file_number, $filename;
    if ($counter >= 50000) {
      $file_number++;
      $counter = 0;
      fwrite($handle,"</urlset>\n");
      fclose($handle);
    }
    if ($counter == 0) {
      $filename = "sitemap" . $file_number . ".xml";
      $handle = fopen($filename,"a");
      fwrite($handle,'<?xml version="1.0" encoding="UTF-8"?>' . "\n");
      fwrite($handle,'<urlset xmlns="http://www.google.com/schemas/sitemap/0.9">'."\n");
    }
    if ($url != "" && $url != "END") {
      fwrite($handle,"\t <url>\n");
      fwrite($handle,"\t\t<loc>$url</loc>\n");
      fwrite($handle,"\t\t<priority>$priority</priority>\n");
      fwrite($handle,"\t</url>\n");
    }
    if ($url == "") {
      fwrite($handle,"</urlset>\n");
      fclose($handle);
    }
    $counter++;
  }

?>
link|improve this question
The source of sitemapx.php would help a bit. – ott-- Jan 3 at 13:43
What makes you assume that this is a permissions error? – Dan Jan 3 at 13:51
because on my old host host the same script was working great .. – user889030 Jan 3 at 14:13
How did this not get migrated? – Tim Jan 3 at 16:51
@Tim because the OP thinks it's an error he/she could solve changing something on the system (security/permissions). – splattne Jan 3 at 17:51
feedback

1 Answer

Here is your script written in a tidier way, with more error handling. Try it out and see if it fixes the problem, and if not, what the error message you see is.

<?php

  // Calling "global" in the global scope is pointless - anything declared here is already implicitly global

  // Rather than importing global variables into a function, it's better to delare them in the global scope
  // and pass them as arguments if necessary. I've also separated the logic into three functions.
  function open_xml ($fileName) {
    // Opens a file pointer for the XML document and writes the header
    // We should use 'w' as the mode here, not 'a'. If you append to the file, you'll create invalid XML
    // documents with multiple root nodes when the file already exists. 'w' will overwrite it.
    return (($fp = fopen($fileName, 'w')) && fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.9\">\n")) ? $fp : FALSE;
  }
  function write_xml ($fp, $url, $priority = 0.7) {
    // Writes a <url> element to the XML file
    return is_resource($fp) && fwrite($fp, "\t<url>\n\t\t<loc>$url</loc>\n\t\t<priority>$priority</priority>\n\t</url>\n");
  }
  function close_xml ($fp) {
    // Writes the footer for the XML document and closes the file pointer
    return is_resource($fp) && fwrite($fp, "</urlset>\n") && fclose($fp);
  }

  // Get config
  include('config.php');    

  // Connect to MySQL and select database
  if ((!$con = mysql_connect($db_host, $db_username, $db_password)) || !mysql_select_db("$database", $con))
    exit("Unable to initialise MySQL: ".mysql_error());

  // Open an XML file
  $fileNumber = 1;
  $fileName = "sitemap{$fileNumber}.xml";
  $filePath = realpath($fileName);
  if (!$fp = open_xml($fileName))
    exit("Unable to open '$fileName' ($filePath) for writing!");

  // Get database results
  $result = mysql_query("SELECT * FROM table_name", $con);

  // Loop the results and write them to file
  for ($current = 0, $total = 0; $row = mysql_fetch_assoc($result); $current++, $total++) {

    if ($current >= 50000) {

      // Close the current file
      if (!close_xml($fp))
        exit("Unable to close '$fileName' ($filePath)");

      // Open a new file
      $fileNumber++;
      $fileName = "sitemap{$fileNumber}.xml";
      $filePath = realpath($fileName);
      if (!$fp = open_xml($fileName))
        exit("Unable to open '$fileName' ($filePath) for writing");

      // Reset the counter
      $current = 0;

    }

    // Write this URL to file
    if (!write_xml($fp, "http://www.w3db.org/www.{$row['domain']}"))
      exit("Unable to write to '$fileName' ($filePath)");

  }

  // Close off the XML file
  if (!close_xml($fp))
    exit("Unable to close '$fileName' ($filePath)");

  echo "URLs added to Sitemap : $total";
link|improve this answer
its give error ( Unable to open 'sitemap1.xml' () for writing! ) what if it create sitemap1.xml and then after 40,000 urls it create sitemap2.xml ...... so please update it little i will be thankful to you – user889030 Jan 3 at 15:21
What exactly does the error say? Unable to open or write? – DaveRandom Jan 3 at 15:22
i made manually sitemap1.xml in my root directory but it still say Unable to open 'sitemap1.xml' (/home/admin/w3db/sitemap1.xml) for writing! – user889030 Jan 3 at 15:28
It does sound like permissions in that case - do you have 777 permissions all the way up the directory hierarchy? I.e. on /home, /home/admin ... /home/admin/w3db/sitemap1.xml? – DaveRandom Jan 3 at 15:48
thanks :) ya i forget to set permission to 777 its working now thanks :) .... – user889030 Jan 3 at 16:05
show 5 more comments
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.