在 OpenShift 上使用 PHP 上传文件

Upload file using PHP on OpenShift

我正在尝试使用以下代码上传图片:

index.php:

<form action="upload_photo.php" method="post" enctype="multipart/form-data">
    <p>
        Upload a new photo to the server:<br/><br/><br/>
        <input type="file" name="myphoto"/><br/><br/>
        <input type="submit" value="Upload photo"/>
    </p>
</form>

upload_photo.php:

// This function is included from another .php file
function checkUploadedPhoto() {
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["myphoto"]["name"]);

    if(isset($_FILES['myphoto']) AND $_FILES['myphoto']['error'] == 0) {
        // Check size
        if($_FILES['myphoto']['size'] <= 1000000) {
            // Get extension name
            $fileInfo = pathinfo($_FILES['myphoto']['name']);
            $upload_extension = $fileInfo['extension'];
            $allowed_extensions = array('jpg', 'jpeg', 'gif', 'png');

            // Check if the file already exists
            if (file_exists($target_file)) {
                echo "Sorry, file already exists.";
            }

            // Check if the file has a correct, expected extension
            if(in_array($upload_extension, $allowed_extensions)) {
                if(move_uploaded_file($_FILES['myphoto']['tmp_name'], $target_file)) {
                    return true;
                }
            }
            else
                echo "error3";
        }
        else
            echo "error2";
    }
    else
        echo "error1";

    echo "<pre>". print_r($_FILES) ."</pre>";
    echo "Error code: " .$_FILES['myphoto']['error'] ."<br/>";
    return false;
}

if(checkUploadedPhoto()) {
    header("Location: index.php");
}
else {
    echo "upload error";
}

浏览器结果:

即使错误代码是 0上传失败。是权限问题吗?我似乎无法判断问题出在哪里。

我检查过的其他参考文献没有帮助:

link1/ link2/ link3/ W3Schools PHP upload

编辑:

这是我的应用程序的结构:

更新:

我添加了这个测试来检查uploads/目录是否可写,结果是无法访问:

// This condition is true
if (!is_writeable('uploads/' . $_FILES['myphoto']['name'])) {
    die("Cannot write to destination file");
}

更新 2:

我将 目标目录 "uploads/" 更改为 "/uploads/" & 它在 localhost 上工作,但没有在托管服务器上。

我找到了解决方案。我不得不改变:

$target_dir = "uploads/";

$target_dir = dirname(__FILE__) ."/uploads/";

dirname(__FILE__)指的是文件的完整路径文件名