簡體   English   中英

從多個文件夾上載和刪除所選文件時重命名文件

[英]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 } ?> 

這是為了刪除。 想要刪除讓我們說文件夾名稱是week1,week2 week3,周但只刪除它中的選擇文件。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM