0

I am trying to upload pdf or word files with PHP to mysql database. my code works with images that i specified but it doesn't work with pdf and word files.

here is my code with table of form and php code:

<?php
include("./upload.php");

$dosyayolu="resimler/";
$logos=yukleyici("logo",2048*1024,$dosyayolu);
$links=yukleyici("link",20000000000000,$dosyayolu);

if($logos!="1"){
if($_POST['baslik']!=""){
if(mysql_query("INSERT INTO gfsidocuments (baslik,logo,detay,link)              
VALUES ('".$_POST['baslik']."','".$logos."','".$_POST['detay']."','".$links."')"));
header( 'Location: ./gfsidocedit.php');
}
}
}
?>


<table border="0" width="100%" height="40" cellspacing="0" cellpadding="0">
<tr>
<td width="100" bgcolor="#E1E1E1"><font face="Tahoma" size="2">&nbsp;Document</font></td>
<td width="238" bgcolor="#E1E1E1"><input type="file" name="link" id="link" size="20"></td>
<td width="211" bgcolor="#E1E1E1"></td>
<td bgcolor="#E1E1E1"></td>
</tr>
</table>

Here is the upload.php :

<?php
function yukleyici($filefield,$limit,$folder){
if ( ($_FILES[$filefield]["type"] == "image/gif")
|| ($_FILES[$filefield]["type"] == "image/jpeg")
|| ($_FILES[$filefield]["type"] == "image/png")
|| ($_FILES[$filefield]["type"] == "application/pdf")
|| ($_FILES[$filefield]["type"] == "application/msword")
)
{
if(($_FILES[$filefield]["size"] < $limit)){
$filename=getUniqueName() . $_FILES[$filefield]["name"];
if(move_uploaded_file($_FILES[$filefield]["tmp_name"],
realpath("../") . "/" . $folder . $filename)){
return $folder . $filename;
}else{
return "1";
}
}else{
return "1";
}
}else{
return "1";
}
}
function getUniqueName(){
return substr(md5(uniqid(rand(), true)),1,10);
}
?>

As i said when i upload image it works, but when i upload pdf or word document it returns 1 to database... İ assume that problem on determining size but i couldn't figure it out how to fix.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
kazata
  • 71
  • 2
  • 2
  • 10
  • Maybe the mimetype isn't correct, so check that. – putvande Dec 31 '13 at 16:23
  • Can you do at least *some* debugging on this? In every case you just `return "1"` which doesn't actually tell you anything useful. When you step through the code, where and how does it fail? – David Dec 31 '13 at 16:28

4 Answers4

0

You're receiving a '1' in the database because you're not sending any data. You need to surround your form's HTML with a form tag, like so:

<form action="upload.php" method="post" enctype="multipart/form-data">
<table border="0" width="100%" height="40" cellspacing="0" cellpadding="0">
<tr>
<td width="100" bgcolor="#E1E1E1"><font face="Tahoma" size="2">&nbsp;Document</font></td>
<td width="238" bgcolor="#E1E1E1"><input type="file" name="link" id="link" size="20"></td>
<td width="211" bgcolor="#E1E1E1"></td>
<td bgcolor="#E1E1E1"></td>
</tr>
</table>
</form>
0

first check your data type in mysql..

Also,you can look this:

I can't upload pdf to my mysql database

maybe can help you..

Are you try to store documents outside of database? It is safe method,and no headache when database is big :)

Community
  • 1
  • 1
Pavlen
  • 129
  • 1
  • 12
0

Have you tried to echo the $_FILES[$filefield]["type"] to verify it is returning the MIME type "application/pdf"? One issue I've seen before is sometimes it returns "application/acrobat". If you have the correct MIME type as verified by the echo statement, you can echo out your SQL statement to see what data is being passed. That will help you narrow your search.

Since it is possible for multiple MIMEs to be used, I'd change the following code:

($_FILES[$filefield]["type"] == "application/pdf")

To:

(in_array($_FILES[$filefield]["type"],$mime_types))

with $mime types being defined as:

$mime_types = array('application/pdf','application/acrobat');

That way you can avoid the issue of only one MIME type being reported for an application. Here is a link to the list of MIME types.

You can also print a list of your form variables to see what data is being passed by the form by using:

echo '<pre>'; print_r($_REQUEST);echo '</pre>';

The tag is to make it a little easier to read.

W.A. Murray
  • 13
  • 1
  • 5
0

I'm not sure but I think the problem comes from these lines:

$logos=yukleyici("logo",2048*1024,$dosyayolu); //that means pixel

if($logos!="1"){ ...
halfer
  • 19,824
  • 17
  • 99
  • 186
hous
  • 174
  • 4
  • 11
  • Thank you very much for clue, i changed 'if($logos!="1") to if($logos!="1" && $links!="1") and now it works :) – kazata Dec 31 '13 at 17:48