I cant access any file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Heresh
Forum Newbie
Posts: 9
Joined: Wed Apr 28, 2010 9:16 am

I cant access any file

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: I cant access any file

Post by social_experiment »

Are you receiving any errors?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Heresh
Forum Newbie
Posts: 9
Joined: Wed Apr 28, 2010 9:16 am

Re: I cant access any file

Post by Heresh »

No, nothing shown in browser.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: I cant access any file

Post 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';	
  }
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Heresh
Forum Newbie
Posts: 9
Joined: Wed Apr 28, 2010 9:16 am

Re: I cant access any file

Post 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.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: I cant access any file

Post by mikosiko »

are you sure about the file name?.... check if effectively it is named "filename.txt" all lowercase
Heresh
Forum Newbie
Posts: 9
Joined: Wed Apr 28, 2010 9:16 am

Re: I cant access any file

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: I cant access any file

Post 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.
.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Heresh
Forum Newbie
Posts: 9
Joined: Wed Apr 28, 2010 9:16 am

Re: I cant access any file

Post 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';
  }

?>
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: I cant access any file

Post 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;
  }
?>
Heresh
Forum Newbie
Posts: 9
Joined: Wed Apr 28, 2010 9:16 am

Re: I cant access any file

Post by Heresh »

hi flying_circus, Thanks for replay

wait a second ...
Last edited by Heresh on Thu Apr 29, 2010 12:32 am, edited 2 times in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: I cant access any file

Post by social_experiment »

Like flying_circus says :) I changed the directory to one where my filename.txt is and it works.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Heresh
Forum Newbie
Posts: 9
Joined: Wed Apr 28, 2010 9:16 am

Re: I cant access any file

Post by Heresh »

didnt work for me :(
same result
output is : Filse is non-existant: /var/www/html/mydir/filename.txt
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: I cant access any file

Post 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']' ?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Heresh
Forum Newbie
Posts: 9
Joined: Wed Apr 28, 2010 9:16 am

Re: I cant access any file

Post by Heresh »

What if you remove '$_SERVER['DOCUMENT_ROOT']' ?
Non-existant
Post Reply