Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8513843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:41:43+00:00 2026-06-11T04:41:43+00:00

i have this code that submits multiple files and titles im trying to combine

  • 0

i have this code that submits multiple files and titles im trying to combine the results to upload to my DB and check if title[] is empty and print a custom value but im having problems with the title[] i need to combine with the s_upload[] array:

<?php
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ){

foreach ($_FILES["s_upload"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["s_upload"]["tmp_name"][$key];
        $name = $_FILES["s_upload"]["name"][$key];
       // move_uploaded_file($tmp_name, "data/$name");

       if ($_POST['title']==''){
       echo 'Title';
       }else{
       print_r ($_POST['title']);
       echo $name;
       }
    }
}   
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>

</head>
    <body>


<form method="post" enctype="multipart/form-data">
<div class='file_upload' id='f1'>
<input type="text" name="title[]" id="t1">
<input size="14" name='s_upload[]' id="i1" type='file'/>
</div>
<div class='file_upload' id='f2'>
<input type="text" name="title[]" id="t2">
<input size="14" name='s_upload[]' id="i2" type='file'/>
</div>
<input type="submit"/>
</form>


    </body>
</html>

when i submit this is are the results:

Array ( [0] => 11111 [1] => 22222 ) 1.jpgArray ( [0] => 11111 [1] => 22222 ) 2.jpg

i need this results if title exists:

1111 1.jpg 
2222 2.jpg

and this results if title is empty:

Title 1.jpg 
2222 2.jpg
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:41:44+00:00Added an answer on June 11, 2026 at 4:41 am

    Since you didn’t specify where the 1111 came from, I’ll give you a few options:

    <?
    if ( $_SERVER['REQUEST_METHOD'] == 'POST' ){
        foreach ($_FILES["s_upload"]["name"] as $key => $name) {
            if ($_FILES["s_upload"]["error"][$key] == UPLOAD_ERR_OK) {
                $tmp_name = $_FILES["s_upload"]["tmp_name"][$key];
    
                if ($_POST['title'][$key]==''){
                    // No title was specified: construct default title
                    // This defaults the title to the filename of the file that was uploaded
                    $title = $name;
                    // This defaults the title to some random 32-character hex string
                    $title = md5(time()+rand()); 
                }
                else{
                    // A title was specified in the input box: use it
                    $title = $_POST['title'][$key];
                }
                echo "$title $name<br />";
            }
        }
    }
    ?>
    

    Here’s what I changed:

    1. I’m now looping over the name instead of the error. It just makes a lot more sense to do it this way. Looping over the error was a source of confusion.
    2. Since you named your inputs using an array (the []) in the input name, you have to specify which one you’re referring to in your code. This means you need to use $key consistently throughout.
    3. You said you wanted a default title when one wasn’t specified, but didn’t explain how you want it constructed. I threw two random ideas in there. The first defaults to the filename of the uploaded file. The second is just a random string. You could change this to something based off the current time, the IP of the uploader, etc. etc. In any case, you’d do it there.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to emulate the multiple upload files in html form that looks
I have this code that I've edited: http://pastebin.com/vrqHek6S I've put in comments where I
I have this code that runs but never stops. class A { public static
I have this code that reads from XML file. It gets five strings (groupId,
I have this code that suppose to place the marker by the latlng public
I have this code that where I would normally use one line: if (tableView
I have this code that works in a unit test but doesn't work when
I have this code that I want to make point-free; (\k t -> chr
I have this code that fetches some text from a page using BeautifulSoup soup=
I have this code that changes the opacity of the div on hover. $(#navigationcontainer).fadeTo(slow,0.6);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.