problem with inserting file name in database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • liberty1
    New Member
    • Jan 2010
    • 3

    problem with inserting file name in database

    Hi everyone. I appreciate your effort at helping newbies like me. I have the following problems and will appreciate urgent help.

    PROBLEM NUMBER 1:
    Using PHP and MySQL, I am able to upload picture successfully unto the server but not so with the file name of the picture even though other parameters in my form got inserted successfuly in the database. I have read several posts on this topic including here on this site but I can't get around the problem yet.

    PROBLEM NUMBER 2:
    I equally need help on limiting file TYPE permitted for upload (gif, jpeg, png ONLY) as well as the file SIZE (not more than 100kb).

    PROBLEM NUMBER 3:
    To force a resize of all pictures uploaded into a smaller thumbnail size of 20kb and stored this in a seperate folder on the server with a seperate filename on the database.

    Below is my current PHP code;

    Code:
        <?php 
        if (!isset($_POST['upload'])) 
        { 
        include ("submit_interview_test.php"); //shows form if it's not been posted 
        }
    
        else 
        { 
        if($_FILES['pic']['tmp_name'] == "none") 
        { 
        echo "<b>File not successfully uploaded. Maybe check the filesize limit.</b>"; 
        include ("submit_interview_test.php"); 
        exit(); 
        }
    
        if(!ereg("image",$_FILES['pic']['type'])) 
        { 
        echo "<b>File is not an image - try another file.</b>"; 
        include ("submit_interview_test.php"); 
        exit(); 
        }
    
        else 
        { 
        $uploadDir = 'intervimages/'; 
        $destination = $uploadDir.basename($_FILES['pic']['name']); 
        $temp_file = $_FILES['pic']['tmp_name'];
        include ("processinterview.php");
        if ( move_uploaded_file ($temp_file,$destination) ) 
        { echo '<p>Your file has been successfully uploaded!</p>'; 
        } 
    
        else 
        { echo "Problem with picture upload!"; 
        } 
        } // end of else
    
        } // end of else 
        ?>
    THIS IS my processintervie w.php code;

    Code:
        <?php
        include_once 'includes/db.php';
        $sql="INSERT INTO interview (interviewer, media_house, category, interview_title, title_rider, personality, interview_body, source, published, temp_file, interview_date, location, interview_intro, date) VALUES ('$_POST[interviewer]','$_POST[media_house]','$_POST[category]','$_POST  [interview_title]','$_POST[title_rider]','$_POST[personality]','$_POST [interview_body]','$_POST[source]','$_POST[published]','$_FILES[temp_file]','$_POST[interview_date]','$_POST[location]','$_POST[interview_intro]',now())";
    
        if (!mysql_query($sql))
        {
        die('Error: ' . mysql_error());
        }
        echo "1 interview record has been successfully added to the database.";
        ?>
    AND, this is my form (submit_intervi ew_test.php);

    Code:
        <form enctype="multipart/form-data" action="processinterview3.php" method="post">
        <table bgcolor="#ececec" border="0" cellspacing="5">
        <tbody>
        <tr><td>Interview conducted by (Interviewer's Name):</td><td><input size="30"  name="interviewer" type="text" /></td></tr>
        <tr><td>Media house (e.g., Punch):</td><td><input size="30" name="media_house" type="text" /></td></tr>
        <tr><td>Location (e.g., Ibadan or USA):</td><td><input type="text" size="30" name ="location" /></td></tr>
        <tr><td>Interview Category (e.g., Local):</td><td><input type="text" size="30" name   ="category" /></td></tr>
        <tr><td>Interview Personality:</td><td><input type="text"  size="30" name ="personality" /></td></tr>
        <tr><td>Interview Title:</td><td><input type="text"  size="130" name ="interview_title" /></td></tr>
        <tr><td>Title Rider:</td><td><input type="text"  size="130" name ="title_rider" /></td></tr>
        <tr><td>Source (e.g., http://...):</td><td><input size="130" name="source" type="text" /></td></tr>
        <tr>  <td valign="top">Interview Introduction:</td>
        <td><textarea name="interview_intro" rows="3" cols="100"></textarea></td></tr>
        <tr><td>Date of Interview (e.g., 26/09/2009):</td><td><input size="30" name="interview_date" type="text" /></td></tr>
        <tr>  <td valign="top">Interview Main Content:</td>
        <td><textarea name="interview_body" rows="12" cols="100"></textarea></td></tr>
        <tr><td>Add Photo (Jpeg or gif not more than 80KB):</td><td><input type="file" name="pic"/></td></tr>
        <tr><td valign="top">Publish rightaway?</td>
        <td><input type="checkbox" name="published" value="1"> Yes (Leave this if this   Interview is not to be published yet)<br>
        <tr><td>&nbsp;</td><td><input value="Submit Interview" type="submit" name="upload"/><font face="arial" size="1">&nbsp;&nbsp;Please check for any error before you submit</font></td></tr>
        </tbody></table>
    Thanks for the trouble pls.
    Last edited by Dormilich; Jan 13 '10, 06:31 AM. Reason: Please use [code] tags when posting code
  • dgreenhouse
    Recognized Expert Contributor
    • May 2008
    • 250

    #2
    1- $_FILES['pic']['name'] - for the original filename
    2- preg_match('/image\/(jpeg|gif|png)/i',$_FILES['pic']['type']) - for the type check
    3- $_FILES['pic']['size'] - for the file size
    4- I'd use $_FILES['pic']['error'] versus $_FILES['pic']['tmp_name'] to check for no file uploaded
    5- For the auto-thumbnail generation, use either the gd image library which is usually enabled on most modern hosting accounts. If not, you'll need to set it up.
    You can always adjust the size on display, but the defeats that benefit of having an actual thumbnail.

    Comment

    • liberty1
      New Member
      • Jan 2010
      • 3

      #3
      Originally posted by dgreenhouse
      1- $_FILES['pic']['name'] - for the original filename
      2- preg_match('/image\/(jpeg|gif|png)/i',$_FILES['pic']['type']) - for the type check
      3- $_FILES['pic']['size'] - for the file size
      4- I'd use $_FILES['pic']['error'] versus $_FILES['pic']['tmp_name'] to check for no file uploaded
      5- For the auto-thumbnail generation, use either the gd image library which is usually enabled on most modern hosting accounts. If not, you'll need to set it up.
      You can always adjust the size on display, but the defeats that benefit of having an actual thumbnail.
      Thanks dgreenhouse. I am actually a novice. I know there is $_FILES['pic']['name'] in the code so am I to change it again? Why is the code as it is not inserting name into the database? It is uploading the pictures but it is not inserting the names. I changed $_POST or $_FILES[temp_file] to just $temp_file and got some charaters inserted but there are not proper names of the files. I changed tmp_name to simply 'name' and got the names inserted but then the picture then refused to load.

      Comment

      • liberty1
        New Member
        • Jan 2010
        • 3

        #4
        UPDATE: Once again thank you. I finally changed tmp_name in $_FILES['pic']['tmp_name'] to just 'name' thus becoming $_FILES['pic']['name'] in my main code and changed $temp_file to $_FILES['pic']['tmp_name'] in the move to server part of the code. And that did it. The $_FILES[temp_file] in the process for submit into database (i.e., processintervie w.php) part was also changed to just $temp_file. So now I am able to upload picture and insert the name in the database. Thank you for your help. I will work on the remainder parts as you suggested.

        Comment

        • dgreenhouse
          Recognized Expert Contributor
          • May 2008
          • 250

          #5
          Glad you're on the right track...

          Comment

          Working...