1

Currently I am running this for loop which generates some content

$total = count($_FILES['uploaded_file']['name']);
$str="";    
for($i=0; $i<$total; $i++) 
{
    $content="";
    $str=$str."";
    $file_path = "v/";
    $tmpFilePath = $_FILES['uploaded_file']['tmp_name'][$i];
    $ogfilename = $_FILES['uploaded_file']['name'][$i];
    $ogfilesize = $_FILES['uploaded_file']['size'][$i];
    $filename=$_FILES['uploaded_file']['name'][$i];
    $str=$str.'<div style="border-bottom: 1px solid #eee; padding-bottom: 10px;">Song Name: '.$ogfilename.' <br>URL: <a href="http://traxchive.com/v/'.$rand.'">v/'.$rand.'</a><br>File Name: '.$filename.'<br>Size: '.$val.'</div>';   
}
$str=$str."";
echo $str;
print '<textarea style="width: 100%"><a href="/v/'.$rand.'">'.$ogfilename.'</a></textarea>';

I'm trying to make it so that all of the results which are returned within the for loop will be displayed inside of a <textarea> tag as you can see being declared at the bottom. (this only returns the last result how ever if numerous files are uploaded)

enter image description here

How can I make it so that all of the results will return in the <textarea> portion?

Land
  • 119
  • 1
  • 5
  • Concatenate the variable in the loop and output the variable in the textarea. – Script47 Sep 26 '17 at 09:26
  • Instead of writing `$str = $str."...";` you could write `$str .= "...";`. I know the difference is minimal, but perhaps you like it? – KIKO Software Sep 26 '17 at 09:29
  • My first question would be: why would you want to? textareas are for input, not output. You can format the data much more nicely using HTML, as you do now. Do you plan to submit this data back again in text format, for some reason? Or allow the user to edit something? Probably that could be done in the original request that uploaded the files, instead of by this method. – ADyson Sep 26 '17 at 09:36
  • Why cant you use $str in text area print ''; – Vikas Gautam Sep 26 '17 at 09:43

3 Answers3

2

In your loop where you set the file name, instead of overwriting the vairable you can concatenate instead,

Change,

$ogfilename = $_FILES['uploaded_file']['name'][$i];

To,

$ogfilename .= $_FILES['uploaded_file']['name'][$i] . '&#13;&#10;';

This can be done with any string variable where you require it to add on to instead of overwriting.

Reading Material

&#13;&#10;

Script47
  • 14,230
  • 4
  • 45
  • 66
  • Messed around with this for a bit however having a hard time understanding how I can make my entire request duplicate and not just the title or the id. `'.$ogfilename.'` is the data that needs to be looped, not just the title, will continue messing with it though thanks for teaching me something new! – Land Sep 26 '17 at 09:42
  • **Sanity Check:** Is `$rand` expected to be different or the same? I ask because you could generate the whole link in the loop instead if you want. Then simply output the variable in the textarea outside of the loop. – Script47 Sep 26 '17 at 09:45
  • `$rand` is random. – Land Sep 26 '17 at 09:46
2

Try Something like below:

$total = count($_FILES['uploaded_file']['name']);
    $str="";
    $textareacontent = "";   
    for($i=0; $i<$total; $i++) 
    {
        $content="";
        $str=$str."";
        $file_path = "v/";
        $tmpFilePath = $_FILES['uploaded_file']['tmp_name'][$i];
        $ogfilename = $_FILES['uploaded_file']['name'][$i];
        $ogfilesize = $_FILES['uploaded_file']['size'][$i];
        $filename=$_FILES['uploaded_file']['name'][$i];
        $str=$str.'<div style="border-bottom: 1px solid #eee; padding-bottom: 10px;">Song Name: '.$ogfilename.' <br>URL: <a href="http://traxchive.com/v/'.$rand.'">v/'.$rand.'</a><br>File Name: '.$filename.'<br>Size: '.$val.'</div>';
        $textareacontent .= '<a href="/v/'.$rand.'">'.$ogfilename.'</a>&#13;&#10;';
    }
    $str=$str."";
    echo $str;
    print '<textarea style="width: 100%">'.$textareacontent.'</textarea>';
Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33
0

Thats because $ogfilename get overridden every time the loop ran. You need to make the $ogfilename variable the same as $str, that way it collect a list of in a string format.

$total = count($_FILES['uploaded_file']['name']);
$str=""; 
$cont = "";
for($i=0; $i<$total; $i++) 
{
    $content="";
    $str=$str."";
    $file_path = "v/";
    $tmpFilePath = $_FILES['uploaded_file']['tmp_name'][$i];
    $ogfilename = $_FILES['uploaded_file']['name'][$i];
    $ogfilesize = $_FILES['uploaded_file']['size'][$i];
    $filename=$_FILES['uploaded_file']['name'][$i];
    $cont=$cont.'<a href="/v/'.$rand.'">'.$ogfilename.'</a>';
    $str=$str.'<div style="border-bottom: 1px solid #eee; padding-bottom: 10px;">Song Name: '.$ogfilename.' <br>URL: <a href="http://traxchive.com/v/'.$rand.'">v/'.$rand.'</a><br>File Name: '.$filename.'<br>Size: '.$val.'</div>';   
}
$str=$str."";
echo $str;
print '<textarea style="width: 100%">'.$cont.'</textarea>';

In this case its lot easier to make a list of strings that are links

Prav
  • 2,785
  • 1
  • 21
  • 30
  • @Placeholder How do you mean title get duplicated on multiple upload? `$cont = "";` runs on every upload, so it always rest to an empty string on every upload. Only possible way to have a duplicate title is if the upload has duplicate titles – Prav Sep 26 '17 at 10:22