Jump to content


Photo

Problem with writing file to the right location


  • Please log in to reply
4 replies to this topic

#1 Elusid

Elusid
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 07 September 2006 - 12:42 AM

So here is my 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'>"; 
?>

and it's writing to the root directory and not reviews/ what's wrong with it?

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 07 September 2006 - 12:50 AM

Try to define teh path explicitly i.e. $dir = $_SERVER['DOCUMENT_ROOT'] .'/reviews';

That is assuming the reviews dir is in teh root dir.
follow me on twitter @PHPsycho

#3 Elusid

Elusid
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 07 September 2006 - 01:03 AM

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.

<?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'>"; 
?>

EDIT and no it still writes to the root and now reviews/

#4 live_ex3me

live_ex3me
  • Members
  • PipPip
  • Member
  • 15 posts
  • LocationRomania

Posted 07 September 2006 - 05:31 AM

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"
There is no place like 127.0.0.1 !

#5 Elusid

Elusid
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 08 September 2006 - 01:42 AM

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.

<?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'>"; 
?>

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users