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++;
    }
3
  • 1
    What makes you "believe you experience an error"? I'd suggest you post some more code here, what I can see now could probably work. Commented Dec 7, 2012 at 23:14
  • Why don't you try it first, the ask the question Commented Dec 7, 2012 at 23:21
  • I added the whole loop... I am having issues in general with it, all the data IS being uploaded to the MySQL database but the file IS NEVER reaching the destination folder @Eritrea ... I did try it first and tried to fix it. My main question was can I but variables in the [] like that... Commented Dec 7, 2012 at 23:21

2 Answers 2

1
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.

Sign up to request clarification or add additional context in comments.

6 Comments

I tried this but it is still not working... There may be something messed up with my directory listing? I have the folder images in my public_html directory but this script is in admin/upload.php but using the ../ it should be going back to the public_html directory and then the images directory? Correct? It used to work but I added in the loop to do multi-file upload and only this part got screwed up...
Not necessarily. I use absolute paths when I handle files: $_SERVER['DOCUMENT_ROOT']."/mysite/media/images/".$newFileName - obviously replacing the path with your actual path... Dumb question, but are you sure your form has the enctype="multipart/form-data" param set?
No, you're trying to help so it's not a dumb question and yes I do. It used to work yesterday but I wanted to make it more dynamic, not one file at a time so I made it so you could do 20 at once... And I will use the root and see if that produces any positive results.
I just tried that and even "echo'ed" out the the server root with the images/file concatenated onto it and it was exact but the file does not exist, the folder is still empty.
$directory = $_SERVER['DOCUMENT_ROOT'].'/images/'.$newFileName; if(move_uploaded_file($_FILES['fileField']['tmp_name'], $directory)) { $NOTICE = 'File successfully uploaded!'; }
|
1

Don't use copy. Use move_uploaded_file first, before you do anything with the uploaded file. Files are uploaded to a special, temporary location.

1 Comment

I tried this but it is still not working... There may be something messed up with my directory listing? I have the folder images in my public_html directory but this script is in admin/upload.php but using the ../ it should be going back to the public_html directory and then the images directory? Correct? It used to work but I added in the loop to do multi-file upload and only this part got screwed up...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.