【问题标题】:PHP - Fileuploads, ['tmp_name'] doesn't exists physicallyPHP - 文件上传,['tmp_name'] 物理上不存在
【发布时间】:2013-09-26 21:08:38
【问题描述】:

这是一些非常基本的东西。 由于未知原因,我无法通过标准 HTML+PHP 上传表单上传文件。

文件在本地/tmp下不存在

php.ini

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir = /tmp/

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 30M

; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20

; open_basedir, if set, limits all file operations to the defined directory
; and below.  This directive makes most sense if used in a per-directory
; or per-virtualhost web server configuration file. This directive is
; *NOT* affected by whether Safe Mode is turned On or Off.
; http://php.net/open-basedir
open_basedir = /srv/http/:/home/:/tmp/:/usr/share/pear/:/usr/share/webapps/

; Maximum size of POST data that PHP will accept.
; Its value may be 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
; http://php.net/post-max-size
post_max_size = 8M

上传代码(与html表单位于同一文件,index.php)

print '<pre>';
print var_dump($_FILES);
print var_dump(is_uploaded_file($_FILES["file"]["tmp_name"]));
print is_writable('/tmp');
print '</pre>';

html

<form method="post" action="./" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="submit" value="Submit">
</form>

我的输出是:

array(1) {
  ["file"]=>
  array(5) {
    ["name"]=>
    string(11) "test.jpg"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(14) "/tmp/php66JkW8"
    ["error"]=>
    int(0)
    ["size"]=>
    int(75766)
  }
}
bool(true)
1

文件的权限和信息:

[torxed@archie http]$ ls -l / | grep tmp
drwxrwxrwt   9 root root   220 Sep 26 13:34 tmp

[torxed@archie http]$ ls -lh /home/torxed/ | grep test
-rw-r--r--  1 torxed users  74K Sep 26 13:08 test.jpg

【问题讨论】:

  • 什么是$files..???
  • 缺少变量名的_和区分大小写? $files !== $_FILES
  • 愚蠢的我,解决了调试输出问题。现在我对整个事情的原始问题是永远不会创建文件(意思是,PHP 或其他任何东西)无法获取文件。
  • 您的代码中缺少某些内容。行动是./ 而不是upload.php。也许您只是将文件从/tmp/ 移动到/home/torxed/
  • 使用move_uploaded_file 时,即使对于大文件也很快,因为它只是更改文件的地址而不是其内容。该文件没有被复制,它被移动了。如果你不使用上传的临时文件,它就消失了,这就是临时的!

标签: php html linux file-upload


【解决方案1】:

自己解决了。 尽管问题很愚蠢,但答案更愚蠢。

[torxed@archie http]$ ls -lh /srv/http | grep tmp
drwxr-xr-x 2 torxed root 4.0K Sep 26 14:10 tmp

我认为该文件在 /tmp 下的存在时间足以让我目睹它被创建,这首先是愚蠢的。

但尝试使用 move_uploaded_file 时,在尝试将其移动到 ./ 时返回了 false 原因是文件夹权限错误。

move_uploaded_file($src, '/srv/http' + $src); 只需将其移动到另一个临时位置即可(在修复 /srv/http/tmp 的写入权限后)

【讨论】:

  • 我正要告诉你,在创建文件时你将无法看到它。有时,如果没有适当的解释,我不理解投票者或他们这样做的理由。
【解决方案2】:

您是否将 post_max_size 值设置为更高的值?

【讨论】:

  • 我的错,默认值(我会更新我的php.ini,是 8M)
  • 我真的不知道,我投票赞成指出我错过了我的 php.ini 剪切中的一个重要变量。即使这并没有解决问题,这也是我的一个很好的发现。
【解决方案3】:
var_dump(is_uploaded_file($_FILES["file"]["tmp_name"]));

【讨论】:

  • 这只能解决我的调试输出显示错误信息。问题仍然存在,我无法访问 /tmp 下的本地临时文件,因为它从来没有被创建过。
猜你喜欢
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
  • 2015-04-25
  • 2017-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多