By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
463,045 Members | 494 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 463,045 IT Pros & Developers. It's quick & easy.

problem with inserting file name in database

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

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.

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).

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;

Expand|Select|Wrap|Line Numbers
  1.     <?php 
  2.     if (!isset($_POST['upload'])) 
  3.     { 
  4.     include ("submit_interview_test.php"); //shows form if it's not been posted 
  5.     }
  7.     else 
  8.     { 
  9.     if($_FILES['pic']['tmp_name'] == "none") 
  10.     { 
  11.     echo "<b>File not successfully uploaded. Maybe check the filesize limit.</b>"; 
  12.     include ("submit_interview_test.php"); 
  13.     exit(); 
  14.     }
  16.     if(!ereg("image",$_FILES['pic']['type'])) 
  17.     { 
  18.     echo "<b>File is not an image - try another file.</b>"; 
  19.     include ("submit_interview_test.php"); 
  20.     exit(); 
  21.     }
  23.     else 
  24.     { 
  25.     $uploadDir = 'intervimages/'; 
  26.     $destination = $uploadDir.basename($_FILES['pic']['name']); 
  27.     $temp_file = $_FILES['pic']['tmp_name'];
  28.     include ("processinterview.php");
  29.     if ( move_uploaded_file ($temp_file,$destination) ) 
  30.     { echo '<p>Your file has been successfully uploaded!</p>'; 
  31.     } 
  33.     else 
  34.     { echo "Problem with picture upload!"; 
  35.     } 
  36.     } // end of else
  38.     } // end of else 
  39.     ?> 
THIS IS my processinterview.php code;

Expand|Select|Wrap|Line Numbers
  1.     <?php
  2.     include_once 'includes/db.php';
  3.     $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())";
  5.     if (!mysql_query($sql))
  6.     {
  7.     die('Error: ' . mysql_error());
  8.     }
  9.     echo "1 interview record has been successfully added to the database.";
  10.     ?>
AND, this is my form (submit_interview_test.php);

Expand|Select|Wrap|Line Numbers
  1.     <form enctype="multipart/form-data" action="processinterview3.php" method="post">
  2.     <table bgcolor="#ececec" border="0" cellspacing="5">
  3.     <tbody>
  4.     <tr><td>Interview conducted by (Interviewer's Name):</td><td><input size="30"  name="interviewer" type="text" /></td></tr>
  5.     <tr><td>Media house (e.g., Punch):</td><td><input size="30" name="media_house" type="text" /></td></tr>
  6.     <tr><td>Location (e.g., Ibadan or USA):</td><td><input type="text" size="30" name ="location" /></td></tr>
  7.     <tr><td>Interview Category (e.g., Local):</td><td><input type="text" size="30" name   ="category" /></td></tr>
  8.     <tr><td>Interview Personality:</td><td><input type="text"  size="30" name ="personality" /></td></tr>
  9.     <tr><td>Interview Title:</td><td><input type="text"  size="130" name ="interview_title" /></td></tr>
  10.     <tr><td>Title Rider:</td><td><input type="text"  size="130" name ="title_rider" /></td></tr>
  11.     <tr><td>Source (e.g., http://...):</td><td><input size="130" name="source" type="text" /></td></tr>
  12.     <tr>  <td valign="top">Interview Introduction:</td>
  13.     <td><textarea name="interview_intro" rows="3" cols="100"></textarea></td></tr>
  14.     <tr><td>Date of Interview (e.g., 26/09/2009):</td><td><input size="30" name="interview_date" type="text" /></td></tr>
  15.     <tr>  <td valign="top">Interview Main Content:</td>
  16.     <td><textarea name="interview_body" rows="12" cols="100"></textarea></td></tr>
  17.     <tr><td>Add Photo (Jpeg or gif not more than 80KB):</td><td><input type="file" name="pic"/></td></tr>
  18.     <tr><td valign="top">Publish rightaway?</td>
  19.     <td><input type="checkbox" name="published" value="1"> Yes (Leave this if this   Interview is not to be published yet)<br>
  20.     <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>
  21.     </tbody></table>
Thanks for the trouble pls.
Jan 13 '10 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 250
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.
Jan 13 '10 #2

P: 3
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.
Jan 13 '10 #3

P: 3
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., processinterview.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.
Jan 13 '10 #4

Expert 100+
P: 250
Glad you're on the right track...
Jan 13 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.