<legend id='YFxK9'><style id='YFxK9'><dir id='YFxK9'><q id='YFxK9'></q></dir></style></legend>
  • <tfoot id='YFxK9'></tfoot>
  • <i id='YFxK9'><tr id='YFxK9'><dt id='YFxK9'><q id='YFxK9'><span id='YFxK9'><b id='YFxK9'><form id='YFxK9'><ins id='YFxK9'></ins><ul id='YFxK9'></ul><sub id='YFxK9'></sub></form><legend id='YFxK9'></legend><bdo id='YFxK9'><pre id='YFxK9'><center id='YFxK9'></center></pre></bdo></b><th id='YFxK9'></th></span></q></dt></tr></i><div id='YFxK9'><tfoot id='YFxK9'></tfoot><dl id='YFxK9'><fieldset id='YFxK9'></fieldset></dl></div>

    1. <small id='YFxK9'></small><noframes id='YFxK9'>

      • <bdo id='YFxK9'></bdo><ul id='YFxK9'></ul>

        使用 PHP 重命名上传的文件但保留扩展名

        时间:2023-05-21
        <legend id='VlZv7'><style id='VlZv7'><dir id='VlZv7'><q id='VlZv7'></q></dir></style></legend>
        <tfoot id='VlZv7'></tfoot>
        <i id='VlZv7'><tr id='VlZv7'><dt id='VlZv7'><q id='VlZv7'><span id='VlZv7'><b id='VlZv7'><form id='VlZv7'><ins id='VlZv7'></ins><ul id='VlZv7'></ul><sub id='VlZv7'></sub></form><legend id='VlZv7'></legend><bdo id='VlZv7'><pre id='VlZv7'><center id='VlZv7'></center></pre></bdo></b><th id='VlZv7'></th></span></q></dt></tr></i><div id='VlZv7'><tfoot id='VlZv7'></tfoot><dl id='VlZv7'><fieldset id='VlZv7'></fieldset></dl></div>

          <small id='VlZv7'></small><noframes id='VlZv7'>

            <tbody id='VlZv7'></tbody>
              • <bdo id='VlZv7'></bdo><ul id='VlZv7'></ul>

                  本文介绍了使用 PHP 重命名上传的文件但保留扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 PHP 将图像从表单上传到服务器,并想重命名图像 lastname_firstname.[原始扩展名].我目前有:

                  I'm using PHP to upload an image from a form to the server and want to rename the image lastname_firstname.[original extension]. I currently have:

                  move_uploaded_file($_FILES["picture"]["tmp_name"], "peopleimages/" . "$_POST[lastname]" . '&#95;' . "$_POST[firstname]")
                  

                  当然,它会重命名文件 lastname_firstname 而不带扩展名.如何重命名文件但保留扩展名?

                  which, of course, renames the file lastname_firstname without an extension. How do I rename the file but keep the extension?

                  谢谢!

                  推荐答案

                  你需要先找出原来的扩展名是什么;-)

                  You need to first find out what the original extension was ;-)

                  要做到这一点,pathinfo函数可以创造奇迹;-)

                  To do that, the pathinfo function can do wonders ;-)


                  引用手册中给出的示例:


                  Quoting the example that's given in the manual :

                  $path_parts = pathinfo('/www/htdocs/index.html');
                  echo $path_parts['dirname'], "
                  ";
                  echo $path_parts['basename'], "
                  ";
                  echo $path_parts['extension'], "
                  ";
                  echo $path_parts['filename'], "
                  "; // since PHP 5.2.0
                  

                  会给你:

                  /www/htdocs
                  index.html
                  html
                  index
                  


                  作为旁注,不要忘记安全性:


                  As a sidenote, don't forget about security :

                  • 在你的情况下,你应该转义 $_POST[lastname],以确保它只包含 valid 字符
                    • 顺便说一句,你应该使用 $_POST['lastname'] -- 见 为什么$foo[bar] 错了?
                    • In your case, you should escape $_POST[lastname], to make sure it only contains valid characters
                      • And, BTW, you should use $_POST['lastname'] -- see Why is $foo[bar] wrong?
                      • 参见mime_content_type 用于 PHP <5.3
                      • 和/或 finfo_file 对于 PHP >= 5.3

                      这篇关于使用 PHP 重命名上传的文件但保留扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHP 文件上传不读取 $_FILES['image'] 下一篇:如何使用php将图像存储在mysql数据库中

                  相关文章

                  最新文章

                1. <legend id='INGov'><style id='INGov'><dir id='INGov'><q id='INGov'></q></dir></style></legend>
                2. <tfoot id='INGov'></tfoot>

                      <i id='INGov'><tr id='INGov'><dt id='INGov'><q id='INGov'><span id='INGov'><b id='INGov'><form id='INGov'><ins id='INGov'></ins><ul id='INGov'></ul><sub id='INGov'></sub></form><legend id='INGov'></legend><bdo id='INGov'><pre id='INGov'><center id='INGov'></center></pre></bdo></b><th id='INGov'></th></span></q></dt></tr></i><div id='INGov'><tfoot id='INGov'></tfoot><dl id='INGov'><fieldset id='INGov'></fieldset></dl></div>

                        <bdo id='INGov'></bdo><ul id='INGov'></ul>
                    1. <small id='INGov'></small><noframes id='INGov'>