Rename PHP filename inside directory - php

I want to change filename in directory carbrands/alto/alto.php .Instead of alto.php I want to change as alto_new.php. But if I try to change name as
rename($old_name,$file_name);
After using this the filename changed but its not replace inside directory carbrands/alto instead its replaced out of directory. How to fix this issue?

rename("carbrands/alto/alto.php", "carbrands/alto/alto_new.php");
try this

I missed full path for $filename.Now I used full path for old and filename Now its worked correcly.
$pagename="carbrands/alto/alto.php";
$filename="alto_new.php";
$arr = explode("/", $page_name, 2);
$first = $arr[0];
$second1 = explode("/", $arr[1], 2);
$second = $second1[0];
$third = $second1[1];
$directory="$first/$second/";
foreach(glob('*.php') as $path_to_file) {
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace($page_name,$file_name,$file_contents);
file_put_contents($path_to_file,$file_contents);
}
rename($directory.$third,$directory.$file_name);

You need to mention the entire path.
$old_name = 'carbrands/alto/alto.php';
$file_name = 'carbrands/alto/alto_new.php';
rename($old_name,$file_name);

Related

php get file folder name from url

I need get file folder name from url
examples:
http://domain/folder/NEEDTHIS/filename.xml
http://domain/folder/folder2/NEEDTHIS/filename.xml
http://domain/folder/folder2/folderanother/NEEDTHIS/filename.xml
I need only "NEEDTHIS" folder name only
i using this code for get file name
$parts = parse_url("http://domain/folder/NEEDTHIS/filename.xml");
$title = basename($parts['path']);
echo $title;
// Output: filename.xml
How can i get this file name folder?
With this approach you can even avoid parse_url()...:
$url = "http://domain/folder/NEEDTHIS/filename.xml";
$items = explode('/', $url);
echo $items[sizeof($items) - 2];
// Output: NEEDTHIS
Note: this solution only assumes directories/filenames are separated by a character. To be even more general, you could use DIRECTORY_SEPARATOR:
$url = "http://domain/folder/NEEDTHIS/filename.xml";
$items = explode(DIRECTORY_SEPARATOR, $url);
echo $items[sizeof($items) - 2];
Here you go. Use dirname() to get simple one-liner that works both for URL and normal dir-paths. Works too, if no file is specified in path (or when path ends at dir).
$path = 'http://example.com/root/one/two/three/four.xml';
echo end(explode('/',dirname($path)));
Outputs:
three
You can replace $path with anything.
Try this:
$url = "http://domain/folder/folder2/folderanother/NEEDTHIS/filename.xml";
$array = explode('/',$url);
$count = count($array);
echo $array[$count-2];

How to copy a file without overwriting the destination file?

The script I made is.
<?php
$source_file = 'http://www.domain.tld/directory/img.png';
$dest_file = '/home/user/public_html/directory/directory/img.png';
copy($source_file, $dest_file);
?>
I need that image to not be delete and reuploaded every time the script is running. I would either want it to be img1.png, img2.png, img3.png, etc. Or img(Date,Time).png, img(Date,Time).png, etc. Is this possible and if so, how do I do this?
If you're concerned with overwriting a file, you could just drop in a timestamp to ensure uniqueness:
$dest_file = '/home/user/public_html/directory/directory/img.png';
// /home/user/public_html/directory/directory/img1354386279.png
$dest_file = preg_replace("/\.[^\.]{3,4}$/i", time() . "$0", $dest_file);
Of if you wanted simpler numbers, you could take a slightly more tasking route and change the destination file name as long as a file with that name already exists:
$file = "http://i.imgur.com/Z92wU.png";
$dest = "nine-guy.png";
while (file_exists($dest)) {
$dest = preg_replace_callback("/(\d+)?(\.[^\.]+)$/", function ($m) {
return ($m[1] + 1) . $m[2];
}, $dest);
}
copy($file, $dest);
You may need to be using a later version of PHP for the anonymous function callback; I tested with 5.3.10 and everything worked just fine.
<?php
$source_file = 'http://www.domain.tld/directory/img.png';
$dest_file = '/home/user/public_html/directory/directory/img.png';
if(!is_file($dest_file)){
copy($source_file, $dest_file);
}
else{
$fname = end(explode('/',$dest_file));
$fname = time().'-'.$fname;
$dest_file = dirname($dest_file).'/'.$fname;
copy($source_file,$dest_file);
}
?>
use this code
This will add time before filename
$source_file = 'http://www.domain.tld/directory/img.png';
$dest_file = '/home/user/public_html/directory/directory/img'.uniqid().'.png';
copy($source_file, $dest_file);
uniquid gives you a unique Id which is rarely possible to overwrite...
also i would make folders for each month or related to the id of the image
like
mkdir(ceil($imgId / 1000), 0777);
You can use rename().
For Example:
rename ("/var/www/files/file.txt", "/var/www/sites/file1.txt");
Or
You can also use copy
$source_file = 'http://www.domain.tld/directory/img.png';
$dest_file = '/home/user/public_html/directory/directory/img.png';
if(!is_file($dest_file)){
copy($source_file, $dest_file);
}
Or if you want to add time it ,you can try like this.
$source="http://www.domain.tld/directory/";
$destn ="/home/user/public_html/directory/directory/";
$filename="image.png";
$ex_name = explode('.',$filename));
$newname = $ex_name[0].'-'.time().$ex_name[1]; //where $ex_name[0] is filename and $ex_name[1] is extension.
copy($source.filename,$destn.$newname );

