0

我正在尝试使用 php 上传文件,代码片段

$upload_path = "/var/upload/" . $_FILES['mfile']['name'];
if(move_uploaded_file($_FILES['mfile']['tmp_name'], $upload_path))
    echo "file uploaded";
else
    echo "upload failed";

注意:/var/upload 目录存在且拥有 777 权限。

代码打印“上传失败”

当我检查 /var/upload 目录时,有文件 f_505bf77bd8a0f_mypdf.pdf。什么地方出了错?

添加日志(根据loler的要求):

[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice:  Undefined variable: showdebug in /Volumes/data/htdocs/ebeu/upload.php on line 558, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice:  Undefined variable: showdebug in /Volumes/data/htdocs/ebeu/upload.php on line 559, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice:  Undefined index: accessories in /var/www/my/web/folder/myupload.php on line 21, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice:  Undefined index: pmaterial in /var/www/my/web/folder/myupload.php on line 27, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice:  Undefined index: occupation_bwc in /var/www/my/web/folder/myupload.php on line 33, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice:  Undefined index: occupation_bwc in /var/www/my/web/folder/myupload.php on line 75, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice:  Undefined index: occupation_spec in /var/www/my/web/folder/myupload.php on line 75, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice:  Undefined index: babycarrier in /var/www/my/web/folder/myupload.php on line 78, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice:  Undefined variable: accessories_all in /var/www/my/web/folder/myupload.php on line 79, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice:  Undefined variable: pmaterials_all in /var/www/my/web/folder/myupload.php on line 80, referer: http://localhost/upload/test/upload.php

添加了 var_dump($_FILES)

array(1) {
  ["mfile"]=>
  array(5) {
    ["name"]=>
    string(23) "mypdf.pdf"
    ["type"]=>
    string(15) "application/pdf"
    ["tmp_name"]=>
    string(26) "/var/tmp/phpUl6k50"
    ["error"]=>
    int(0)
    ["size"]=>
    int(478704)
  }
}
4

2 回答 2

1

试试这个代码来上传你的文件..

if (file_exists("/var/upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "/var/upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "/var/upload/" . $_FILES["file"]["name"];
  }
于 2012-09-21T05:40:16.027 回答
0

在你var_dump()那里的$_FILES["file"]["name"]长度是 23,但它应该是 9。所以试试trim这个字符串。代替

$upload_path = "/var/upload/" . $_FILES['mfile']['name'];

试着写

$upload_path = "/var/upload/" . trim($_FILES['mfile']['name']);
于 2012-09-21T06:10:17.013 回答