duanhun3273 2011-10-05 18:35
浏览 54
已采纳

jQuery ajax php重命名文件名的一部分

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 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
  • 写回答

2条回答 默认 最新

  • doubingling4706 2011-10-05 18:56
    关注
    $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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建