Jump to content

Archived

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

Elusid

Problem with writing file to the right location

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?

Share this post


Link to post
Share on other sites
Try to define teh path explicitly i.e. $dir = $_SERVER['DOCUMENT_ROOT'] .'/reviews';

That is assuming the reviews dir is in teh root dir.

Share this post


Link to post
Share on other sites
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/

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

×

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.