Trying to set folder name as variable - php

My index.php is stored in ***/101/index.php. What I want to do is to extract the parent folder name and use it as a $var.
I tried the $var = getcwd() . "\n"; , but it gets the whole structure, etc: c:/wamp/www/HC/101. Is it even possible to get out only the last folder in the line?

Try this:
$dirname = __DIR__;
basename($dirname); //Returns the current file directory name
In your example c:/wamp/www/HC/101 should return 101

$directory = str_replace(dirname(__DIR__), null, __DIR__):
Or basename(__DIR__)

Related

Replacing php realpth when outputing to users

I used php real path to get actual path of files and directory to delete and after deleted i will print all deleted
items. But my problem is that it also show the real path where the file source is and i don't want to show it to users
is there any way i can hide the pay and only show the file example.
I don't like it to look like this
[File]: /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/mosaic_1.jpg
[File]: /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/room_home_1.jpg
[Directory]: /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder
Is there anyway i can make it look this way using rejex or any method please i need help i have to remove /mnt/wef66/d2/81/557642661/htdocs/
[File]: www.example.com/useruploads/myfiles/imagefolder/mosaic_1.jpg
[File]: www.example.com/useruploads/myfiles/imagefolder/room_home_1.jpg
[Directory]: www.example.com/useruploads/myfiles/imagefolder
Maybe using something like this
echo preg_replace("/mnt/wef66/d2/81/557642661/htdocs", "www.example.com", "[File]: /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/mosaic_1.jpg");
$path = realpath($parentBas);
I would use configuration variables $privatePath and $publicPath.
So you can concat whichever you want to the relative paths to your directories or files.
For your example:
$privatePath = '/mnt/wef66/d2/81/557642661/htdocs/';
$publicPath = 'www.example.com/';
$pic1RelativePath = 'useruploads/myfiles/imagefolder/mosaic_1.jpg';
$pic1privatePath = $privatePath . $pic1RelativePath;
// /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/mosaic_1.jpg
$pic1publicPath = $publicPath . $pic1RelativePath;
// www.example.com/useruploads/myfiles/imagefolder/mosaic_1.jpg
I think this is easier and more efficient than replacing the paths with regex.
EDIT:
If you have all the real paths in an array, you can loop through it and replace easily all the private paths with the public paths this way:
$paths = [
'/mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/mosaic_1.jpg',
'/mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/room_home_1.jpg',
'/mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder'
];
foreach ($paths as &$path) {
$path = str_replace($privatePath, $publicPath, $path);
}
print_r($paths);

If file with name.jpg exists do A else do B

I need to develop a little PHP script that I can run from a cron job which in pseudo code does the following:
//THIS IS PSEUDO CODE
If(file exists with name 'day.jpg')
rename it to 'fixtures.jpg'
else
copy 'master.jpg' to 'fixtures.jpg'
Where day.jpg should be the current day of the month.
I started to replace the pseudo code with the stuff I'm pretty sure how to do:
<?php
if(FILE EXISTS WITH NAME DAY.JPG) {
rename ("DAY.JPG", "fixtures.jpg");
} else {
copy ("master.jpg", "fixtures.jpg");
}
?>
Clearly there are still a few things missing. Like I need to get the filename with the current day of the month and I need to check if the file exists or not.
I guess I need to do something like this $filename='date('j');'.jpg to get the filename, but it isn't really working so I kinda need a bit help there. Also I don't really know how to check if a file exists or not?
$path = __DIR__; // define path here
$fileName = sprintf("%s%d.jpg", $path, date("j"));
$fixtures = $path . DIRECTORY_SEPARATOR . "fixtures.jpg";
$master = $path . DIRECTORY_SEPARATOR . "master.jpg";
file_exists($fileName) ? rename($fileName, $fixtures) : copy($master, $fixtures);
Basicly you need script like above but you need to work on your path. Your code above had syntax problem.
You have a basic syntax problem, it should be:
$filename = date('j') . '.jpg';
You don't put function calls inside quotes, you need quotes around the literal string '.jpg', and you need to use . to concatenate them.
I recommend you read the chapter on Strings in a PHP tutorial.

PHP - Explode/ substr/ Filename

