将图片上传到服务器成功但是将文件名插入数据库是空白的

时间:2010-01-13 00:57:35

标签: php mysql filenames photo

我有以下问题。

问题编号1: 使用PHP和MySQL,我能够将图片成功上传到服务器,但不能使用图片的文件名上传,即使我的表单中的其他参数已成功插入数据库中。我已经阅读了几个关于这个主题的帖子,包括这个网站,但我还是无法解决这个问题。

问题编号2: 我同样需要帮助限制允许上传的文件TYPE(仅限gif,jpeg,png)以及文件SIZE(不超过100kb)。

问题编号3: 强制调整上传到较小缩略图大小20kb的所有图片的大小,并将其存储在服务器上的单独文件夹中,并在数据库中使用单独的文件名。

以下是我当前的PHP代码;

<?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 
?> 

这是我的processinterview.php代码;

<?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.";
?>

这是我的表格(submit_interview_test.php);

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

1 个答案:

答案 0 :(得分:0)

1)您的查询正在插入$ _FILES [temp_name]作为您应该使用$ temp_name时存储的值。

2)使用以下mime类型数组进行检查(取自Symfony框架的上传图像验证码):

<?php
  $valid_mimes = array(
    'image/jpeg',
    'image/pjpeg',
    'image/png',
    'image/x-png',
    'image/gif',
  );

  if(!in_array($_FILES['pic']['type'], $valid_mimes)) {
    // invalid mime
  }

您的文件大小可以使用$ _FILES ['pic'] ['size']值进行验证。

3)有许多库可用于生成图像和缩略图,您的具体选择取决于您在服务器上安装的支持图形库(GD或ImageMagick)。以下任何结果都适用于您:http://www.google.com/search?q=php+image+thumbnail