在使用php上传之前,如何重命名文件名

时间:2017-02-16 12:26:00

标签: php

我需要使用不同的ext示例上传两个文件:

file_1.txt和file_image.png

当用户使用上传表单发送这两个文件时:

file_1.txt和file_image.png

在move_uploaded_file之前

我需要将图像名称重命名为txt。名称文件:

file_1.txt file_1.jpeg

我只是想重命名图像文件名,而不是扩展名

代码是这样的:

switch($_REQUEST['action']) {

    case "upload":

        $Imagefile_temp = $_FILES['file']['tmp_name'];
        $Textfile_temp = $_FILES['file']['tmp_name'];

        $Imagefile_name = $_FILES['file']['name'];
        $Textfile_name = $_FILES['file']['name'];

        $ImageFileType =  array('png' ,'jpg');
        $TextFileType =  array('txt');



        $Image_extension = pathinfo($Imagefile_name, PATHINFO_EXTENSION);   // holds the file extension of the file
        $Text_extension = pathinfo($Textfile_name, PATHINFO_EXTENSION);   //holds the file extension of the file



        // //Declaring Path for uploaded images 
        $file_path = "uploads";

        //checks for duplicate files
        if((!file_exists($file_path."/".$Imagefile_name)) || (!file_exists($file_path."/".$Textfile_name))) {

            $j = 0; //Variable for indexing uploaded image 

            for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array

                if(in_array($Image_extension,$ImageFileType) ) {

                    $Image_extension = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) 
                    $file_extension = end($Image_extension); //store extensions in the variable
                    $target_path = $target_path . md5(uniqid()) . "." . $Image_extension[count($Image_extension) - 1];//set the target path with a new name of image

                    $j = $j + 1;//increment the number of uploaded images according to the files in array   

                    $Imagefilestatus = move_uploaded_file($Imagefile_temp,$file_path."/"."image_".$Imagefile_name) ;//if file moved to uploads folder

                } 


                if(in_array($Text_extension,$TextFileType) ) {

                    $Text_extension = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) 

                    $file_extension = end($Text_extension); //store extensions in the variable

                    $target_path = $target_path . md5(uniqid()) . "." . $Text_extension[count($Text_extension) - 1];//set the target path with a new name of image

                    $j = $j + 1;//increment the number of uploaded images according to the files in array       

                    $Textfilestatus = move_uploaded_file($Textfile_temp,$file_path."/".$Textfile_name) ;//if file moved to uploads folder

                } 
            }
        }   
    }       

3 个答案:

答案 0 :(得分:0)

您可以直接从扩展程序中分解文本文件,并在移动时将其用作图像文件名:

$text_file_name = $_FILES["text"]["name"]; // give you the text file name
$image_file_name = $_FILES["image"]["name"]; // give you the image file name
$file_expensions = strtolower(end(explode(".", $image_file_name))); // give you the image extension and so you can use it later for saving the file
$n = explode(".", $text_file_name, 1); 
$name = $n[0]; // get text file name
$name = $name.".".$file_expensions; // this is new name of the image

move_uploaded_file($file_tmp, "path/$name")

答案 1 :(得分:0)

$name = 'Whatevernameyouwant.'.pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
参数PATHINFO_EXTENSION

pathinfo会返回文件扩展名。

答案 2 :(得分:0)

我假设您知道您在此处提供的代码允许具有upload and execute PHP code上传权限的任何人。

(我提到这个的原因是其他地方还有很多改进的余地 - 如果安全漏洞可以利用,那么你应该考虑从头开始重新开始)。

您的分机是否总是.jpg或.png? (.jpeg怎么办?)

  

我需要将图像名称重命名为txt。名称文件

我认为您的意思是您希望保留的图像文件上的文件名与txt文件的文件名相匹配 - 您可以在move_uploaded_file()中指定目标名称。将流程分解为步骤是管理复杂性的一个好主意,但文件操作既昂贵又多余。

所以你真的想知道如何在没有扩展名的情况下获取文件名,然后从不同的文件中添加文件扩展名。

您已在$ Image_extension中添加了要应用的扩展程序

虽然你可以做....

$destination_name=$destination_path 
   . str_replace('.txt', '.' . $image_extension, $Textfile_name);

但是,如果$ imageFile_name包含多个“.txt”实例,则无法正常工作。

您可以使用PCRE指定您只应将更改应用于字符串的结尾:

$destination_name=preg_replace("/txt$/i", $image_extension, $Textfile_name);

或者从$ Textfile_name中删除最后4个字符,然后附加图像扩展名。

$destination_name=substr($Textfile_name, 0, -3) . $image_extension;

或者strrev文本文件名,用strrev($ image_extension)替换'txt',限制为1,然后strrev结果以确保只替换字符串的结尾。

如果我花更多的时间考虑这个问题,可能还有其他解决方案 - 但它们会变得越来越深奥。

一般来说,允许用户为在文档根目录中上传的内容选择自己的名字(甚至是其中的一部分)是个坏主意。

相关问题