-
Phantom Flasher...
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;
$b++;
each time the loop runs, but I don't think $Img.$b will work.
Here is my code
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>";
$success++;
$imagenum++;
}
Any ideas?
-
Bearded (M|G)od
You can just use an array:
PHP Code:
$images = array();
for($i=0;$i<$totalimages;$i++) { $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:
for($i=0;$i<$totalimages;$i+) { $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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|