[英]PHP Upload Two Different File Types Using One Form With Two Different Inputs Into Two Different Directories
[英]php - two file inputs two different folder
我在這里問(因為我今天已經在互聯網上搜索了很多東西)
就是這種情況:有一個帶有某些字段的表單,然后有2個輸入type =“ file”。 關鍵是我希望文件A進入文件夾A,文件B進入文件夾B。
我試過的
這是projs_upload.php
$uploaddirb = '../images/projects/';
$uploaddira = '../images/projects/thumb/';
$uploadfile = $uploaddira . basename($_FILES['min_img']['name']);
$uploadimg = $uploaddirb . basename($_FILES['img']['name']);
move_uploaded_file($_FILES['min_img']['tmp_name'], $uploadfile);
move_uploaded_file($_FILES['img']['tmp_name'], $uploadimg);
這是表格:
require("common.php");
if($_POST){
$query = "
INSERT INTO projects (
`idprojects`,
`cat`,
`name`,
`desc`,
`min_img`,
`img`,
`ext_url`
) VALUES (
:idprojects,
:cat,
:name,
:desc,
:min_img,
:img,
:ext_url
)
";
$query_params = array(
':idprojects'=> "",
':cat' => $_POST['cat'],
':name' => $_POST['name'],
':desc' => $_POST['desc'],
':min_img' => $_FILES['min_img']['name'],
':img' => $_FILES['img']['name'],
':ext_url' => $_POST['exturl']
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
include ("funcs/proj_uploads.php");
header("Location: ?p=manage_projects");
die("Redirecting done");
}
<form action="?p=manage_projects" method="post" enctype="multipart/form-data">
<input type="text" placeholder="kategoria" name="cat" />
<input type="text" placeholder="nazwa projektu" name="name" />
<input type="text" placeholder="link do projektu" name="exturl" />
<textarea placeholder="opis" name="desc" />
</textarea>
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
<input type="file" placeholder="miniatura" name="img" />
<input type="file" placeholder="duży" name="img" />
<button type="submit" class="expand">Zapisz</button>
</form>
和“?p = manage_projects”是指表單頁面。
$uploaddirb
路徑必須以/
結尾,否則文件將作為thumbsomename
文件上傳到images/projects
中:
$uploaddirb = '../images/projects/thumb/';
如果這不能為您解決問題,請使用更多信息更新您的問題,例如HTML和PHP代碼,兩個文件都無效嗎?是否檢查目錄是否存在並具有適當的權限?
您的兩個file input
標記都稱為img
。
您應該有一個叫img
,一個叫min_img
這樣:
<input type="file" placeholder="miniatura" name="min_img" />
<input type="file" placeholder="duży" name="img" />
代替:
<input type="file" placeholder="miniatura" name="img" />
<input type="file" placeholder="duży" name="img" />
注意:應該可以在if($_POST)
塊中使用var_dump($_FILES)
輕松找到它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.