php move_uploaded_file强制给出新名称

时间:2012-09-21 05:19:31

标签: php

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

$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)
  }
}

2 个答案:

答案 0 :(得分: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"];
  }

答案 1 :(得分:0)

var_dump()中,$_FILES["file"]["name"]长度为23,但应为9.因此请尝试trim此字符串。而不是

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

尝试写

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