how to get foldername and filename using regex - php

i have output from string
foldername/subfolder/filename.jpg
how to get only foldername/subfolder/ and only filename.jpg
$filepath = 'foldername/subfolder/filename.jpg';
$folder= (preg_match('~[^A-Za-z0-9_\./\]~', $filepath));
echo 'folder:' .'<br>' .$folder;
$filename =(preg_match('....', $filepath));
echo 'fileneme' .'<br>' .$filename;
output
folder:
foldername/subfolder/
filename:
filename.jpg
thank you

Try this
$filepath = 'foldername/subfolder/filename.jpg';
// using regex
$parts = preg_split('~/(?=[^/]*$)~', $filepath );
echo " folder::".$parts[0]." AND fileneme:". $parts[1];
DEMO
Or use pathinfo
$filepath = 'foldername/subfolder/filename.jpg';
$path_parts = pathinfo($filepath);
echo "dirname : ".$path_parts['dirname']."\n";
echo "basename : ".$path_parts['basename']."\n";
echo "extension : ".$path_parts['extension']."\n";
echo "filename : ".$path_parts['filename']."\n";
DEMO

Related

php move_uploaded_file not working when filename has spaces

i am uploading a file to the server. everything works fine unless the uploaded file has a space in it.
I tried to use:
str_replace(" ", "_", $_FILES['image']['name']);
My code is
$image_name= str_replace(" ", "_", $_FILES['image']['name']);
$image_tmp_name = $_FILES['image']['tmp_name'];
$image=$_FILES['image'];
$url = "http://jkshahclasses.com/push_images/$image_name";
if(move_uploaded_file($image_tmp_name,"../../push_images/$image_name"))
{
echo "file uploaded";
}
else
{
echo "error: file not uploaded";
}
Thanks in advance
You can use this function to slugify your file name before uploded :
public function slugify($text)
{
// replace all non letters or digits by _
$text = preg_replace('/\W+/', '_', $text);
// trim and lowercase
$text = strtolower(trim($text, '_'));
return $text;
}
But first you have to get only file name you can do it with this function :
$file_name = pathinfo($path, PATHINFO_FILENAME);
If you want to get file extension you have to use :
$file_ext = pathinfo($path, PATHINFO_EXTENSION);

How to get all files containing substring

Can somebody help me? I have many pictures with current name like : "black_abc","black_bcd","black_cde","white_abc". How can I get only file with filename contains "Black"?
glob will help you find all files containing "Black" in their filename:
$folder = "images"; //the folder containing all your images
$pattern = "*Black*"; //the word you are looking for
$files = glob($folder. '/' . $pattern, GLOB_BRACE);
foreach($files as $filename) {
//Display all pictures
echo "<img src='"$folder . "/" . $filename . "' />";
}
strpos($filename, 'black') !== false
Something like this [Make use of stripos() [Case-Insensitive]
<?php
$files=array("black_1","white_2","black_3");
for($i=0;$i<count($files);$i++)
{
if(stripos($files[i],'black'))
{
echo "Filename is $files[$i]";
}
}

Scandir without file extensions

How do I get the files displaying without the file extension?
Currently I get a file like logo.png but I only need the file name logo.
if (is_dir($dir_path)) {
$files = scandir($dir_path);
foreach($files as $file) {
if ( !in_array( $file, $exclude_all ) ) {
$path_to_file = $dir_path . $file;
$extension = pathinfo ( $path_to_file, PATHINFO_EXTENSION );
$file_url = $dir_url . $file;
echo 'Path to file: ' . $path_to_file . '<br />';
echo 'Extension: ' . $extension . '<br />';
echo 'URL: ' . $file_url . '<br />';
}
}
}
Since PHP 5.2.0 pathinfo can do that as well:
$bareName = pathinfo($path_to_file, PATHINFO_FILENAME);
$text = "filename.test.php";
echo substr($text, 0, strrpos($text, ".")); //filename.test

Get the file extension (basename?)

If I have a code like this:
$file = basename($filename);
How do I get the file extension of $file? The variable $file could contain any kind of file, like index.php or test.jpeg.
Use the pathinfo() function:
$path_parts = pathinfo('/www/htdocs/index.html');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n";
or simply:
echo pathinfo($file, PATHINFO_EXTENSION);
You can of course look for the last "." in the filename and get everything after (relatively easy) but why reinvent the wheel?
pathinfo($filename, PATHINFO_EXTENSION);

hiding file extension it self

I looking for your help please
I use simple php code like:
$file = $_SERVER["SCRIPT_NAME"];
//echo $file;
$break = Explode('/', $file);
$pfile = $break[count($break) - 1];
echo $pfile;
than output $pfile e.g. fileforme.php
but that i want is output of $pfile become fileforme
because i want use it to:
$txt['fl']= $pfile ;
how to do? or there are any another better way?
You can use pathinfo() and its PATHINFO_FILENAME flag (only available for php 5.2+)
e.g.
$file = $_SERVER["SCRIPT_NAME"];
echo 'file: ', $file, "\n";
echo 'PATHINFO_FILENAME: ', pathinfo($file, PATHINFO_FILENAME);
prints (on my machine)
file: C:\Dokumente und Einstellungen\Volker\Desktop\test.php
PATHINFO_FILENAME: test
See basename in the PHP Manual:
$path = "/home/httpd/html/index.php";
$file = basename($path); // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
or use pathinfo if you do not know the extension:
$path_parts = pathinfo('/www/htdocs/index.html');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
Basically you're looking for the filename without the extension:
$filename = basename($_SERVER["SCRIPT_NAME"], ".php");
Note that this answer is specific for the php extension.
$pfile_info = pathinfo($pfile);
$ext = $pfile_info['extension'];
See pathinfo() for further information.

Categories