简体   繁体   English

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

[英]How do I use php to upload and rename multiple files using a dropdown menu for the file names?

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 问题是我不知道如何将多个新文件名链接到可以使用$ _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) ... 这是上传和重命名单个文件的代码(它使用.jpg文件,但会在适当的时候更改为.pdf文件)...

 <?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> 

Html HTML

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

PHP 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);

}

Update: I used Mani's code (which helped a lot) and it now works. 更新:我使用了Mani的代码(它有很多帮助),现在可以使用了。 Here is the key bit... 这是关键位......

html: 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(位于页面顶部)

 <?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. 它还没有无效/意外数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM