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
Post
by Heresh » Wed Apr 28, 2010 9:57 am
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!
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?
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Wed Apr 28, 2010 10:10 am
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
Post
by Heresh » Wed Apr 28, 2010 10:26 am
No, nothing shown in browser.
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Wed Apr 28, 2010 1:00 pm
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
Post
by Heresh » Wed Apr 28, 2010 2:49 pm
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
Post
by mikosiko » Wed Apr 28, 2010 2:56 pm
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
Post
by Heresh » Wed Apr 28, 2010 3:01 pm
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
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Wed Apr 28, 2010 3:07 pm
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
Post
by Heresh » Wed Apr 28, 2010 3:16 pm
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';
}
?>
flying_circus
Forum Regular
Posts: 732 Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR
Post
by flying_circus » Wed Apr 28, 2010 3:23 pm
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
Post
by Heresh » Wed Apr 28, 2010 3:27 pm
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.
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Wed Apr 28, 2010 3:28 pm
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
Post
by Heresh » Wed Apr 28, 2010 3:31 pm
didnt work for me
same result
output is : Filse is non-existant: /var/www/html/mydir/filename.txt
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Wed Apr 28, 2010 3:45 pm
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
Post
by Heresh » Wed Apr 28, 2010 3:48 pm
What if you remove '$_SERVER['DOCUMENT_ROOT']' ?
Non-existant