hiding file extension it self - php

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.

Related

how to get foldername and filename using regex

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

exploding string to remove path

My script is returning the following path.
/home/vol14_2/project.com/b22_16126933/test.project.com/htdocs/php/api.php
I want to remove the rest and end up with the file name.
I know I have to explode the string, I just don't really know how to go about it.
use basename
echo basename("/home/vol14_2/project.com/b22_16126933/test.project.com/htdocs/php/api.php");
//api.php
OR pathinfo
$path_parts = pathinfo('/home/vol14_2/project.com/b22_16126933/test.project.com/htdocs/php/api.php');
echo $path_parts['basename']; // since PHP 5.2.0
//api.php
<?php
$path = "/home/vol14_2/project.com/b22_16126933/test.project.com/htdocs/php/api.php";
$file = basename($path); // $file is set to "api.php"
$file = basename($path, ".php"); // $file is set to "api"
?>

PHP copy image from url to server and echo the final url

I am trying to write a code that will copy an image from URL to a relative path on my server with a random file name and echo back the final url.
I have 2 problems:
It doesn't work with relative path. If I don't declare the path, the function works but the image is being saved on the same folder of the PHP file. If I do specify the folder, it doesn't return any error but I don't see the image on my server.
The echo function always return an empty string.
I am a client side programer so PHP is not my thing... I would appreciate any help.
Here is the code:
<?php
$url = $_POST['url'];
$dir = 'facebook/';
$newUrl;
copy($url, $dir . get_file_name($url));
echo $dir . $newUrl;
function get_file_name($copyurl) {
$ext = pathinfo($copyurl, PATHINFO_EXTENSION);
$newName = substr(md5(rand()), 0, 10) . '.' . $ext;
$newUrl = $newName;
return $newName;
}
EDIT:
Here is the fixed code if anyone is interested:
<?php
$url = $_POST['url'];
$dir = 'facebook/';
$newUrl = "";
$newUrl = $dir . generate_file_name($url);
$content = file_get_contents($url);
$fp = fopen($newUrl, "w");
fwrite($fp, $content);
fclose($fp);
echo $newUrl;
function generate_file_name($copyurl) {
$ext = pathinfo($copyurl, PATHINFO_EXTENSION);
$newName = substr(md5(rand()), 0, 10) . '.' . $ext;
return $newName;
}
Answer Here
Either Use
copy('http://www.google.co.in/intl/en_com/images/srpr/logo1w.png', '/tmp/file.jpeg');
or
//Get the file
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//Store in the filesystem.
$fp = fopen("/location/to/save/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);
You should use file_get_contents or curl to download the file. Also note that $newUrl inside your function is local and this assignment doesn't alter the value of global $newUrl variable, so you can't see it outside your function. And the statement $newUrl; in 3rd line doesn't make any sense.

Remove image extension, rename and restore

Say the image is called:gecko.jpg
Can I first remove ".jpg" and add "-100x100" after "gecko", and then put the extension back, so it would be "gecko-100x100.jpg"?
use pathinfo
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
$new = $path_parts['filename'] . '-100x100.' .$path_parts['extension'];
Yes, quite simply with PHP's string functions in conjunction with basename()
$base = basename($filename, ".jpg");
echo $base . "-100x100" . ".jpg";
Or to do it with any filetype using strrpos() to locate the extension by finding the last .
// Use strrpos() & substr() to get the file extension
$ext = substr($filename, strrpos($filename, "."));
// Then stitch it together with the new string and file's basename
$newfilename = basename($filename, $ext) . "-100x100" . $ext;
--
// Some examples in action...
$filename = "somefile.jpg";
$ext = substr($filename, strrpos($filename, "."));
$newfilename = basename($filename, $ext) . "-100x100" . $ext;
echo $newfilename;
// outputs somefile-100x100.jpg
// Same thing with a .gif
$filename = "somefile.gif";
// outputs somefile-100x100.gif

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);

Categories