Ι have somewhere located in my pc a txt file, let's say for example C:\Users\my_file.txt. Is there a way to print its size? As I search it, I am given:
$filename = 'somefile.txt';
echo $filename . ': ' . filesize($filename) . ' bytes';
But what path should I give to filename variable?
In Linux, the path separator is /. In Windows, it is either \, / or \\.
I personally use front-slashes / on both OS so I never get confused.
Below you can find 3 valid full paths on Windows, i.e.:
C:/Users/my_file.txt
or
C:\\Users\\my_file.txt
or simply:
C:\Users\my_file.txt
To get the filesize you can use the function filesize
$myFile = "C:\Users\my_file.txt"
echo filesize($myFile) . "bytes";
If the file is on the same dir as the php script you can use a relative path:
$myFile = "my_file.txt"
echo filesize($myFile) . "bytes";
If the file is one dir below you can also use a relative path.
$myFile = "../my_file.txt"
echo filesize($myFile) . "bytes";
Another relative path example, this time 1 dir UP:
$myFile = "./newdir/my_file.txt"
echo filesize($myFile) . "bytes";
Ideally you just give filesize an absolute path.
If that is not an option, you have to give it a relative path (relative to the PHP-file you are in).
Say you have a dir files with another folder in it (php), and in it again there is a file called filesize.php. In the files folder you have a file called bigfile.txt.
To get the filesize of the bigfile.txt in your php script which is in php/filesize.php you could do one of the following:
<?php
$filesize = filesize("../bigfile.txt"); // One folder up (relative)
$filesize = filesize("C:\files\bigfile.txt"); // Absolute path
Related
Is there any way to file_put_contents from different path i mean instead of
c/programfiles/../../htdocs/test.txt using c/test/test.txt
the below is the code i am using:
$date=current_date;
$file = "test_"$date".txt";
$contents = "testtest";
file_put_contents($file, $contents);
Many thanks in advance!
When placing files on your computer you can either use relative or absolute path like this:
// absolute path
$file = "C:/temp/test.txt";
// relative path from C:/Program Files/wamp/htdocs/index.php
$file = "../../temp/test.txt";
Both of those files point to the same place
I am looking to save an xml file to a different directory from the root using php5. any ideas?
//write to the file
$filename = $id . ".xml";
$doc->save($filename);
I want to save the file to the /xml/ directory.
Change the argument to $doc->save to include the path
$filename = '/xml/' . $id . ".xml";
$doc->save($filename);
Now the thing to bear in mind is that this is a filesystem path, not web URL so its literally going to save in /xml not DOCUMENT_ROOT/xml.
I've got the code
$directory = "C:/My file path";
$phpfiles = glob($directory . "*.html");
foreach($phpfiles as $phpfiles)
{
echo $phpfiles;
}
But how would I change it so that it doesn't just list the files, but actually links to them?
First of all, don't use same variable names at foreach(). You can link to files, like this.
foreach($phpfiles as $phpfile)
{
echo "<a href=$phpfile>".basename($phpfile)."</a>";
}
$phpfile containing full path of file (for example : /home/eray/Desktop/test.html)
basename() is returning just file name from path . basename($phpfile)'s output is test.html . If you want to print just test (without .html extension) , you can use this : basename($phpfile, ".html") Thanks, #aSeptik.
Assuming that the links are accessible via a web server you'll need a different root path for web access than you have on your computer. Also, your foreach is wrong. The second variable needs to be singular (well, at least different than the first). So assuming your web server sees the file path as a valid site path:
$rootPath = "/MyFilePath";
foreach ($phpfiles as $phpfile)
{
echo "$phpfile";
}
$files = glob("*.html");
echo '<ul>'.implode('', array_map('sprintf', array_fill(0, count($files), '<li>%s</li>'), $files, $files)).'</ul>';
This is ok "eray"
$phpfile containing full path of file (for example :
/home/eray/Desktop/test.html) basename() is returning just file name
from path . basename($phpfile)'s output is test.html . If you want to
print just test (without .html extension) , you can use this :
basename($phpfile, ".html") Thanks, #aSeptik.
how to remove. php extension and in the link.
exaple:
http//example.com/dir1/file.php **
(with out .php on end).
Thanks
1.Hi, I have a internal use only file upload script that uploads the files to a directory. When I upload something from my computer with a spcace in the name i.e example 1.zip it uploads with a space in the name thus killing the link in a email. Is it possible to make apache remove the space when its uploaded or make it a underscore?
The second problem I am having is how would I parse this to make the link an email link with the url of the file as the body of the email amd the email addy anything?
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $_FILES['file']['name'])) {
// uploaded file was moved and renamed succesfuly. Display a message.
echo "Link: " . "http://example.org/" . $_FILES["file"]["name"];
You just need to urlencode() your file name and everything is fine:
echo "Link: http://example.org/" . urlencode($_FILES["file"]["name"]);
But if you want to remove the spaces for another reason, you can use str_replace():
$replaced_name = str_replace(' ', '_', $_FILES["file"]["name"]);
rename($uploaddir . '/' . $_FILES['file']['name'], $uploaddir . '/' . $replaced_name);
# You should urlencode() it nonetheless:
echo "Link: http://example.org/" . urlencode($replaced_name);
Try:
$filename = $_FILES['file']['name'];
$filename = preg_replace("/[^a-zA-Z0-9]/", "", $filename);
//then
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $filename)) {
// uploaded file was moved and renamed succesfuly. Display a message.
echo "Link: " . "http://example.org/" . $filename;
As a side note : with the code you are using, what is happening if two files with the same name are uploaded ? If you don't do a check (like "is there a file that already has that name in $uploaddir ?") the second file will replace the first one.
That might not be something you want... is it ?
If not, to solve that (potential) problem, one solution is to always rename uploaded files, with names you control. (A simple counter would probably to the trick)
Another thing is : $_FILES["file"]["name"] is sent by the client, and, as such, can probably be forged to contains whatever someone would want. If it contains something like "../../index.php" (or something like this - you get the idea), this could allow someone to put any file they want on your server.
To prevent this from happening, you shoud be sure the file name/path used as destination of move_uploaded_file does not contain anything "dangerous". A solution could be to use basename. (see, for instance, example #2 on POST method uploads)
You might also want to check the mimetype of the uploaded file, so you don't get executables, for instance -- and you should make sure files uploaded are not executable by the webserver.
how to map the path to the file easily?
public_html/api/function.php
<?php
function writetologfile($content)
{
$filename = 'logfile/testing_randomstring.txt';
if (!$handle = fopen($filename, 'a'))
{
echo "Cannot open file ($filename)";
exit;
}
fclose($handle);
}
?>
the actual path of the text file is in public_html/r/admin/logfile/testing_randomstring.txt
so if I run the script at public_html/folder1/folder2/addlog.php, it won't be able to find the path to the testing_randomstring.txt
addlog.php
<?php
include("../../api/function.php");
writetologfile('hahaha');
?>
How I can able to easily point to this text file path, no matter where my php calling script is from.
I tried to change $filename = 'logfile/testing_randomstring.txt'; inside writetologfile function by enforcing it to absolute fix path,
something like $filename='/r/admin/logfile/testing_randomstring.txt',
but it is not working
Instead of using a relative path, you could specify an absolute path. Assuming public_html is in your home directory, try this:
$filename = '/public_html/r/admin/logfile/testing_randomstring.txt';
fopen(getenv('HOME') . $filename, 'a');
This uses getenv to read the contents of the environment variable $HOME.