如何通过PHP文件上传到MySQL的文本,pdf,doc,zip文件

时间:2012-05-09 16:38:31

标签: php mysql

我刚刚通过PHP实现了上传到MySQL的图像。但我想将文本,pdf,doc,zip文件上传到MySQL。这是我的代码。如何改变?

// uploadAction.php

mysql_connect("localhost", "root", "1989") 

or die(mysql_error());

mysql_select_db("uploadfile") or die(mysql_error());


$sql_id = "SELECT (MAX(id)+1) as \"result\" FROM save";

$rs_id = mysql_query($sql_id);

$row_id = mysql_fetch_assoc($rs_id);

$id = $row_id['result'];

if($id == null)

$id = 1;    

$newName = '';  

$file = $_FILES['photo']['name'];

if ((($_FILES["photo"]["type"] == "image/gif")||

($_FILES["photo"]["type"] == "image/jpeg")||

($_FILES["photo"]["type"] == "image/pjpeg"))

&& ($_FILES["photo"]["size"] < 200000))
{

if ($_FILES["photo"]["error"] > 0)

    echo "Return Code: " . $_FILES["photo"]["error"] . "<br />";

  else{  

      $newName = "$id.".basename($_FILES["photo"]["type"]);

      move_uploaded_file($_FILES["photo"]["tmp_name"], "images/" . $newName);

      }

} 

else

  {

  echo "Invalid file";

  }

$queryUser = "INSERT INTO save VALUES(NULL, \"$newName\")";

$insert = mysql_query($queryUser);

if(!$insert){

    echo mysql_error().'Upload Fail';

  }else {

    echo "<script>alert('Upload Sccessful');";

    echo 'window.location="index.php";</script>';
}

数据库SQL

CREATE TABLE `save` (

  `id` int(255) NOT NULL auto_increment,

  `photo` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,

  PRIMARY KEY  (`id`)

)

1 个答案:

答案 0 :(得分:1)

试试这个:

if ($_FILES["photo"]["type"] == "image/gif" ||
$_FILES["photo"]["type"] == "image/jpeg" ||   
$_FILES["photo"]["type"] == "image/pjpeg" || 
$_FILES["photo"]["type"] == "application/zip" || 
$_FILES["photo"]["type"] == "application/pdf" || 
$_FILES["photo"]["type"] == "application/msword" || 
$_FILES["photo"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")

您可以在此处查看不同的MIME类型:http://en.wikipedia.org/wiki/Internet_media_type