Retrieve path of tmpfile()

Quickie...
Is there a way to retrieve the path of a file created by tmpfile()?
Or do I need to do it myself with tempnam()?
It seems stream_get_meta_data() also works :
$tmpHandle = tmpfile();
$metaDatas = stream_get_meta_data($tmpHandle);
$tmpFilename = $metaDatas['uri'];
fclose($tmpHandle);
Like this
$path = array_search('uri', #array_flip(stream_get_meta_data($GLOBALS[mt_rand()]=tmpfile())));
file_put_contents($path, 'hello');

How do I use PHP to grab the name of the file?

what I want to do is PHP to look at the url and just grab the name of the file, without me needing to enter a path or anything (which would be dynamic anyway). E.G.
http://google.com/info/hello.php, I want to get the 'hello' bit.
Help?
Thanks.
You need basename and explode to get name without extension:
$name = basename($_SERVER['REQUEST_URI']);
$name_array = explode('.', $name);
echo $name_array[0];
$filename = __FILE__;
Now you can split this on the dot, for example
$filenameChunks = split(".", $filename);
$nameOfFileWithoutDotPHP = $filenameChunks[0];
This is safe way to easily grab the filename without extension
$info = pathinfo(__FILE__);
$filename = $info['filename'];
$_SERVER['REQUEST_URI'] contains the requested URI path and query. You can then use parse_url to get the path and basename to get just the file name:
basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '.php')
http://php.net/manual/en/function.basename.php
$file = basename(__FILE__); // hello.php
$file = explode('.',$file); // array
unset($file[count($file)-1]); // unset array key that has file extension
$file = implode('.',$file); // implode the pieces back together
echo $file; // hello
You could to this with parse_url combined with pathinfo
Here's an example
$parseResult = parse_url('http://google.com/info/hello.php');
$result = pathinfo($parseResult['path'], PATHINFO_FILENAME);
$result will contain "hello"
More info on the functions can be found here:
parse_url
pathinfo

PHP Get only a part of the full path

I would like to know how can I subtract only a part of the full path:
I get the full path of the current folder:
$dbc_root = getcwd(); // That will return let's say "/home/USER/public_html/test2"
I want to select only "/public_html/test2"
How can I do it?
Thanks!
I think you should check the path related methods:
pathinfo() - Returns information about a file path
dirname() - Returns directory name component of path
basename() - Returns filename component of path
You should be able to find a solution with one of these.
Well, if you know what the part of the path you want to discard is, you could simply do a str_replace:
$dbc_root = str_replace('/home/USER/', '', $dbc_root);
Depends on how fixed the format is. In easiest form:
$dbc_root = str_replace('/home/USER', '', getcwd());
If you need to get everything after public_html:
preg_match('/public_html.*$/', getcwd(), $match);
$dbc_root = $match;
<?php
function pieces($p, $offset, $length = null)
{
if ($offset >= 0) $offset++; // to adjust for the leading /
return implode('/', array_slice(explode('/', $p), $offset, $length));
}
echo pieces('/a/b/c/d', 0, 1); // 'a'
echo pieces('/a/b/c/d', 0, 2); // 'a/b'
echo pieces('/a/b/c/d', -2); // 'c/d'
echo pieces('/a/b/c/d', -2, 1); // 'c'
?>
You can replace the /home/USER with an empty string:
$path=str_replace("/home/USER", "", getcwd());
$dbc_root = getcwd(); // That will return let's say "/home/USER/public_html/test2"
$dbc_root .= str_replace('/home/USER', '', $dbc_root); // Remember to replace USER with the correct username in your file ;-)
After this your $dbc_root should be without /home/USER
I didn't test, if you prefer to create a new var for this...
You could try:
$slim_dbc_root = str_replace('/home/USER', '', $dbc_root);
I hope this will help you into the right direction
Try this "/home/pophub/public_html/" is the text you are removing from the getcwd()
$dir = getcwd();
$dir1 = str_replace('/home/pophub/public_html/', '/', $dir);
echo $dir1;

Categories