【问题标题】:move_uploaded_file() not workingmove_uploaded_file() 不工作
【发布时间】:2014-10-09 17:59:36
【问题描述】:

我有一个非常简单的文件上传表单和 php 脚本,但它似乎不起作用。

HTML:

<form enctype="multipart/form-data" method="post" action="upload_file.php">
    Send this file: <input name="userfile" type="file" /><br />
    <input type="submit" value="Send File" />
</form>

PHP:

<?php
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], "./upload")) {
        print "Received {$_FILES['userfile']['name']} - its size is {$_FILES['userfile']['size']}";
    } else {
        print "Upload failed!";
    }
?>

当我上传文件并访问“upload_file.php”页面时,我收到此错误:

警告:move_uploaded_file():copy() 函数的第二个参数 不能是 blahhhh/blahhhh/public_html/upload_file.php 中的目录 在第 2 行警告:move_uploaded_file():无法移动 '/tmp/phprsYav7' 到 './upload' 中 blahhhh/blahhhh//public_html/upload_file.php 在第 2 行上传失败!

关于如何解决这个问题的任何想法?谢谢!

【问题讨论】:

  • 阅读错误信息了吗?您需要提供一个文件路径,而不仅仅是一个目录。类似于upload/foobar.jpg,而不仅仅是upload
  • 在这里,阅读F 手册php.net/manual/en/function.move-uploaded-file.php - 你错过了一个论点。请参见示例 #1。
  • @deceze 哦,我明白了,当文件上传到 ./upload 目录时,我该如何获取它以便文件名是“tmp 名称”? - 如果可能的话>

标签: php html file move


【解决方案1】:

您需要指定文件名而不仅仅是路径。像这样的:

<?php
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], "./upload/blah.txt")) {
        print "Received {$_FILES['userfile']['name']} - its size is {$_FILES['userfile']['size']}";
    } else {
        print "Upload failed!";
    }
?>

【讨论】:

    猜你喜欢
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 2011-05-13
    相关资源
    最近更新 更多