php上传上传文件时出错

时间:2014-05-18 14:56:51

标签: php html file-upload

我无法将任何文件上传到我的网站。

我的代码错了吗?另外我想知道用于设置我想要上传到服务器的文件的最大上传大小的php代码。另外我应该如何在我的XAMPP中更改文件上传的最大大小,我使用的是1.7.3版本。

这是我的代码:

<?php 
//Load the settings
require_once("Setting.php");
require_once("db.php");
$message = "";
//Has the user uploaded something?
if(isset($_FILES['file']))
{
    $_FILES['file']['tmp_name'];
    $target_path = Setting::$uploadFolder;  
    $target_path = $target_path . time() . '_' . basename( $_FILES['file']['name']); 
    echo $target_path;

        //Try to move the uploaded file into the designated folder
        if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) 
        {
            $message = "The file ".  basename( $_FILES['file']['name']). " has been uploaded";

            $query ="insert into upload (path) values ('$target_path')";

        $dbresult = mysql_query($query,$dblink);

        } else{
            $message = "There was an error uploading the file, please try again!";
        }
    }

    //Clear the array
    unset($_FILES['file']);

if(strlen($message) > 0)
{
    $message = '<p class="error">' . $message . '</p>';
}

/** LIST UPLOADED FILES **/
$uploaded_files = "";

//Open directory for reading
$dh = opendir(Setting::$uploadFolder);

//LOOP through the files
while (($file = readdir($dh)) !== false) 
{
    if($file != '.' && $file != '..')
    {
        $filename = Setting::$uploadFolder . $file;
        $parts = explode("_", $file);
        $size = formatBytes(filesize($filename));
        $added = date("m/d/Y", $parts[0]);
        $origName = $parts[1];
        $filetype = getFileType(substr($file, strlen($file) - 3));
        $uploaded_files .= "<li class=\"$filetype\"><a href=\"$filename\">$origName</a> $size - $added</li>\n";
    }
}
closedir($dh);

if(strlen($uploaded_files) == 0)
{
    $uploaded_files = "<li><em>No files found</em></li>";
}

function getFileType($extension)
{
    $images = array('JPG', 'GIF', 'PNG', 'BMP');
    $docs   = array('TXT', 'RTF', 'DOC');
    $apps   = array('ZIP', 'RAR', 'EXE');

    if(in_array($extension, $images)) return "Images";
    if(in_array($extension, $docs)) return "Documents";
    if(in_array($extension, $apps)) return "Applications";
    return "";
}

function formatBytes($bytes, $precision = 2) { 
    $units = array('B', 'KB', 'MB', 'GB', 'TB'); 

    $bytes = max($bytes, 0); 
    $pow = floor(($bytes ? log($bytes) : 1024) / log(1024)); 
    $pow = min($pow, count($units) - 1); `enter code here`

    $bytes /= pow(1024, $pow); 

    return round($bytes, $precision) . ' ' . $units[$pow]; 
} 
?>

2 个答案:

答案 0 :(得分:0)

它是否属于你的if语句if(isset($_FILES['file']))

如果不能更好地检查你的html标签并检查其中是否有enctype="multipart/form-data"

您的最终代码将如下所示

<form method="POST" action="submit.php" enctype="multipart/form-data">
<!-- Your HTML form -->
</form>

关于更改文件上传的大小: 只需打开php.ini中的/xampp/php/php.ini即可。 查找并更改以下内容:

upload_max_filesize

post_max_size

输入50M。

答案 1 :(得分:0)

要首先上传任何文件,请确保将PHP配置为允许文件上载。 检查文件

  

/opt/lampp/etc/php.ini

搜索file_uploads指令,并将其设置为On,

  

file_uploads = On

对于上传文件的最大允许大小,您可以再次更新文件php.ini指令,例如

  

upload_max_filesize = 40M

     

post_max_size = 40M

post_max_size&gt; = upload_max_filesize