dszm02606009 2012-02-12 16:53
浏览 51

PHP上传文件到数据库不能使用gif,jpeg和其他文件类型

I have a problem with some PHP/MySQL not working. I can upload files like .docx and .sql perfectly but when i try with .gif, .jpg .zip and .rar, it doesn't work.

Here is the upload form code:

<?php 
if (isset($_GET['uploadError']))
{
    echo '<div id="error">There was an error uploading the file. Please try again</div>';
}
elseif ((!isset($_SESSION['dbusername']))&&(!isset($_SESSION['dbpassword'])))
{
    header('Location: ?page=login&uploadAttempt=true&attemptedSite=upload');
}
else
{
    echo '
    <center>
    <form class="form" enctype="multipart/form-data" action="uploadaction.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="30720" />
    <p class="uploadfile">
        <input name="uploadedfile" type="file" />
    </p>
    <p class="submit">
        <input type="hidden" name="upload" value="start" />
        <input type="submit" value="Upload File" />
    </p>
    </form></center>';
}
?>

Here is the uploadaction.php code:

    <?php
require('lib.php');
localhost_con('filehunt');
session_start();

if(isset($_POST['upload']) && $_FILES['uploadedfile']['size'] > 0)
{
    $fileName = $_FILES['uploadedfile']['name'];
    $tmpName  = $_FILES['uploadedfile']['tmp_name'];
    $fileSize = $_FILES['uploadedfile']['size'];
    $fileType = $_FILES['uploadedfile']['type'];

    $fp      = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);

    if(!get_magic_quotes_gpc())
    {
        $fileName = addslashes($fileName);
    }
    /*$fh = fopen($_FILES['uploadedfile']['tmp_name'], 'r');
    $theData = fread($fh, filesize($_FILES['uploadedfile']['tmp_name']));
    $theData = mysql_real_escape_string($theData);
    fclose($fh); */
    $date = date("y/m/d : H:i:s", time());

    $sql = "INSERT INTO files (rowID, file, mimetype, data, uploaded_by, uploaded_date, size, times_downloaded) 
    VALUES (NULL, '$fileName', '$fileType', '$content', '$user', '$date', $fileSize, 0);";

    if (mysql_query($sql,$con))
    {
        header('Location: index.php?page=search&uploadSucces=true');
    } 
    else echo mysql_error();
}
else header('Location: index.php?page=upload&uploadError=true');

?>

It gets inserted in the database, but the mimetype and data column is empty. Can anyone tell me why this is happening, and how to fix it?

Thank you in advance Adam

  • 写回答

2条回答 默认 最新

  • duanqinqiao4844 2012-02-13 21:52
    关注

    Not quite shure, but this:

    $fileType = $_FILES['uploadedfile']['type'];
    

    must be like this:

    $finfo = new finfo();
    $fileType = $finfo->file($_FILES['uploadedfile']['tmp_name'], FILEINFO_MIME);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据