jQuery ajax php rename part of a file name
In the frontend i offer the user to rename any of his images. This is done via javascript prompt to get the new name, jQuery ajax and php. When file is renamed it must take effect on 2 files as you see in the links below. 1
in the url is dynamic. It's开发者_JAVA百科 the user id. The thing is i only want to replace the product
in the filename and not the _1317804260
that comes after it. In the second link we have thumb_
in front of the file name. So again we only want to replace the product
with the new rename. Working on this, it Seems like i need to use explode() many times to get the job done. Is there an easier way to do this.
http://domain.com/userfiles/1/images/product_1317804260.gif
http://domain.com/userfiles/1/images/thumbnails/thumb_product_1317804260.gif
So basically if a new name is newname
the final output of the file should be
newname_1317804260.gif
thumb_newname_1317804260.gif
$oldName = "http://domain...product_userid.gif";
$newName = "what the user entered";
$parts = explode('/', $oldName);
$user_path = $parts[3] . '/' . $parts[4]; // it will contain userfiles/1
$parts = explode("_", $oldName);
$product_name = $new_name . "_" . end($parts);
$thumb_name = "thumb_" . $product_name;
JS:
var tmpName = "product_1317804260.gif".split("_")
var newName = "newname" + "_" + tmpName[1]
PHP:
$tmpName = explode("_", "product_1317804260.gif");
$newName = "newname" . "_" . $tmpName[1];
精彩评论