Forum Moderators: coopster

Message Too Old, No Replies

Variable name change in a loop

         

dombi

8:47 pm on Apr 1, 2003 (gmt 0)

10+ Year Member



What my script does takes a file upload and stores it in a specific folder on the server. I have several file uploads, so I figured I would use a loop to store all three of them. I cannot get the loop to work.
Here is a little sample that explains my problem.

for ($counter=1; $counter < $max_number_of_images; $counter++)
{
if (is_uploaded_file($imgfile["$counter"]))
{
$uploads = "uploads";
$images_dir = "$uploads/$id";
....

My file uploads are called imgfile1, imgfile2, and imgfile3. I want this for loop for go through all those. How can I tell php that in my if statement the counter variable should show up only as a 'PHP text'?

Basically php should read the if statement as:

if (is_uploaded_file($imgfile1))

if (is_uploaded_file($imgfile2))

if (is_uploaded_file($imgfile3))

Thank you, I hope that this makes sense...sorry if I am a little confusing, I am still new to PHP.

Thank you,

dombi

andreasfriedrich

9:03 pm on Apr 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have a look at the PHP [php.net] manual for variable variables [php.net].


$imgfile1 = 'Aaron';
$i = 1;
echo ${"imgfile$i"};

will print Aaron.

Since you can access uploaded files by the $_FILES superglobal you can use this code as well:


$_FILES["imgfile$1"]

Andreas

dombi

9:51 pm on Apr 1, 2003 (gmt 0)

10+ Year Member



Thank you so much for your help....I got it working. Whoo Hoo! It alwasy feels good to get things going after a little stall. So here is what I ended up changing:

for ($counter=1; $counter < $max_number_of_images; $counter++)
{
if (is_uploaded_file(${"imgfile".$counter}))
{
$uploads = "uploads";
$images_dir = "$uploads/$id";

So I can do something like this with a variable:

${"string".$counter."string".$counter2."string"}

Thank you once again!

dombi