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 8957649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:02:37+00:00 2026-06-15T15:02:37+00:00

I am creating an upload script with PHP and HTML that could have any

  • 0

I am creating an upload script with PHP and HTML that could have any number of file uploads. I loop through and echo out any number of file uploads and name each one “fileField1”, “fileField2” and so on using a loop.

Now I want to retrieve the name of the file in that field. Can I use a variable in another loop like this?

  $fileField = 'fileField'.$i;

    $fileName = $_FILES[$fileField]['name']; // Name of file 

This is contained within the loop, will this work?

I believe I am experiencing an error…

Variable $i is increased by one each time ($i++)

EDIT

Notice the comment that I never reach…

Here is the whole loop:

$i = 1;
while ($i <= $numberOfFields) {
$fileField = 'fileField'.$i;
$titleId = 'title'.$i;
$genreId = 'genre'.$i;

    $fileName = $_FILES[$fileField]['name']; // Name of file 

    if($fileName !=""){
        $randNum = rand(1000000000,9999999999); //Generate random number

        $newFileName = $fileName."-".$randNum; //Rename file

            if (file_exists($baseUrl."images/".$newFileName)){
            echo $newFileName." already exists. ";
            }else{      
                if(isset($_POST[$titleId]) && !empty($_POST[$titleId])){
                    // Assign url, title, genre
                    $url = 'images/'.$newFileName;          
                    $title = $_POST[$titleId];          
                    $genre = $_POST[$genreId]; 
                    // Execute final query and upload
                    $sql = "INSERT INTO images (url,title,genre) VALUES (:url,:title,:genre)";
                    $q = $conn->prepare($sql);
                    $q->execute(array(':url'=>$url,
                                      ':title'=>$title,
                                      ':genre'=>$genre));       
                    if(copy($_FILES['fileField']['tmp_name'],'../images/'.$newFileName)) {
                        $NOTICE = 'File successfully uploaded!';
                        // I AM NEVER REACHING THIS NOTICE WHICH IS ECHOED LATER ON IN THE CODE
                    }
                }else{
                    $NOTICE = "Please fill out the title for picture ".$i;
                    break;                  
                }
            }
    }else{
        $NOTICE = "Please select a file to upload in slot ".$i;
        break;
    }
    $i++;
    }
  • 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-15T15:02:38+00:00Added an answer on June 15, 2026 at 3:02 pm
    if(copy($_FILES['fileField']['tmp_name'],'../images/'.$newFileName)) {
    

    I believe should be:

    if(move_uploaded_file($_FILES['fileField']['tmp_name'],'../images/'.$newFileName)) {
    

    And yes, you can put variables in [] like that. So long as they’re valid and won’t result in an undefined offset error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script that receives uploads and it works fine in mamp,
I have a script that uploads a file and then moves it to a
I am creating file upload script and I'm looking for the best techniques and
I'm creating something that includes a file upload service of sorts, and I need
TLDR; How do I reference a temp file upload POSTed to a PHP script
How do I start with creating a file upload mechanism that works via Ajax?
I'm creating a script to only upload zip files and I have the following
I have a php script and using Cpanel interface Im creating a cron job
I am creating an AJAX PHP upload script, but I am having troubles. After
I am creating an upload script for images and have a working script although

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.