-1

I just implemented an image upload to MySQL via PHP. But i want to upload text, pdf, doc, zip file to MySQL. This is my code. How to change it ?

// 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>';
}

Database 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`)

)
3
  • 1
    If you precede any line of code with four spaces, it will indent (see help on 'Markdown' syntax). A carriage return between every line makes for less readability too - would you edit? :) Commented May 9, 2012 at 16:40
  • Protip: When pasting code, highlight it and select {} from the toolbar (or press Ctrl+k). Commented May 9, 2012 at 16:47
  • Sorry about that. because i am a new user. haha Commented May 9, 2012 at 17:31

1 Answer 1

2

Try this:

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

You can look at the different MIME-types here: http://en.wikipedia.org/wiki/Internet_media_type

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks your answer. But the msword and zip can't upload to MySQL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.