This question already has answers here:
PHP, get file name without file extension
(18 answers)
Closed 6 years ago.
Is that possible to remove the file extension type from file name and underscore? For example: File name is MTK_USB_All_v1.0.1.zip But I want to show it as
MTK USB All v1.0.1
Ex: https://androiddatahost.com/upload/Amomp
Right now Im using
<?php echo $name; ?>
to show the file name. I also want to show the alternate name of the file without file extension and underscore.
Is that possible?
You can try this
$filename=basename($pathinfo);
$final_filename=str_replace('_','',$filename);
To Show only the Filename you can use pathinfo.
$filename = pathinfo('filename.zip', PATHINFO_FILENAME);
To replace underscore with blank space you can use str_replace();
Related
This question already has answers here:
PHP what is the best way to write data to middle of file without rewriting file
(3 answers)
How to insert some text in middle of file in PHP [closed]
(4 answers)
Closed 4 years ago.
I am generating some PHP source code files using PHP only :)
And I want to know if I have to write some line of text before closing brackets, how can I do that using PHP file functions.
e.g. below is my target file code. filename : project.php
Class project{
public function get_project($id){
..... some code here
}
public function add_project(){
..... some code here
}
<--- add code here
}
I have a line of code which I want to add to the position pointed in the example. I can use file_put_content() method but it has option to Append the code on the end of the file like below
file_put_contents('project.php', 'Hello World', FILE_APPEND);
but it will not write to specific position. So how can I do that?
This question already has answers here:
How to Get filename from a variable (url) in php? [duplicate]
(4 answers)
Closed 4 years ago.
I have this URL and I want to remove all characters before "blog_images/" this world.
http://localhost/login/uploads/blog_images/woman4.jpg
I have tried this PHP function it removes all words but I want to remove this word also, I need to get only this characters woman4.jpg
strstr(http://localhost/login/uploads/blog_images/woman4.jpg, 'blog_images/');
You can get file name using basename function php
<?php
/* $your_file_path_along_with_file_name is your input path */
$your_file_path_along_with_file_name = "http://localhost/login/uploads/blog_images/woman4.jpg";
$original_file_name = basename($your_file_path_along_with_file_name);
echo $original_file_name //it prints your file name
?>
you can use explode like this,
$dat = explode('blog_images/','http://localhost/login/uploads/blog_images/woman4.jpg');
echo $dat[1];
This question already has answers here:
How do I get a file name from a full path with PHP?
(14 answers)
Closed 6 years ago.
How do I get only the file name instead of the path + file name?
foreach(glob('./item/*.txt') as $filename){
echo $filename;
}
Use basename($filename) to get the filename of the file. Check the documentation here: http://php.net/manual/en/function.basename.php
This question already has answers here:
How to get the last dir from a path in a string
(11 answers)
Closed 6 years ago.
So I have a file path as a text variable in php:
"one/cool/file/path"
I need a php code to return "path"
By return I think you mean display...
Lets suppose you have the following path:
"one/cool/file/path"
Stored as a string in the variable:
"path"
To return it (or display it) you can just do:
echo path;
If you then go to your php in a web browser you should see your parh there!
You could achieve this using the following method:
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "\n";
Output: /www/htdocs/inc
http://php.net/manual/en/function.pathinfo.php#example-2657
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How remove extension from string (only real extension!)
Remove file extension on file upload
I am looking for ways to remove extensions but from what i found in google are really bad examples on how to do it.
$from = preg_replace('/\.[^.]+$/','',$from);
Something like this, any idea on how to do it?
Check out the basename function, which should do that for you. Note that it will also strip any folder names in the path.