I have a basic upload form:
<form method="post" action="" enctype="multipart/form-data" >
<input type="file" name="logo">
<input type="submit" class="button-primary" value="Upload Image">
</form>
And this is how I upload stuff (these are WordPress functions, but the question is rather php-related, so I’m asking here, not on wp-se):
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$uploaded = insert_attachment($file,$post_id);
$uploaded_src = wp_get_attachment_url($uploaded);
update_option('logo', $uploaded_src);
}
}
Now, there are two issues and I’m not sure how to fix them:
-
When user uploads a file and clicks “Upload image” the image is being uploaded. But if user refreshes the page the iamge is uploaded once again. and again, and again. I believe the form is sending itself after refreshing, what’s the easiest way of repairing that?
-
As you can see my code updates only one option called “logo”, how to get name of upload fields and pass it to foreach loop so I’ll be able to put more upload fields on my page? I mean something like:
update_option('ThisFormUploadInputID', $uploaded_src);.
Thanks a lot! 🙂
PRG pattern – on successful POST, send
Locationheader and terminate the script. Next request from browser will be GET, and refreshing the page won’t resend the form.As for the second question,
print_r($_FILES)might help.