1

I'm attempting to list all the filenames of images uploaded via a form to then be sent via email.

fileupload.html:

<form method="post" action="upload.php" enctype="multipart/form-data" id="upload" >
<input type="file" name="filesupl[]" multiple id="files" />
<input type="submit" name="UploadBtn" value="Upload" class="formtext" id="UploadBtn">
</form>

upload.php:

$art_file = $_FILES['filesupl'];
$filecounter = 1;
$count = 0;
foreach ( $art_file as $i => $art_inner ){ 
                    echo $filecounter . ") " . $art_file[$i]['name'] . "<br />"; $filecounter++; 
        }
if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($_FILES['filesupl']))
    {
        $extension = pathinfo($_FILES['filesupl']['name'], PATHINFO_EXTENSION);

        // Upload files
        // loop all files
        foreach ( $_FILES['filesupl']['name'] as $i => $name )
        {
            // if file not uploaded then skip it
            if ( !is_uploaded_file($_FILES['filesupl']['tmp_name'][$i]) )
                continue;

            // skip large files
            if ( $_FILES['filesupl']['size'][$i] >= $max_size )
                continue;

            // skip unprotected files
            if( !in_array(pathinfo($name, PATHINFO_EXTENSION), $extensions) )
                continue;

            // now we can move uploaded files
            if( move_uploaded_file($_FILES["filesupl"]["tmp_name"][$i], $dir . $name) )
                $count++;
        }

    }

echo json_encode(array('count' => $count));

I've attempted numerous ways to access the 'name' of the files uploaded (the code above was one of those attempts), to no avail.

  • its a normal array, whats the issue? –  Jan 26 '17 at 20:30
  • You're trying to determine the extension too soon. `$_FILES['filesupl']['name']` is an array. – Shira Jan 26 '17 at 20:30
  • I'm attempting to output the filenames in an email using foreach, but I'm having an issue accessing that variable for each file uploaded from the nested array. Every time the code executes, I get either an empty result, or the word "Array," 5 times. – Charlie Marrero Jan 26 '17 at 20:40

1 Answers1

0

I'm attempting to list all the filenames of images uploaded ...

If your sole purpose is to get all the file names of the uploaded files, then your code should simply be like this:

foreach($_FILES['filesupl']['name'] as $name){
    // echo $name . '<br />';
}

Now if you want to refactor your code, and to make it work - use a for loop like this:

$count = count($_FILES['filesupl']['name']);
for($i = 0; $i < $count; ++$i){
    // if file not uploaded then skip it
    if ( !is_uploaded_file($_FILES['filesupl']['tmp_name'][$i]) )
        continue;

    // skip large files
    if ( $_FILES['filesupl']['size'][$i] >= $max_size )
        continue;

    // get file extension
    $extension = pathinfo($_FILES['filesupl']['name'][$i], PATHINFO_EXTENSION);

    // skip unprotected files
    if( !in_array($extension, $extensions) )
        continue;

    // now we can move uploaded files
    if( move_uploaded_file($_FILES["filesupl"]["tmp_name"][$i], $dir . $_FILES['filesupl']['name'][$i])){
        // file has been uploaded
    }
}

echo json_encode(array('count' => $count));
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • Thank you for the suggestion! I've implemented the second portion suggested. Unfortunately, I still receive an empty value when I expect to get a file name (using the foreach as per your suggestion). – Charlie Marrero Jan 26 '17 at 21:17
  • @CharlieMarrero Where in the `for` loop you're trying to get file name, and how? Please paste your code on [pastebin.com](http://pastebin.com/index.php) and give me it's link here. – Rajdeep Paul Jan 26 '17 at 21:22
  • Rajdeep Paul, here is the pastebin link to the area where I want to output the filenames: http://pastebin.com/GsnCFqBc (this is part of the upload.php page, right after files have been uploaded) – Charlie Marrero Jan 26 '17 at 23:05
  • @CharlieMarrero That's because you didn't append the file names to `$pobody`. That `foreach` loop should be like this, [http://pastebin.com/uCnJ8983](http://pastebin.com/uCnJ8983) – Rajdeep Paul Jan 26 '17 at 23:33
  • Rajdeep Paul, thanks for pointing that out! That was missing for sure. However, upon adding it, I still received an email with no filenames listed, even after successfully uploading 2 pictures. – Charlie Marrero Jan 27 '17 at 16:03
  • @CharlieMarrero Okay, just for the debugging purpose do `var_dump($_FILES);` and check whether your file information is there or not. Simultaneously, paste the output of `var_dump($_FILES);`on [pastebin.com](http://pastebin.com/index.php) and give me it's link here. – Rajdeep Paul Jan 27 '17 at 17:25
  • Rajdeep Paul, here is the link to the output from the var_dump: http://pastebin.com/QufefibL – Charlie Marrero Jan 27 '17 at 21:50
  • @CharlieMarrero From the `var_dump(...);` I can see the files are not being uploaded, since `$_FILES` information is empty. Go through this SO thread to debug this issue further, [http://stackoverflow.com/questions/3586919/why-would-files-be-empty-when-uploading-files-to-php](http://stackoverflow.com/questions/3586919/why-would-files-be-empty-when-uploading-files-to-php) – Rajdeep Paul Jan 27 '17 at 21:54
  • I see. The files do get uploaded (I receive the copy on the server), but the file names never reach the mailer. Thank you kindly for your help. – Charlie Marrero Jan 30 '17 at 15:28