How To Allow Image Upload Through Form?

Discussion in 'Programming' started by lokielookies, Sep 4, 2008.

  1. #1
    Hi

    I'm looking for the way to allow people to upload several images through an (existing) form.

    Let's say this is my form:

    <form id="form1" name="form1" method="post" action="result.php">
      <label>Name:
      <input name="name" type="text" id="name" />
      </label>
      <p>
        <label>Email:
        <input name="email" type="text" id="email" />
        </label>
      </p>
      <p>
        <input type="submit" name="Submit" value="Submit" />
      </p>
    </form>
    
    Code (markup):
    And this is the code of the output page:

    <p>Name: <?PHP echo $_POST["name"];?></p>
    <p>Email: <?PHP echo $_POST["email"];?></p>
    <p>Show Pic 1: ?????</p>
    <p>Show Pic 2: ?????</p>
    <p>Show Pic 3: ?????</p>
    PHP:

    How can I add several fields to the form that allow people to browse their machine for a file and make sure the pics appear on the output page?
     
    lokielookies, Sep 4, 2008 IP
  2. saurabhj

    saurabhj Banned

    Messages:
    3,459
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    0
    #2
    saurabhj, Sep 4, 2008 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Your form will be something like this:

    
    <form action="result.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
      <label>Name:
      <input name="name" type="text" id="name" />
      </label>
      <p>
        <label>Email:
        <input name="email" type="text" id="email" />
    </label>
      </p>
      <p>
        <label>File 1:
        <input name="file1" type="file" id="file1"> 
        </label>
      </p>
      <p>
        <label>File 2:
        <input name="file2" type="file" id="file2"> 
        </label>
      </p>
      <p>
        <label>File 3:
        <input name="file3" type="file" id="file3"> 
        </label>
      </p>
      <p>
        <input type="submit" name="Submit" value="Submit" />
      </p>
    </form>
    
    PHP:
    Output page will be something like:

    
    <?php
    	$file1 = $_FILES['file1']['name'];
    	$file2 = $_FILES['file2']['name'];
    	$file3 = $_FILES['file3']['name'];
    	move_uploaded_file($_FILES['file1']['tmp_name'],'files/'.$_FILES['file1']['name']);
    	move_uploaded_file($_FILES['file2']['tmp_name'],'files/'.$_FILES['file2']['name']);
    	move_uploaded_file($_FILES['file3']['tmp_name'],'files/'.$_FILES['file3']['name']);
    ?>
    <p>Name: <?PHP echo $_POST["name"];?></p>
    <p>Email: <?PHP echo $_POST["email"];?></p>
    <p>Show Pic 1: http://sitename.com/files/<?=$file1;?></p>
    <p>Show Pic 2: http://sitename.com/files/<?=$file2;?></p>
    <p>Show Pic 3: http://sitename.com/files/<?=$file3;?></p>
    
    PHP:
    I am skipping file verification (certain extensions,size,name, etc..).

    Peace,
     
    Barti1987, Sep 4, 2008 IP
  4. lokielookies

    lokielookies Well-Known Member

    Messages:
    1,246
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    150
    #4
    Great! It worked :D

    Thanks a lot!

    ;)
     
    lokielookies, Sep 9, 2008 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    That was a quick response :rolleyes:

    Peace,
     
    Barti1987, Sep 9, 2008 IP
  6. Dreads

    Dreads Well-Known Member

    Messages:
    1,884
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    150
    #6
    i would advise adding some anti-shells ? like making it more secure lol
     
    Dreads, Sep 9, 2008 IP
    Barti1987 likes this.
  7. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #7
    +rep for giving such great advise. (don't you wish everyone was as nice as me:eek:)

    Peace,
     
    Barti1987, Sep 9, 2008 IP