简体   繁体   English

PHP-两个文件输入两个不同的文件夹

[英]php - two file inputs two different folder

I'm here to ask (cause I've searched today tons of internet for this) 我在这里问(因为我今天已经在互联网上搜索了很多东西)

Here's the case: got one form with some fields, and then it have 2 inputs type="file". 就是这种情况:有一个带有某些字段的表单,然后有2个输入type =“ file”。 The point is that I want the file A go to folder A, and the file B go to folder B. 关键是我希望文件A进入文件夹A,文件B进入文件夹B。

what I tried: 我试过的

this is projs_upload.php 这是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);

this is form: 这是表格:

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>

and "?p=manage_projects" refers to the form page. 和“?p = manage_projects”是指表单页面。

Your $uploaddirb path needs to end in a / , otherwise files will be uploaded into images/projects as files thumbsomename : $uploaddirb路径必须以/结尾,否则文件将作为thumbsomename文件上传到images/projects中:

$uploaddirb = '../images/projects/thumb/';

If this doesn't solve it for you, update your question with more information, ie both HTML and PHP code, do both files not work?, have you checked the directories exist and have proper permissions? 如果这不能为您解决问题,请使用更多信息更新您的问题,例如HTML和PHP代码,两个文件都无效吗?是否检查目录是否存在并具有适当的权限?

Both your file input tags are called img . 您的两个file input标记都称为img

You should have one called img and one called min_img 您应该有一个叫img ,一个叫min_img

So do: 这样:

<input type="file" placeholder="miniatura" name="min_img" />
<input type="file" placeholder="duży" name="img" />

instead of: 代替:

<input type="file" placeholder="miniatura" name="img" />
<input type="file" placeholder="duży" name="img" />

NB: This should be easily spotted with a var_dump($_FILES) in your if($_POST) block. 注意:应该可以在if($_POST)块中使用var_dump($_FILES)轻松找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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