Jump to content

inserting variable into image upload name


kestrel

Recommended Posts

I've been working to have it so users of a database can upload a few images.  The images would be stored in an image directory and have the file name of username-pic1, username-pic2...

 

The code I have below pulls the user name from a previous posted form. 

 

There are several echo statements that are there only so I can see that variables are being passed along.

 

Right now all the variables are being held fine until the form is processed and then the username variable goes dead.  So the echo shows that it will be uploading the file as "username-pic1" but instead it uploads it as "-pic1".

 

I've been looking at this way too much and I've have tried a number of minor variations but with no luck.

 

Here is the code. 

 

<form enctype="multipart/form-data" action="" method="post">
First picture: <input name="userfile[]" type="file" /><br />
<input type="submit" value="Upload" />
</form>

<?PHP
session_start();
?>

<?php
$user = $_POST['record'];
echo "username = $user<br><BR>";

  $success = 0;
  $fail = 0;
  for ($i=0;$i<1;$i++)
  {
   $loc1 = "images/" ;
   $loc2 = "pic1." ;
   $uploaddir = $loc1 . $user . "-" . $loc2 ;
   echo "new file name = ".$uploaddir.$loc2 ;
   if($_FILES['userfile']['name'][$i])
  {

  $uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$i]);
    $ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
    if (preg_match("/(jpg|gif|png|bmp)/",$ext))
    {
     if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $uploaddir . $ext))
     {
     $success++;
     echo "File $success uploaded successfully.";
     echo "uploadfile  =  ";
     echo $uploadfile;
     echo "<br /> user  =  ";
     echo $_POST['record'];
     echo "<br /> uploaddir  =  ";
     echo $uploaddir;
     echo "<br /> new file name  =  ";
     echo $uploaddir.$ext;
   }
     else
     {
     echo "Error Uploading the file. Please try again.\n";
     $fail++;
     }
    }
    else
    {
     $fail++;
    }
   }
  }

?>

 

Does anyone have any suggestions as to what I am missing here?

 

Thanks,

 

Jim

Link to comment
Share on other sites

Ok, never mind.  I took a break, ate lunch, played with the kids, and came back a bit more refreshed.  I realize now that I forgot to pass along the variables with hidden input fields.  I spent all my time going over the php script and forgot to look at the html code.

 

oops...thanks for looking anyway.

 

Jim

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.