Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

Multiple image upload problem

images php coding issues or problems here.

Moderators: macek, egami, gesf

Multiple image upload problem

Postby ShamanFR » Mon Apr 04, 2005 10:53 am

Hey guys I am new to the forum and I was hoping that you would be able to help me out with this problem I having.

I found this code on the net that lets me upload multiple images at once. Now I have no problem getting the files into the destination but I cant seem to be able to store the name in my db. I have created a db table with 8 colums (id, title, date, txt, picture1, picture2, picture3, picture4).
Now if you look at my code you may be asking yourself what the hell is that unorganized code, but since I am a begginer at PHP and I tried modifying it to the best of my knowledge which is why I am here asking you how to fix this problem.

Here is my code:

Code: Select all
<?PHP
include("func.php");
if ($_GET['option']) {
  switch ($_GET['option']) {
      case "upload":
        echo "<form method=post action='upload.php?option=process' enctype='multipart/form-data'>\n
           <a class=txt><li> Title</a><br>
           <input type=text name=title>
           <textarea cols=55 rows=10 name=txt size=24 wrap=VIRTUAL></textarea><br>
              *<b>File:</b><br><small>(max file size 4 meg and .zip file format only)</small>\n
              <input type=file name='userfile1' size=50><br><br>\n
              *<b>File:</b><br><small>(max file size 4 meg and .zip file format only)</small>\n
              <input type=file name='userfile2' size=50><br><br>\n
              *<b>File:</b><br><small>(max file size 4 meg and .zip file format only)</small>\n
              <input type=file name='userfile3' size=50><br><br>\n
              *<b>File:</b><br><small>(max file size 4 meg and .zip file format only)</small>\n
              <input type=file name='userfile4' size=50><br><br>\n
           <input type=hidden name=assume value=true>
              <input type=submit name='submit' value='Submit File'>\n
              <br><br>Only hit Submit File once and wait for file to upload.\n
              </form>\n";
        break;
      case "process":      
      $assume = $_POST['assume'];
      $txt = $_POST['txt'];
      $title = $_POST['title'];

      
        if ($assume == "true") {
      
          $iCount = 1;
          while ($iCount != 5){
            $stFile = "userfile" . $iCount;
            if ($_FILES[$stFile]['tmp_name'] != '') {

              if (copy($_FILES[$stFile]['tmp_name'], "newsimgs/" . $_FILES[$stFile]['name'])) {
                $userfile_location = "newsimgs/";
            
                unlink($_FILES[$stFile]['tmp_name']);
              }
            }
       $iCount = $iCount + 1;
      }
     $picture1 = $_POST['$stFile'];
      $picture2 = $_POST['$stFile'];
     $picture3 = $_POST['$stFile'];
     $picture4 = $_POST['$stFile'];
    
      $dbcnx = mysql_connect("localhost", "username", "password");
     mysql_select_db("comments");
      
     $sql = "INSERT INTO pgwrldnews SET txt='$txt', title='$title', picture1='$picture1', picture2='$picture2', picture3='$picture3', picture4='$picture4'";
      if (mysql_query($sql)) {
      echo("Your comment has been added");
      } else {
      echo("Error adding entry: " .  mysql_error() . "");
       } 
    }
  }
} else {
  echo "Click <a href='upload.php?option=upload'>Upload</a> to test this program.<br><br>";
}

?>

Any help would be greatly apreciated, thanks.

Nick
ShamanFR
New php-forum User
New php-forum User
 
Posts: 2
Joined: Mon Apr 04, 2005 10:47 am
Location: France

Postby ruturajv » Mon Apr 04, 2005 8:09 pm

$picture1 = $_POST['$stFile'];
$picture2 = $_POST['$stFile'];
$picture3 = $_POST['$stFile'];
$picture4 = $_POST['$stFile'];

I don't understand this....
there is a problem with this part of code,
User avatar
ruturajv
php-forum Super User
php-forum Super User
 
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India

Postby Alexej Kubarev » Tue Apr 05, 2005 4:11 am

you must have different field names or something for each of your pictures..

in this case all four varibles:
$picture1 = $_POST['$stFile'];
$picture2 = $_POST['$stFile'];
$picture3 = $_POST['$stFile'];
$picture4 = $_POST['$stFile'];

will get the same final value... i would use an array $puctures
and do it in a loop $pictures[$i] = $_POST[$stFile]; while $stFile changes and $i increases...
Last edited by Alexej Kubarev on Tue Apr 05, 2005 3:44 pm, edited 1 time in total.
User avatar
Alexej Kubarev
Site Admin
Site Admin
 
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län

Postby ShamanFR » Tue Apr 05, 2005 9:21 am

No problem guys I got it figures out. I hardcoded it.

$picture1 = $_FILES['userfile1']['name'];
$picture2 = $_FILES['userfile2']['name'];
$picture3 = $_FILES['userfile3']['name'];
$picture4 = $_FILES['userfile4']['name'];


Thanks anyways.

Nick
ShamanFR
New php-forum User
New php-forum User
 
Posts: 2
Joined: Mon Apr 04, 2005 10:47 am
Location: France

Postby swirlee » Tue Apr 05, 2005 9:24 am

ruturajv wrote:
$picture1 = $_POST['$stFile'];
$picture2 = $_POST['$stFile'];
$picture3 = $_POST['$stFile'];
$picture4 = $_POST['$stFile'];

I don't understand this....
there is a problem with this part of code,


The problem is that variables are not expanded inside single-quotes. The correct way to do this is thus:

Code: Select all
$picture1 = $_POST[$stFile];
$picture2 = $_POST[$stFile];
$picture3 = $_POST[$stFile];
$picture4 = $_POST[$stFile];
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back


Return to PHP coding => Images

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.