且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP-无法打开流:在该目录中没有此类文件或目录

更新时间:2023-02-22 17:51:18

正如Peter所说,我怀疑问题是无法引用临时文件的完整路径,即 $ _ FILES ["userfile"]["tmp_name"] .这是我过去使用过的内容的再现,使用 move_uploaded_file 而不是ftp,但您可能会明白:

As Peter says, I suspect the problem is the failure to reference the full path of the temp file, namely, $_FILES["userfile"]["tmp_name"]. Here a rendition of what I've used in the past, using move_uploaded_file rather than ftp, but you probably get the idea:

<?php

header('Content-Type: application/json');

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["userfile"]["name"]));
if ((($_FILES["userfile"]["type"] == "image/gif")
     || ($_FILES["userfile"]["type"] == "image/jpeg")
     || ($_FILES["userfile"]["type"] == "image/png")
     || ($_FILES["userfile"]["type"] == "image/pjpeg"))
    && ($_FILES["userfile"]["size"] < 200000)
    && in_array($extension, $allowedExts))
{
    if ($_FILES["userfile"]["error"] > 0)
    {
        echo json_encode(array("error" => $_FILES["userfile"]["error"]));
    }
    else
    {
        if (file_exists("upload/" . $_FILES["userfile"]["name"]))
        {
            echo json_encode(array( "error" => $_FILES["userfile"]["name"] . " already exists"));
        }
        else
        {
            move_uploaded_file($_FILES["userfile"]["tmp_name"], "upload/" . $_FILES["userfile"]["name"]);
            echo json_encode(array("success"   => true,
                                   "upload"    => $_FILES["userfile"]["name"],
                                   "type"      => $_FILES["userfile"]["type"],
                                   "size"      => ($_FILES["userfile"]["size"] / 1024) . " kB",
                                   "stored in" => "upload/" . $_FILES["userfile"]["name"]));
        }
    }
}
else
{
    echo json_encode(array( "error" => "Invalid file"));
}
?>

也许您不需要文件大小检查(或者200k可能不是正确的阈值),所以这取决于您.但是请注意,对于编程接口,我将响应格式设置为JSON,因此我的应用可以轻松解析响应.

Perhaps you don't need the file size check (or 200k might not be the right threshold), so that's up to you. But note that for programmatic interfaces, I format the response in JSON, so my app can easily parse the response.