I hate to bother you guys but I have a problem that has me stumped. When I test it on my local server it works fine, but then I uploaded the files to the web server and it doesn't work and I get the following errors:

Warning: fwrite(): supplied argument is not a valid stream resource in /DISK3/WWW/conway.cz/www/quizzes/XML/XML_doc_processing.php on line 69

Warning: fclose(): supplied argument is not a valid stream resource in /DISK3/WWW/conway.cz/www/quizzes/XML/XML_doc_processing.php on line 70

The web hosting guy tells me there's a problem with my code because I shouldn't have any problem creating an XML file in my directory, but it's just not working. Please take a look at my code and tell me if you can find a mistake that would cause such an error:

$page_sel = mysql_prep($_GET['page_sel']);
$quiz_id = mysql_prep($_GET['quiz_id']);
$quiz_id = mysql_real_escape_string($quiz_id);
$quiz_title = mysql_prep($_GET['quiz_title']);
$quiz_title = mysql_real_escape_string($quiz_title);
$dir = 'http://www.conway.cz/quizzes/XML';

$quiz_file = getNextFilename_file($dir, 'quiz', 'xml');
echo $quiz_file;

$link = "http://www.conway.cz/quizzes/quiz1.php";
$sql_quiz_id = "INSERT INTO quizzes (id, quiz_id, quiz_file, title, link) VALUES (NULL, '$quiz_id', '$quiz_file', '$quiz_title', '$link')";
$result2 = mysql_query($sql_quiz_id, $connection);

if(!is_empty_dir($dir)){
	$file = getNextFilename($dir, 'quiz', 'xml');
} else {
	$file = "http://www.conway.cz/quizzes/XML/quiz001.xml";
}

$query = "SELECT * FROM xml_docs WHERE quiz_id=\"$quiz_id\"";

$result = mysql_query($query, $connection);
if($result) {

$file_handle = fopen($file,'x');


$xmlPacket = "<?xml version='1.0'?>\n";


//----
$xmlPacket .="<StevesQuiz>\n";

while($row = mysql_fetch_array($result,MYSQL_ASSOC)){

$xmlPacket .= "\n<problem type=\"";
$xmlPacket .= $row['type_of_question']."\" questionText=\"".$row['question']."\" correctAnswers=\"";
$xmlPacket .= $row['correct_answers']."\" quizTitle=\"".$row['quiz_title']."\" quizID=\"".$row['quiz_id']."\" quizIDTaken=\"".$row['quiz_id_taken']."\">
<possible label=\"".$row['possible1']."\" />
<possible label=\"".$row['possible2']."\" />
<possible label=\"".$row['possible3']."\" />
<possible label=\"".$row['possible4']."\" />
</problem>\n";
}
$xmlPacket .= "\n</StevesQuiz>";
}
else{

$xmlPacket = "<?xml version='1.0'?><StevesQuiz></StevesQuiz>";
}

//here are the two lines that are returning errors
fwrite($file_handle,$xmlPacket);
fclose($file_handle); 
die(redirect_to('../../content_management.php?page_sel=content_management'));

Thanks for the help! It's so frustrating.
Dan

Does it matter if the server is using LINUX or not? I noticed that they use LINUX and I've never done anything with LINUX, so I'm not sure if that could be the problem or not.

You need to chmod the folder with 0777 permission where you want to fwrite.

You need to chmod the folder with 0777 permission where you want to fwrite.

Thanks for the help. Okay, so how do I do it? This is what I tried. Is this right?

$file = "http://www.conway.cz/quizzes/XML/quiz001.xml";
chmod("http://www.conway.cz/quizzes/XML/$file", 0600);

I did that (and I tried 0777) and I got a new error:
Warning: chmod() [function.chmod]: stat failed for http://www.conway.cz/quizzes/ in /DISK3/WWW/conway.cz/www/quizzes/XML/XML_doc_processing.php on line 23

Thanks again, though, for the help. Am I doing it wrong?
P

You can do chmod using your FTP client. Right click on the folder/files that you need to set permissions.

Okay, I get it, so I set the permission. It was already set to 755, but I set it to 777 as per what you said and it still doesn't work. Maybe it's something in my code? It seems bizarre that it would work on my computer but not on their server. Any other ideas?

Okay, I figured out why it's not working. It's because of this function. This function is supposed to return the next file name. In the folder is a file called "quiz001.xml'. This function is supposed to return "quiz002.xml", but it's not working for some reason. It doesn't return anything.

function getNextFilename($dir, $prefix, $type) {
  // run some security checks on the arguments supplied
  if (!is_dir($dir)) return false;
  if (!preg_match('/^[-._a-z0-9]+$/i', $prefix)) return false;
  $permittedTypes = array('txt', 'doc', 'pdf', 'jpg', 'jpeg', 'gif', 'png', 'xml');
  if (!in_array(strtolower($type), $permittedTypes)) return false;
  
  // if the checks are OK, get an array of the directory contents
  $existing = scandir($dir);
  // create a search pattern for filenames that match the prefix and type
  $pattern = '/^'.$prefix.'(\d+)\.'.$type.'$/i';
  $nums = array();
  // loop through the directory
  // get the numbers from all files that match the pattern 
  foreach ($existing as $file) {
    if (preg_match($pattern, $file, $m)) {
	  $nums[] = intval($m[1]);
	  }
	}
  // find the highest number and increase it by 1
  // if no file yet created, assign it number 1
  $next = $nums ? max($nums)+1 : 1;
  // calculate how many zeros to prefix the number with
  if ($next < 10) {
    $zeros = '00';
	}
  elseif ($next < 100) {
    $zeros = '0';
	}
  else {
    $zeros = '' ;
	}
  // return the next filename in the series
  return "{$prefix}{$zeros}{$next}.{$type}";
  }

Yeah, it's this function because when I hardwire it with the next file name manually it works fine. It creates the file in the right place. So for some reason this function is returning nothing and I don't know why.

$dir = 'http://www.conway.cz/quizzes/XML';
$quiz_file = getNextFilename_file($dir, 'quiz', 'xml');

Your directory is: http://www.conway.cz/quizzes/XML

This is a URL, not a directory path.

PHP will create a HTTP socket to that URL (if allow_url_fopen is enabled in config).
What you want is for PHP to create a PHP socket to the file system. To do this either supply the absolute path, or relative path to that directory.

eg:

$dir = '/path/to/quizes/XML';

ie: scandir() the function used in getNextFilename() is a file system function, it cannot be used with a HTTP socket.

If you chmod() its is good practice to have the least permissions possible, but still allowing PHP to write to the file/directory. This can be achieved without giving the file a permission of 777, which means anyone on the server will be able to write to that directory (which is very insecure).

Here are some good resources on file permissions on Linux and Apache:
http://www.zzee.com/solutions/linux-permissions.shtml
http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch20_:_The_Apache_Web_Server

Thanks. Yeah, I was just going to write that I figured it out. That I was using the URL and not the path. Well, I didn't figure it out; the info guy at the server told me what was up. It was driving me crazy. And thanks a lot for the file permissions stuff. I was worried that I was opening myself up to a heap of trouble by using 777 for all of my folders and files. I got it working last night and I wrote my first file. Relief. Case solved. Thanks once more.

This question has already been answered. Start a new discussion instead.