Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    192
    Thanks
    112
    Thanked 0 Times in 0 Posts

    How to Upload an image to a folder, while "image name" goes in to database?

    Hello all. I am struggling.

    I have created a FORM that will upload a "business name" to a database.
    It also lets you browse for an image and uploads it's "name" to the database.

    I would also like the image to also get actually uploaded to an "uploads" folder. I have been working on this for 2 FULL days and cannot figure out how to do it. I would sincerely appreciate help from anyone.

    I know that I need to use .. enctype="multipart/form-data" in my form, but as soon as I do, the image "name" no longer get's stored in the database.?

    I have honestly lived on this for 2 days, and you can see my example at http://testing.cnjwebsolutions.com/

    You can see that I am not asking for help without trying. I also created a folder on my server called "uploads", if you could possibly help, and attached my code below. I do sincerely appreciate any help, Buffmin.
    Code:
    <?php
     function renderForm($BusinessName, $picture_name,$error) 
     { 
     ?> 
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
     <html> 
     <head>
    </head>
    <body>
    <h1> Add a New Vendor</h1><br /> 
    <form action="" method="post" form name="dform" >
    
      <label> Business Name<br />
      <input type="text" id = "BusinessName" name = "BusinessName"  size="40" >  
      </label><br /><br />  
    
      <label>Upload an Image<br />
      <input type="file" name="picture_name" <?php echo $image_name; ?> >
      </label><br /><br />
    
    <!-- Submit Button -->
      <input type="submit" name="submit" id="submit" value="Submit">
    </form>
    </body>
    </html>
    
    <?php  
     } 
     // connect to the database 
     require ('cred.php');
            $db = connectDB();
      
     // check if the form has been submitted. If it has,save it to the database 
     if (isset($_POST['submit'])) 
     {  
     // get form data, making sure it is valid  
     $Businessname = mysql_real_escape_string(htmlspecialchars($_POST['BusinessName']));
     $picture_name = mysql_real_escape_string(htmlspecialchars($_POST['picture_name']));
    
     // save the data to the database 
     mysql_query("INSERT MyDatabase SET BusinessName='$Businessname', picture_name='$picture_name' ") 
     or die(mysql_error());  
     
    mysql_query("alter table MyDatabase ")	
     or die(mysql_error()); 
      
     // once saved, redirect back to the view page 
     header("Location: view.php");  
     }  
     else 
     // if the form hasn't been submitted, display the form 
     { 
     renderForm('','',''); 
     } 
    ?>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,650 Times in 4,612 Posts
    I don't use PHP, but this took me about 30 seconds to find:

    http://www.php.net/manual/en/feature...ost-method.php

    http://www.php.net/manual/en/features.file-upload.php

    Note that you do *NOT* user $_POST to get info about the upload file, according to that documentation.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Buffmin (03-05-2012)

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    PHP Code:
    <input type="file" name="picture_name" <?php echo $image_name?> >
    You can't enter your own values for the input file name for obvious security reasons.
    Otherwise people could send all sorts of malicious code to the server.

    You can get the original filename to enter into the database from $_FILES['txtUploadFile']['name'] where txtUploadFile is the name of your file input box.

  • Users who have thanked webdev1958 for this post:

    Buffmin (03-05-2012)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,650 Times in 4,612 Posts
    Ahhh...I didn't even notice that he was attempting to *SPECIFY* the file name.

    (But even if you could, his syntax was wrong.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder
    Join Date
    Aug 2011
    Posts
    192
    Thanks
    112
    Thanked 0 Times in 0 Posts
    Thank you guys (or gals!).


  •  

    Posting Permissions

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