Path to a file in a different drive? - php

My php file is here:
D:/Appserv/www/x/y/file.php
I want to load stuff from this folder:
E:/foldie
I don't know what path will lead me there.
$somePath="HELP ME HERE!!!!"
$dir=opendir($somePath);
//looping through filenames
while (false !== ($file = readdir($dir))) {
echo "$file\n";
}

Use full Windows path to the file it should be working: "E:\folder\file.txt"
or just copy the file in the local/project directory for testing purpose.

Set $somePath = "e:\\foldie". If that doesn't work, please indicate to us how it fails.
[Edit:: make sure you escape your backslashes in strings]

Related

How to generate sitemap for HTML or PHP that are unlinked and have no index file?

Say I have a directory with 100 files in it. Some of the files are PHP and the others are HTML. None of them are linked together. It's just a directory with the files and none of the files are linked, and there is no index file. It's a shared hosting cPanel environment. My question: Is there a way via PHP or otherwise to automatically detect these files and generate a sitemap in HTML, XML or other format? Thanks very much for your help on this one.
Untested but here are a couple scripts which I think may solve your issue:
http://apptools.com/phptools/dynamicsitemap.php
http://yoast.com/xml-sitemap-php-script/
If you want a proper sitemap (how the files link to one another) then there are some libraries available for that mentioned by others. If you just want to list them, then just use the opendir and readdir functions:
$directory = 'your directory';
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory.'/'.$file)){
continue;
}
$array_items[] = $file;
}
}
closedir($handle);
}
You can then loop through the $array_items and output xml or html. You can also make this recursive by making this a function and handling the
if (is_dir($directory.'/'.$file)){
continue;
}
section

How to build a file path correctly by using php?

I have a directory and a dynamic file name. Are there any functions I can use to make sure it is impossible to break out from the specified directory?
for example:
secure_path('/tmp/', 'file.txt') -> /tmp/file.txt
secure_path('/tmp/', '../file.txt') -> /tmp/file.txt
If you're only working in the single directory and not subdirectories below you could do
$file = "/tmp/".basename($input);
Which should give you the filename at the end of any path given in input, and append it to the directory you wanted.
Use realpath() to get a canonic form of the path, and eliminate any ../-s. Then verify that it contains the full path of the directory you want to contain it to.
Such as:
$path = "/tmp/file.txt";
$directory = "{full path of your files directory}";
if (strpos(realpath($path), $directory) === 0) {
// path OK
}
else {
// path not ok
}
I don't really know how ../file.txt is supposed to turn into /tmp/file.txt in your example, but to check whether a path is a subpath of another, use realpath with a simple comparison:
$path = '../foo';
$allowedPath = '/tmp/';
$path = realpath($path);
if (substr($path, 0, strlen($allowedPath)) !== $allowedPath) {
// path is not within allowed path!
}

How to delete files with multibyte characters in the file name?

I accidentally created a file with PHP and now I can't delete or re-name it.
The file is called â€%C2%9Dâ2™j299t™93.gif
Edit: I only have access via FTP, the host is Linux based
Edit 2: I can't even delete the directory it is in.
In the command line, write:
ftp to.target.server
del *93.gif
If you are the owner of the file, this should work (permissions were 644)
From PHP you can use the unlink function as follows:
<?php
$filename="â€%C2%9Dâ2™j299t™93.gif";
unlink($filename);
?>
The problem you have been having is probably to do with the ™ symbol being translated. If you copy the code above and paste it into your editor and then run it, this should delete the file, assuming it is run from the same directory as the file.
Download Filezilla and set site encoding to enforced utf-8. Then try to delete your file via it.
I guess you don't know the exact filename (as your ftp client does not show it in a proper way). You can iterate thru files. This example deletes all files with the character % in it. Use it carefully:
$d = dir(".");
while (false !== ($entry = $d->read())) {
if (strpos ($entry, '%') !== false) {
unlink ($entry);
}
}
$d->close();

PHP: moving directories with contents?

I'm having an array $bundle that stores filenames and directory names.
I'm running through the array with a foreach loop and I want to move them inside of another directory. I'm using the rename method therefore and it works pretty fine with JUST FILES.
However directories with other files in there don't respond to the rename() method.
$folder = 'files';
foreach ($bundle as $value) {
$ext = pathinfo($value, PATHINFO_EXTENSION);
if ($ext != "") { //if $value has no suffix it's a fil
rename(PATH . '/' .$value, $folder . '/' . $value);
}
if ($ext == "") { // it's a folder/directory
//rename doesn't work for directories with contents
//what method should i use here???
}
}
I know the pathinfo() method is not the best way to find out if it's a directory or not, however for my little project it's fine. I just need to know how I can move any directory with all it's contents into the "files" folder.
Thank you for your help.
You will have to get all the filenames in that directory using glob or scandir. Then you would have to loop through them with the rename and move them.
Your other option, if you host allows it, is to use shell_exec and do the mv for linux or copy / xcopy for windows command and move them that way. If you choose the exec route, make sure to secure the input etc to prevent any bad stuff from happening.

How to copy all contents in a folder to a dir in php

I am trying to copy all the files from a directory to another directory in php.
$copy_all_files_from = "layouts/";
$copy_to = "Website3/";
Can someone help me do this please.
Something like this(untested):
<?php
$handle = opendir($copy_all_files_from);
while (false !== ($file = readdir($handle))) {
copy( $file, $copy_to);
}
edit:
To use Amadan's method, you should be able to use this php function:
shell_exec();
Not sure since I never need to use server commands
Easiest:
`cp -r $copy_all_files_from $copy_to`
Unless you're on Windows. Without shelling, it's a bit more complex: read directory, iterate on files (if it's a directory, recurse), open each, iterate while not end of file, read block and write it.
UPDATE: doh, PHP has copy...

Categories