donglu2008 2012-05-14 05:04
浏览 19
已采纳

php根据mysql中的列重命名目录中的文件

I was wondering if anyone could help with this. Let' say I have a db with columns 'cust_id' and 'name'. I want to rename 2000+ images in a directory to 'cust_id.jpg' from their existing 'name.jpg'. Column 'name' contains all the image names in the folder and I want them renamed to their associated customer id #.

I'm pretty certain this can be done w/ php/mysql but my knowledge of php is very limited and I wouldn't know where to start writing this. I realise I would have to create a recordset pulling the required data, read directory, loop through etc. but I this is just beyond my capabilities at the moment. Little help?

Thanks!

  • 写回答

2条回答 默认 最新

  • douhao8456 2012-05-14 05:17
    关注

    Try this code.. assuming your table name is tbl_customer and you have your db connection settings in place

    $res = mysql_query("SELECT * FROM tbl_customer");
    
    while($row = mysql_fetch_assoc($res)){
      $name = $row['name'];
      $cust_id = $row['cust_id'];
    
      if(file_exists('path/to/your/image/directory/'.$name)){
        rename('path/to/your/image/directory/'.$name,'path/to/your/image/directory/'.$cust_id.".jpg");
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数