A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: PHP var construction (simple problem)

  1. #1
    Phantom Flasher... Markp.com's Avatar
    Join Date
    May 2000

    Cow Icon PHP var construction (simple problem)

    Hi people, I've forgotten how to do something in PHP and its driving me mad!

    I'm trying to do a simple thing, upload images and save their paths to a database.

    It all works fine, I just can't remember how to create a variable from a string.

    Basically the variables going in to the Database are Img1, Img2, Img3, Img4.

    My code below is a loop that writes out each image as they are uploaded.

    How do I set the variable Img1 with the path of the file, inside the loop?

    I can't do $Img1 = $uploadfile; as that will always write out $Img1 with each pass of the loop with the current image path.

    I need to do something like
    $Img.$b = $uploadfile;
    each time the loop runs, but I don't think $Img.$b will work.

    Here is my code
        $uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$i]);
        $ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
        if (preg_match("/(jpg|gif|png)/",$ext))
         if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $uploadfile))
    // Here I want to add the variable for each image ready to go in to a database.
    		echo "Img".$imagenum." ".$uploadfile."<br>";
    		$imgset = "Img".$imagenum;
    		echo "<img src=\"".$uploadfile."\"><br>";
    Any ideas?

  2. #2
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    You can just use an array:
    PHP Code:
    $images = array();

    $images[$i] = $uploadfile;

    That's just some rough pseudo-code, but it just shows you how to use an array.

    BUT, if you really wanted to work with dynamically named variables, you could do this:
    PHP Code:
    $image 'Img'.$i;
    $image $uploadfile;

    Notice the $$? It means use the value of $image as the variable name. So in this instance, you'd have access to variable names such as $Img0, $Img1, etc. But really, don't do that. Just use an array.
    Last edited by MyFriendIsATaco; 07-17-2009 at 03:39 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width

HTML5 Development Center