Get directories names and split them in PHP - php

I have two problems.
Problem number one: Linux: I want to get only last folder names using ls:
Wrong - /home/test/ok/ok1/
Right - ok1
Currently I'm using command:
ls -d /home/test/*/*/
Problem number two: PHP: I want all directories names (from command's output - point 1) save to separate variables, dividing them into numbers and letters.
For example we have 3 folders from the previous command:
ts2
OK4
oks67
I want each of them to write to the variable $foldername, and then receive two additional subvariables $foldername_1 (letter) and $foldername_2 (number)
Thanks in advance for any help! :)

To address your first problem, use basename
$ basename /home/test/ok/ok1/
ok1

Related

Php script to replace specific string in filenames from database

I need a simple php script which needs to find files on server after reading Number column from database, copy files to another directory and then replace specific strings in saved files. For example we have files
20160107-151620_03216488727-all.mp3
20160418-105509_03225545395-all.mp3
We need to replace (03216488727, 03225545395) with the strings from database. Here is my database info:
Number Policy Number Month
03216488727 123456788 2016-06
03225545395 123433339 2016-06
so after the replacement files will be
20160107-151620_123456788-all.mp3
20160418-105509_123433339-all.mp3
Please help.
Have a look on the str_replace() function.

Extract ZIP's contents on screen but avoid extra information except of the file names

I am looking for a command that will print all the contents of an archive (including sub-folders and it's files) without extracting the actual archive on the disk, but only on screen.
I achieve something using some other questions and answers from this site, and here is my command:
unzip -l test.zip | awk '/-----/ {p = ++p % 2; next} p {print $NF}'
The output:
0 04-11-2009 13:43 jodconverter-webapp-2.2.2/ 1815 04-11-2009 13:43 jodconverter-webapp-2.2.2/README.txt 0 04-11-2009 13:43 jodconverter-webapp-2.2.2/src/ 5349 04-11-2009 13:42 jodconverter-webapp-2.2.2/src/jodconverter-webapp-2.2.2-sources.jar 26436 04-11-2009 13:43 jodconverter-webapp-2.2.2/LICENSE.txt 3819 04-11-2009 13:43 jodconverter-webapp-2.2.2/ChangeLog.txt 3314202 04-11-2009 13:42 jodconverter-webapp-2.2.2/jodconverter-webapp-2.2.2.war
As you can see the output is one line, and includes some extra information that I don't really need.
I want an output of this kind:
jodconverter-webapp-2.2.2/
jodconverter-webapp-2.2.2/README.txt
jodconverter-webapp-2.2.2/src/
jodconverter-webapp-2.2.2/src/jodconverter-webapp-2.2.2-sources.jar
.
.
.
So not only I want to output the file names only (and their full path) and avoid any extra other information like time permissions and so on, but also I want to use something like break-line to distinguish different files.
Keep in mind that this command will run on a PHP file to get the contents of the file, so I don't know if this can help us to use the <br> to do the break lines.
Is that possible with a single command?
Well, I can't think a command that will return you back the output you need, but may someone else can know something more.
What I would do in your case is to split the line into array and play with it until you get what you want.
You actually have to find a pattern that will work for all cases.
With a brief look on the command's output I came with the following decisions:
You splitting the line using as separator the space (i.e explode() )
The pattern I can see is that there exist a time of the form xx:xx exactly before the actual file/directory ! So you can check if the current line is a valid 24-based time using a regular expression then that means if that is the case and you are in the $i position of the array the $i+1 is what you are looking for, and therefore you can copy that to a new array.
Repeat
I think that is a bit pain, but at least is a solution.

Compare two unsorted files line by line and only output lines which are in file 1 AND file 2, but not in file 1

I need to Compare two unsorted files line by line and only output lines which are in file 1 AND file 2, but not in file 1. Essentially giving me New + Same strings from each of the two files, and excluding the old/non-existing strings.
I need to do this on some very large files. (10+ GB)(About 1,000,000 lines).
I have tried a few of the below options, but nothing gives me exactly what I need:
join -v1 -v2 <(sort File1.txt) <(sort File2.txt) > File3.txt
This "join" seems to give me the lines that are in both File1.txt AND/OR File2.txt. (Essentially giving me a combine+unique command). (This is almost correct, but I need this to exclude the the lines/strings if they are not in the second file, but are in the first file.
fgrep -vf File1.txt File2.txt > File3.txt
This works but as you know, is Very slow on large files and is not really an option.
Case sensitivity would be nice, but not at all required. The reason I mention this is because in my research I found that if the compare was case insensitive, it would speed up the search a Lot.
Thanks again in advance.
It's not at all clear what you are trying to do but try this:
comm <(sort File1.txt) <(sort File2.txt)
and man comm for whatever arguments you need.

PHP Exec() Using Sed Gives Strange Results

I have a list of full file paths. All of the full file paths look like "/dir1/dir2/dir3/s..". I want to completely remove the s. from the filename. There is the possibility of a filename being plural, for example s.asdfs.cpp. I do not want to remove the second occurence of s. since that is part of the actual filename and not a reoccuring theme in every full file path in the list.
Running the following in shell works as I want it to:
echo /dir1/dir2/dir3/s.filenames.cpp | sed 's#\(.*\)\/s\.\([^\/]*\)#\1\/\2#g'
Gives the desired result of:
/dir1/dir2/dir3/filenames.cpp
But if I run the following in php:
$formatted_filename = exec("echo ".$filename." | sed 's#\(.*\)\/s\.\([^\/]*\)#\1\/\2#g'");
where
$filename = /dir1/dir2/dir3/s.filenames.cpp;
And then in my bash shell run
php -q script_name_that_contains_command_above.php > test.html
and refresh my firefox browser that displays test.html I get very strange results. In place of where this edited file path should be listed I get
<strange box>/<strange box>
where
<strange box>
is a small box with 2 rows and 2 columns consisting of 0's except for the bottom right cell. The first occurence has a 1 in the bottom right cell, and the second occurence has a 2 in the bottom right cell.
The sed command works, but php, or the exec command is interpreting it incorrectly I believe. Any ideas?
The solution for using exec with this particular regular expression was to use the php function, preg_replace()
preg_replace("/\/s/./", "/", $filename);

Find out what files are in a directory that are "not" listed in the database

I have a small gallery website but the amount of images in directory differs from that accounted for in database by around 150 so I was wondering if there is a way to find out/list what files are in a directory that are "not" in the database (or vice versa).
basic db structure:
images
id
images
image names are stored in db as "imagename.jpg"
and the images themselves are stored in images directory
images/
Put file names from server in one array and files from database in another. Use array_diff() to get result.
Example (PHP):
$files_db = array("car.jpg", "bike.jpg", "plane.jpg", "ship.jpg", "tank.jpg");
$files_server = array("car.jpg", "bike.jpg", "ship.jpg", "rocket.jpg");
Use
print_r(array_diff($files_db, $files_server));
Output
Array
(
[2] => plane.jpg
[4] => tank.jpg
)
Or (vice versa)
print_r(array_diff($files_server, $files_db));
Output
Array
(
[3] => rocket.jpg
)
If this is likely a one-off problem, I'd solve this with a half-assed solution, using first a little SQL script:
SELECT filename FROM images;
and ask the database client mysql or psql or whatever to dump the output as plain text to a file. (Shell redirection may do the job if you can't easily find your database client's 'dump to file' command.)
Then I'd get the directory listing:
ls /path/to/images/ > ls_files
Sort both:
sort db_files > db_files.sorted
sort ls_files > ls_files.sorted
Then run diff(1) to see which files are referenced where:
diff -u ls_files.sorted db_files.sorted
Lines prefixed with a + or - are in one but not the other.
You might need edit the SQL output or the ls output to get one to match the other. If your editor has a tool like vim's ^V block select, some of those editing tasks can be simplified, but sometimes just running ls from another directory can help prepend the right directory structure in front of every filename.

Categories