/    Sign up×
Community /Pin to ProfileBookmark

Multiple image upload

Hey guys,

I’m just wondering the best way to approach this.

On my blog site, I have a place where I can write a blog post. Along with that I can add one image to a database.

I want to add 5 images instead, so I can display these images in association with the given blog post at a later stage.

So it works fine with 1 image and running the query once.

I tried putting the query in a for loop and looping it 5 times, and looking in the $_FILES array for the right values.

However i’m not sure if this was done correctly.

[code=php]
for ($i = 5; $i <= 5; $i += 1)
{
$imgQuery = sprintf(“insert into journal_img
set
journal_id = ‘%s’,
img_name = ‘%s’,
img_size = ‘%s’,
img_type = ‘%s’,
img_path = ‘%s’
“,
mysql_real_escape_string($fid),
mysql_real_escape_string($fileName),
mysql_real_escape_string($fileSize),
mysql_real_escape_string($fileType),
mysql_real_escape_string($filePath));
}
[/code]

The variable values have been extracted like this

[code=php]
$fileName = $_FILES[‘userfile’][‘name’];
$tmpName = $_FILES[‘userfile’][‘tmp_name’];
$fileSize = $_FILES[‘userfile’][‘size’];
$fileType = $_FILES[‘userfile’][‘type’];
[/code]

Shooting from the hip here, I guess I need to access value 0 to 4 in the $_FILES array and increment the query values by 1 on the $_FILES array each time it loops to get the right values, however that might not be the right logic.

Anyone who can help with this would be brilliant.

Thanks in advance

Alistair?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 24.2008 — How are you naming the file-type input elements in your form?
Copy linkTweet thisAlerts:
@NogDogDec 24.2008 — You can give them all the same name with a "[]" at the end, which will create a sub-array within PHP.
<i>
</i>&lt;input type="file" name="userfile[]" /&gt;

Then you can loop through them as:
[code=php]
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
// do your move_uploaded_file(), etc. here with
// $_FILES['userfile']['tmp_name'][$key], $_FILES['userfile']['name'][$key], etc....
}
else {
// echo error or whatever here if you want for failed upload
}
}
[/code]
Copy linkTweet thisAlerts:
@NogDogDec 24.2008 — You copied-and-pasted my code before I edited it to fix the copy-and-paste error I had in it. :p [b]$_FILES['picture'][/b] should have been [b]$_FILES['userfile'][/b] (or whatever you want to name the field in the form).
Copy linkTweet thisAlerts:
@NogDogDec 24.2008 — Noggin seems to be recovering well from the surgery, thank you. Tomorrow (well, today, now) I'll probably find out if it was malignant or benign, which will then determine whether or not I have a merry Christmas. ?
Copy linkTweet thisAlerts:
@NogDogDec 24.2008 — Yes: $malignant == $cancer, $benign != $cancer.
Copy linkTweet thisAlerts:
@NogDogDec 24.2008 — Just a quick follow-up on Noggin: the lab results are in and the verdict is "benign," so now I can actually enjoy the holidays.

Thanks for you concern and support.
Copy linkTweet thisAlerts:
@TopkatDec 25.2008 — Your boy Noggin looks so innocent with the round thing on his neck! He has his "im a poor soul" face on ?
Copy linkTweet thisAlerts:
@TopkatDec 25.2008 — I wish I could help, but I'm just learning PHP myself...
×

Success!

Help @a_g_r_c spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...