kWeglinski
kWeglinski

Reputation: 411

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". The point is that I want the file A go to folder A, and the file B go to folder B.

what I tried:

this is 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.

Upvotes: 0

Views: 133

Answers (2)

Ben
Ben

Reputation: 21249

Both your file input tags are called img.

You should have one called img and one called 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.

Upvotes: 0

thpani
thpani

Reputation: 423

Your $uploaddirb path needs to end in a /, otherwise files will be uploaded into images/projects as files thumbsomename:

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

If this doesn't solve it for you, update your question with more information, i.e. both HTML and PHP code, do both files not work?, have you checked the directories exist and have proper permissions?

Upvotes: 1

Related Questions