Page 1 of 2

I cant access any file

Posted: Wed Apr 28, 2010 9:57 am
by Heresh
I am just finished learning php's basics.
Now I want to use functions in my php codes but I cant get then right file address! :oops:
for example, calling filesize() to get the size of one file which is in /mydir/filename.txt.
I tried this:

Code: Select all

<?php
echo filesize('/mydir/filename.txt');
echo filesize($_SERVER['DOCUMENT_ROOT'].'/mydir/filename.txt');
?>
What is wrong with my codes?

Re: I cant access any file

Posted: Wed Apr 28, 2010 10:10 am
by social_experiment
Are you receiving any errors?

Re: I cant access any file

Posted: Wed Apr 28, 2010 10:26 am
by Heresh
No, nothing shown in browser.

Re: I cant access any file

Posted: Wed Apr 28, 2010 1:00 pm
by social_experiment
You should always check if the file exists before attempting to use it.

Code: Select all

<?php
$file = 'filename.txt';  
  
  if (file_exists($_SERVER['DOCUMENT_ROOT'].'/dir_for_your_file/'.$file)) {
   echo filesize($file).' bytes';
  }
  else {
   echo 'Non-existant';	
  }
?>

Re: I cant access any file

Posted: Wed Apr 28, 2010 2:49 pm
by Heresh
Thanks for quick replay :)
I used your code. "Non-existant" is the output. but the file is there and permission of the directory is set to 777.

Re: I cant access any file

Posted: Wed Apr 28, 2010 2:56 pm
by mikosiko
are you sure about the file name?.... check if effectively it is named "filename.txt" all lowercase

Re: I cant access any file

Posted: Wed Apr 28, 2010 3:01 pm
by Heresh
mikosiko wrote:are you sure about the file name?.... check if effectively it is named "filename.txt" all lowercase
Yes I am sure. both of them are in lowercase

Re: I cant access any file

Posted: Wed Apr 28, 2010 3:07 pm
by social_experiment
Odd. Paste the code you used please.

Btw, when you call 'filesize()' and the file cannot be found you should receive an error. This is from the php manual :
Returns the size of the file in bytes, or FALSE (and generates an error of level E_WARNING) in case of an error.
.

Re: I cant access any file

Posted: Wed Apr 28, 2010 3:16 pm
by Heresh
Here is the code

Code: Select all

<?php
echo hello;
echo "<br />";
//
$file = 'filename.txt';  
 
  if (file_exists($_SERVER['DOCUMENT_ROOT'].'/mydir/'.$file)) {
   echo filesize($file).' bytes';
  }
  else {
   echo 'Non-existant';
  }

?>

Re: I cant access any file

Posted: Wed Apr 28, 2010 3:23 pm
by flying_circus
You need to be sure that you are checking the same location for both existence and filesize:

Code: Select all

<?php
  define('DS', DIRECTORY_SEPARATOR);
  $file = $_SERVER['DOCUMENT_ROOT'] . DS . 'mydir' . DS . 'filename.txt';  
 
  if (file_exists($file)) {
    echo filesize($file) . ' bytes';
  } else {
    echo 'Filse is non-existant: ' . $file;
  }
?>

Re: I cant access any file

Posted: Wed Apr 28, 2010 3:27 pm
by Heresh
hi flying_circus, Thanks for replay

wait a second ...

Re: I cant access any file

Posted: Wed Apr 28, 2010 3:28 pm
by social_experiment
Like flying_circus says :) I changed the directory to one where my filename.txt is and it works.

Re: I cant access any file

Posted: Wed Apr 28, 2010 3:31 pm
by Heresh
didnt work for me :(
same result
output is : Filse is non-existant: /var/www/html/mydir/filename.txt

Re: I cant access any file

Posted: Wed Apr 28, 2010 3:45 pm
by social_experiment

Code: Select all

<?php $file = 'filename.txt';   
  if (file_exists('/mydir/'.$file)) {
   echo filesize($file).' bytes';
  }
  else {
   echo 'Non-existant';
  }
?>
What if you remove '$_SERVER['DOCUMENT_ROOT']' ?

Re: I cant access any file

Posted: Wed Apr 28, 2010 3:48 pm
by Heresh
What if you remove '$_SERVER['DOCUMENT_ROOT']' ?
Non-existant