dsyo9700 2014-05-18 14:56
浏览 62

php上传上传文件时出错

I am unable to upload any file to my website.

Is my code wrong? In addition I wanted to know the php code for setting the maximum upload size for the files I wanted to upload to a Server. Also how I am supposed to change my file upload maximum size in my XAMPP, I am using version 1.7.3.

This is my code:

<?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>
";
    }
}
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条回答 默认 最新

  • doubi6303 2014-05-18 15:10
    关注

    Does it fall on your if statement if(isset($_FILES['file']))?

    if not better check your html tag and check if there is an enctype="multipart/form-data" in it.

    Your final code would look like this

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

    About changing the size of file upload: Just open your php.ini located in /xampp/php/php.ini. Find and change the following:

    upload_max_filesize
    
    post_max_size
    

    Just type 50M.

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