【问题标题】:PHP - upload.class image and $_FILESPHP - upload.class 图像和 $_FILES
【发布时间】:2010-04-10 11:36:41
【问题描述】:

我正在使用class.upload.php 将图片上传到服务器。这是我的表格:

<form action="<?="http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];?>" method="post" enctype="multipart/form-data">
  <table style="width: 100%; padding-top: 20px;">
    <tr>
        <td>Image file:</td>
        <td><input type="file" name="image_file" /></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td align="right"><input type="submit" name="add_new" value="Add image" /><td></td>
    </tr>
</table>
</form>

在 php 代码中我这样做:

if( array_key_exists('add_new', $_POST) )
{
    echo 'add new is in array';

    echo '<pre>';
    print_r($_POST);
    print_r($_FILES);
    echo '</pre>';

    $handle = new Upload($_FILES['image_file']);
    ...
}

这是 print_r 的输出:

Array
(
    [image_file] => somefile.png
    [add_new] => Add image
)
Array
(
)

如您所见,第二个数组为空 ($_FILES),因此无法上传图片。为什么?


  • 操作系统:Linux
  • PHP 版本:5.2.12
  • GD 版本:2.0.34
  • 支持的图片类型:png jpg gif bmp
  • open_basedir : /home/httpd/vhosts/%site%/httpdocs:/tmp

【问题讨论】:

  • 奇怪。你用的是什么版本的PHP?
  • @Pekka,我刚刚更新了帖子。
  • 文件是否正在上传?你能从大文件的加载时间看出这一点吗?
  • @Pekka,不。页面加载速度非常快。
  • @Ockonal 是否适用于所有浏览器?

标签: php file upload


【解决方案1】:

您是否尝试过使用小于 post_max_size ini 设置的图像?另外,请确保您的 /tmp 有空间并且是可写的。

其他资源:

1) 原因列表很长:http://getluky.net/2004/10/04/apachephp-_files-array-mysteriously-empty/

2) 来自http://php.net/manual/en/features.file-upload.php

As said before if POST size exceeds server limit then $_POST and $_FILES arrays become empty. You can track this using $_SERVER['CONTENT_LENGTH'].
For example:

<?php
$POST_MAX_SIZE = ini_get('post_max_size');
$mul = substr($POST_MAX_SIZE, -1);
$mul = ($mul == 'M' ? 1048576 : ($mul == 'K' ? 1024 : ($mul == 'G' ? 1073741824 : 1)));
if ($_SERVER['CONTENT_LENGTH'] > $mul*(int)$POST_MAX_SIZE && $POST_MAX_SIZE) $error = true;
?>

【讨论】:

  • 我看到了那个列表,检查了所有内容。限制为 15M。我正在上传 120 kb。
  • 还有 /tmp 状态(或保存临时文件的位置)?
猜你喜欢
  • 2011-01-28
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-12
  • 1970-01-01
  • 2016-11-20
  • 2013-08-18
相关资源
最近更新 更多