简体   繁体   中英

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

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> 

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

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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