See previous Question which was partly answered but there has been a change in requirements for the script: PHP - Exploding / Moving / Filename
i'm new to php and am stuck. I have loads of files that look like this:
2014-04-01 NS122345 - The date, the initials of the person and there employee code.
I want to be able to move the files that have NS or JB Or GA into there relevant folder/directories. So for NS it would go into the Nathan Saunders Folder, for JB into the Joe Bailey folder.
My directory structure looks like this:
root/wan/upload - Where files/images/docs are stored. Inside upload folder i have:
>2014-04-08 NS6565.doc
>2012-01-03 JB8932.doc
>2013-02-01 GA5434.doc
>etc
root/wan/administrator/components/com_upload - where my code is stored
This is my php code for moving, creating and checking the filename and putting it in the correct folder:
$dir = JPATH_BASE . DS . "upload";
$folders = array('SE528733B'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/528733B','SE125673B'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/125673B','SE3452312'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/3452312');
$files = scandir($dir);
foreach($files AS $file){
if(!is_file($dir.DS.$file)){ continue; }
$array = explode(' ', $file);
if(count($array)<2){ continue; }
$firstTwoLetters = substr($array[1], 0, 9);
$foldername = $firstTwoLetters;
if(is_dir($folders[$firstTwoLetters])||mkdir($foldername[$firstTwoLetters],0777, 1))
rename($dir.DS.$file,$foldername[$firstTwoLetters].DS.$file);
That code currently reads the filename if its already in the array "folders" it moves to the correct folder, I have changed it recently to make the folder automatically reading whatever file is in the upload section, but the problem comes when making the folder, the mkdir seems to make the directory:
1) in the wrong place it makes it where the code is stored which is in the com_upload section instead of making it in the upload folder.
2) Names it wrong it takes the first letter not the letters or numbers after it. E.g. "2014-04-08 NS6565.doc", makes the directory "N"
Any help to fixing those 2 problems would be great.
Thanks,
1) If you want to create the directory in another place or you use a relative path from the directory your code is or you use an absolute path.
2) When you are creating the directory you use $foldername but it isn't a name of any directory. It's instead the name of the file. Also, you use it as an array when it's a string (so it only take one char)
Try this:
$dir = JPATH_BASE . DS . "upload";
$folders = array('SE528733B'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/528733B','SE125673B'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/125673B','SE3452312'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/3452312');
$files = scandir($dir);
foreach($files AS $file){
if(!is_file($dir.DS.$file)){ continue; }
$array = explode(' ', $file);
if(count($array)<2){ continue; }
$firstTwoLetters = substr($array[1], 0, 9);
$foldername = substr($firstTwoLetters,0,2);
if(is_dir($dir. DS . $foldername)||mkdir($dir. DS . $foldername,0777, 1))
rename($dir.DS.$file,$dir . DS . $foldername . DS.$file);

issue with the output of dir->read() in php

inside ajax folder, there are files:
json.php, load.php, script.php
main.php
<?php
$dir = dir("ajax");
while (($file = $dir->read()) !== false)
{
echo "filename: " . $file . "<br />";
}
$dir->close();
It show:
filename: .
filename: ..
filename: json.php
filename: load.php
filename: script.php
Question:
In the result, what does the first two items mean? . .. ?
Those are the current (.) and parent (..) directories.
You can get rid of them in following way:
$result = array_diff($result, array('..', '.'));
. and .. are "directories" which equate to the current directory, and parent directory, respectively.
for example, if you are in the ajax folder, json.php can also be accessed with ./json.php.
on the other hand, ../json.php will look for the file in the parent folder (same directory as the ajax folder).
. is current folder.
.. is parent folder. Any folders on window and linux have two this.
If you want to read some type of file, you can use glob function of php.
$files = glob($dirpath.'/*.php');
Single dot: . This represents current directory.
Double dot: .. This represents parent directory.

Changing Base Path In PHP

I need to change the folder that "relative include paths" are based on.
I might currently be "in" this folder:
C:\ABC\XYZ\123\ZZZ
And in this case, the path "../../Source/SomeCode.php" would actually be in this folder:
C:\ABC\XYZ\Source
And realpath('.') would = 'C:\ABC\XYZ\123\ZZZ';
If however, realpath('.') were "C:\Some\Other\Folder"
Then in this case, the path "../../Source/SomeCode.php" would actually be in this folder:
C:\Some\Source
How do I change what folder is represented by '.' in realpath()?
Like this:
echo ('BEFORE = '.realpath('.')); // BEFORE = C:\ABC\XYZ\123\ZZZ
// Some PHP code here...
echo ('AFTER = '.realpath('.')); // AFTER = C:\Some\Other\Folder
How can I change the folder represented by '.', as seen by realpath()?
The function chdir() does this.
For example:
echo ('BEFORE = '.realpath('.')); // BEFORE = C:\ABC\XYZ\123\ZZZ
chdir('C:/Some/Other/Folder');
echo ('AFTER = '.realpath('.')); // AFTER = C:\Some\Other\Folder
Use the chdir() function.
Change your current working directory with chdir()
http://us.php.net/chdir

Categories