重命名文件同时从多个文件夹上传和删除选定的文件


rename File While Uploading and deleting selected files from mutiple folder

这是我的代码,我的问题是如何在上传时重命名文件,包括上传文件大小。这样我也可以在报告中获取文件大小。

<?php
if ((!isset($_GET['admin'])) and (!isset($_GET['admin']))) {
$valid_formats = array(
    "jpg",
    "png",
    "gif",
    "zip",
    "bmp"
);
$max_file_size = 100000000000 * 100000000000; //100 kb
$path          = "../week/"; // Upload directory
$count         = 0;
if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
    // Loop $_FILES to execute all files
    foreach ($_FILES['files']['name'] as $f => $name) {
        if ($_FILES['files']['error'][$f] == 4) {
            continue; // Skip file if any error found
        }
        if ($_FILES['files']['error'][$f] == 0) {
            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name is too large!.";
                continue; // Skip large files
            } elseif (!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)) {
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            } else { // No error found! Move uploaded files 
                if (move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path . $name)) {
                    $count++; // Number of successfully uploaded files
                    $sql = "INSERT INTO pics_uploads(gf,path) VALUES('$name','$path')";
                    mysql_query($sql);
                }
            }
        }
    }
}
?>
<div class="wrap">
		<h1>Events Name</h1>
		<?php
# error messages
if (isset($message)) {
    foreach ($message as $msg) {
        printf("<p class='status'>%s</p></ br>'n", $msg);
    }
}
# success message
if ($count != 0) {
    printf("<p class='status'>%d files added successfully!</p>'n", $count);
}
?>
		<p>Max file size 10MB, Valid formats jpg, png, gif</p>
		<br />
		<br />
		<!-- Multiple file upload html form-->
		<form action="" method="post" enctype="multipart/form-data">
			<input type="file" name="files[]" multiple accept="image/*">
			<input type="submit" value="Upload">
		</form>
        <br />
  
  <center><table width="80%" border="1" bordercolor="#FF0033" >
    <tr>
    <th>MultiDelete</th>
    <td>File Name</td>
    <td>File Location</td>
    <td>Upload Date</td>
    <td>View (Click to View)</td>
    <th colspan="2">ACTION</th>
    </tr>
    <?php
$sql        = "SELECT * FROM `pics_uploads` WHERE `gf`";
$result_set = mysql_query($sql);
while ($row = mysql_fetch_array($result_set)) {
?>
        <tr>
        <td ><font color="#000"><input name="checkbox[]" type="checkbox"  value="<?
    echo $row['id'];
?>"></font></td>
        <td><?php
    echo $row['gf'];
?></td>
        <td><?php
    echo $row['path'];
?></td>
        <td><?php
    echo $row['curtime'];
?></td>
        <td><a href="../week1/<?php
    echo $row['gf'];
?>" target="_blank"><?php
    echo '<img src="../week1/' . $row['gf'] . '" width="40px" height="20px" border="2" />';
?></a></td>
        <td><a href="javascript:remove(<?php
    echo $row['id'];
?>)">Delete file</a></td>
                        
        </tr>
        <?php
}
?> <tr>
<td>
	<input name="delete" type="submit" id="delete" value="Delete"></td>
		</tr>    
    </table></center>
     <?php
}
?>
     

这是用来删除的。想要从中删除,假设文件夹名称为第1周、第2周、第3周,但只删除其中的选定文件。

<?php
include_once 'dbconfig.php';
if (isset($_GET['remove_id'])) {
$res = mysql_query("SELECT file FROM pics_uploads WHERE id=" . $_GET['remove_id']);
$row = mysql_fetch_array($res);
mysql_query("DELETE FROM pics_uploads WHERE id=" . $_GET['remove_id']);
unlink("week1/", week2, week3, week4 . $row['file']); //this didn't work
header("Location: index-1.php");
}
?>

尝试更改上传文件名

$newfilename = round(microtime(true)). $name;
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$newfilename))