dongqin5604
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
    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);
    
    }
    
    点赞 评论
  • duandazhen7306
    duandazhen7306 2016-06-23 22:48

    Update: I used Mani's code (which helped a lot) and it now works. Here is the key bit...

    html:

        <p>Select the course from the box and then select the .pdf outline...</p>
    
            <?php
            for ($n=0; $n<10; $n++) {
            ?>
            <p>
                    
            <select name="new_name[<?php $n ?>]" value="">
                <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>

    and here is the updated php (which is towards the top of the page)

    <?php 
    
    // Makes directory and changes file name for many files.  
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        
        $uploads = $_POST['name'];  // folder name
            
        if (!is_dir($uploads))
        {
            mkdir($uploads);
        }
        
        $count = count($_FILES['files']['name']);
    
        for($i=0; $i<$count; $i++) {    
    
        $temp = explode(".", $_FILES["files"]["name"][$i]);
        $new_name = $_POST['new_name'][$i];
        $newfilename = $new_name. '.' . end($temp);
        $target = $uploads.'/'.$new_name.".jpg";
        move_uploaded_file($_FILES["files"]["tmp_name"][$i], $target);
    
        }
            
        }
    
    ?>

    The above makes a folder depending on the teachers' name and then allows them to upload their files (renaming them correctly). It will automatically over write old files which is what I want it to do. It does not yet invalid / unexpected data.

    </div>
    
    点赞 评论

相关推荐