0
我想用預定義文件名上傳多個文件,這有可能嗎?如何從php中檢索名稱?php用預定義文件名上傳多個文件
如何從php中檢索名稱'doc1'和'doc2'?
我的代碼:
<?
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*1024; //1MB
$path = "uploads/doc/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute 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 file
}
}
}
}
?>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Ppload with PHP</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
Doc1: <input type="file" id="file" name="files['doc1']" multiple="multiple" accept="image/*" /><br>
Doc2: <input type="file" id="file" name="files['doc2']" multiple="multiple" accept="image/*" /><br>
<input type="submit" value="Upload!" />
</form>
</body>
</html>
在move_uploaded_file函數中上傳後更改文件名 –
您可以在PHP腳本中命名它。您不需要使HTML代碼變得複雜。多個陣列並能夠上傳多個文件? (這甚至不工作,因爲名稱不是多個文件數組)。使用Doc1:''然後運行PHP腳本兩次。 – AkiEru