PDA

View Full Version : using arrays when uploading a file



iwiniquit
April 28th, 2007, 12:51 AM
i'm trying to upload multiple files using arrays..which works fine..but i'm specifically trying to define a variable based on the number of the array.

here's my code to see what i mean:


if ($_POST['action'] == 'editmultiple')
$files = array();
foreach ($_FILES['photosnews'] as $k => $l) {
foreach ($l as $i => $v) {
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
##### THIS IS WHERE I'M HAVING TROUBLE ####
if ($_FILES['photosnews'] == 1) {
$vartest = "this is one";
} else {
$vartest = "this is not one";
}
#################################
}
}so my logic is that each file has a specific array number starting from 0..when the file has an array number of 1, i want to set a variable to something..and then if it's 2, i want it to set to something different..i'm just not sure what to put in that if statement so that it knows what array number does what. i'm thinking maybe a for statement would be better..but i'm not sure how to get it work with what i have so far.

here's part of the form too if needed:

<input name="photosnews[]" type="file" class="input" size="10" accept="image/gif, image/jpeg, image/jpg" >
<input type="hidden" name="action" value="editmultiple" />any help would be greaty appreciated!

-matt

puppy
April 28th, 2007, 01:01 AM
what's $k ?

iwiniquit
April 28th, 2007, 01:08 AM
on each loop the current element's key will be assigned to $k (just like the more common used variable, $key).

Sniper Jo
April 28th, 2007, 09:37 AM
$filenum = count($_FILES['photosnews']); // Amount of elements in array

then use either if or a switch statement

so ure if statement would go like


if ($filenum == 1) {
$vartest = "this is one";
} else {
$vartest = "this is not one";
}

puppy
April 28th, 2007, 10:40 AM
so what's "key"? isn't it a number you need?

iwiniquit
April 28th, 2007, 12:49 PM
hey sniper..doesn't that just give me the total of items in the array? i want to get the number of the item that's currently being pulled up by that for statement.

BetaWar
April 28th, 2007, 01:07 PM
Go here for help on this:
http://us.php.net/features.file-upload

Here is the type of thing you will need in the end:


<form action="" method="post" enctype="multipart/form-data">
<p>Pictures:
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="submit" value="Send" />
</p>
</form>

<?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "data/$name");
}
}
?>

You will need to customize it for what you want it to do, but it is a multi file upload system.

iwiniquit
April 28th, 2007, 01:49 PM
thanks betawar..but i already have the actually uploading part figured out. i just need to customize what i have so i can grab individual array numbers.

what i'm actually doing is updating a mysql database once the file is uploaded so that the database knows the new filename/path. but in order to do that i need to call an if function or something so i can grab an entry in the db that it already recognizes so i can update only that one entry.

basically..in the form i have three images, and three upload items. when you click submit, it's supposed to update the row with the new selected image. each image/uploader represents a row in the database. when you select a file and upload it i want it to know which item/row it's coming from so i can only update the path to that specific row. right now it just uploads all of the three rows to the same path/file because it can't get a specific id number..so all of the rows get updated.

Sniper Jo
April 28th, 2007, 03:09 PM
hey sniper..doesn't that just give me the total of items in the array? i want to get the number of the item that's currently being pulled up by that for statement.

then why dont u set a varible to 0 before the foreach statement and add one to the value just after the foreach statement..

iwiniquit
April 28th, 2007, 03:21 PM
hmm..can you give me an example on where it'd go? i'm not sure where to put it.

iwiniquit
April 29th, 2007, 03:20 AM
nevermind..problem solved. thanks sniper.