dongqin5604 2016-06-23 08:20
浏览 66

如何使用php使用文件名的下拉菜单上传和重命名多个文件?

I need to allow users to upload multiple files and then choose the filename using a drop-down menu. The files will be used for my schools 'options' website and the files need to be named using course codes. Unfortunately, users often misname so I want to allow them to upload files and select the course. The plan is to automatically rename them so that the code is correct. Users select their name and then upload files so that their uploads go in a dedicated folder.

I can do it for one file but I need to be able to do it for more than one file. I can upload multiple files but can't seem to rename them. The problem is that I don't know how to link multiple new file names to the array that can be created using $_FILES

Here's the code for uploading and renaming a single file (it uses .jpg files but will be changed to .pdf files in due course) ...

<?php 

// Makes directory and changes file name for one file

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    $uploads = $_POST['name'];
    if (!is_dir($uploads))
    {
        mkdir($uploads);
    }
    
    $info = pathinfo($_FILES['userFile']['name']);
    $ext = $info['extension']; // get the extension of the file
    $newname = $_POST['new_name'].".".$ext; 

    $target = $uploads.'/'.$newname;
    move_uploaded_file( $_FILES['userFile']['tmp_name'], $target);
    
}
?>

<form name="UploadForm" enctype="multipart/form-data" method="post">
    
    <p>
    Find the file(s) you want to upload and click the "Upload" button below.
    </p>
    <p>
    <b>Teacher Code:</b> 
        <select name="name">
            <option value="Mr_A">Mr A</option>
            <option value="Mr_B">Mr_B</option>
            <option value="Mr_C">Mr C</option>
        </select>
    </p>
    
    <p>Select the course from the box and then select the .pdf outline...</p>

    
        <p>
        
        <select name="new_name">
            <option value="">Choose...</option>
            <option value="L1PROG01">Programming - Level 1</option>
            <option value="L2PROG01">Programming - Level 2</option>
            <option value="L3PROG01">Programming - Level 3</option>
            <option value="L1WEBD01">Web Design - Level 1</option>
            <option value="L2WEBD01">Web Design - Level 2</option>
            <option value="L3WEBD01">Web Design - Level 3</option>
        </select>
        
        <input name="userFile" type="file" />
        
        </p>
        
    <br/>Check this box <input autocomplete="off" type="checkbox" checked name="overwrite" /> to <strong>overwrite</strong> existing files.
    <input type="submit" value="Upload" />
    </form>

</div>
  • 写回答

2条回答 默认 最新

  • doushun4666 2016-06-23 08:26
    关注

    Html

    <input name="files[]" type="file" multiple="multiple" />

    PHP

    $count = count($_FILES['files']['name']);
    
    for($i=0; $i<$count; $i++) {    
    
    $temp = explode(".", $_FILES["files"]["name"][$i]);
    $newfilename = "new_name". '.' . end($temp);
    move_uploaded_file($_FILES["files"]["tmp_name"][$i], "upload/" . $newfilename);
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)