Jump to content

Problem with writing file to the right location


Elusid

Recommended Posts

So here is my code

[code]
<?php
$review = $_POST['review'];
$dir = 'reviews/';
  for ($i=0;$i<1000;$i+=1) { 
  if (!file_exists($dir . i+'')) { $file = fopen($dir . i+'', "a");
  fwrite($file, $review);break;}}
fclose($file);
echo "<meta http-equiv='refresh'

content='2;url=http://flamelicker.com/commentsuccess.html'>";
?>
[/code]

and it's writing to the root directory and not reviews/ what's wrong with it?
Ok so I am trying that right now. I assume you wanted 'DOCUMENT_ROOT' to be replaced with a URL so this is what I have now.

[code]
<?php
$review = $_POST['review'];
$dir = $_SERVER['http://flamelicker.com/media/wips/tds/'] .'/reviews';
  for ($i=0;$i<1000;$i+=1) { 
  if (!file_exists($dir . i+'')) { $file = fopen($dir . i+'', "a");
  fwrite($file, $review);break;}}
fclose($file);
echo "<meta http-equiv='refresh'

content='2;url=http://flamelicker.com/commentsuccess.html'>";
?>[/code]

EDIT and no it still writes to the root and now reviews/
no :) .. u should use exactly

$dir = $_SERVER['DOCUMENT_ROOT'] .'/reviews';

$_SERVER['DOCUMENT_ROOT'] will return the path that ur php file has it ( if u use i.e. : "c:/program files/apache group/apache/htdocs", for example). 

.'/reviews';        --> stands for the folder u want to place the uploaded file. this folder's path will be: "c:/program files/apache group/apache/htdocs/reviews"
Ok so I solved the problem on my own. It ends up that the "i" needed a $ because it's a variable woops... so here is the code that writes the file to the right place.

[code]
<?php
$dir = 'reviews/';
$review = $_POST['review'];
for ($i=0;$i<1000;$i+=1) {
    if (!file_exists($dir . $i+'')) { $file = fopen($dir . $i . '.txt', "a+");
  fwrite($file, $review); break;}}
fclose($file);

echo "<meta http-equiv='refresh'

content='2;url=http://flamelicker.com/commentsuccess.html'>";
?>[/code]

but now I have another problem. It's suposed to make the first text file writen in the folder 1 then 2 then 3 and so on. Only problem right now is that it makes file 0.txt which is right butn then I go to make a file again with this code and it's SUPOSED to make a file 1.txt but it just adds to 0.txt... what's wrong with this code?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.