Php yii2上传目录不';不存在

Php yii2上传目录不';不存在,php,post,directory,yii2,Php,Post,Directory,Yii2,我试图上传文件,我不断得到“未能打开流:没有这样的文件或目录”。我已经把它缩小到了我的输出目录,但不确定为什么它说它不存在。我不确定yii2是否试图改变我的路线,或者发生了什么。任何信息都会有帮助,谢谢 文件夹结构: www\images\uploads\ protected\controlers\UploadController.php protected\views\upload\index.php index.php代码: <?php $output_dir = "/images/

我试图上传文件,我不断得到“未能打开流:没有这样的文件或目录”。我已经把它缩小到了我的输出目录,但不确定为什么它说它不存在。我不确定yii2是否试图改变我的路线,或者发生了什么。任何信息都会有帮助,谢谢

文件夹结构:

www\images\uploads\
protected\controlers\UploadController.php
protected\views\upload\index.php
index.php代码:

<?php
$output_dir = "/images/uploads/";  // <-- this is where i think my issue is
if(isset($_FILES["myfile"]))
{
$ret = array();
$error =$_FILES["myfile"]["error"];
{
    //this is hardcoded for now just to test.
    session_start();
    $listing_ID = $_GET['id'];
    if(!is_array($_FILES["myfile"]['name'])) //single file
    {
        $ImageName      = str_replace(' ','-',strtolower($_FILES['myfile']['name']));
        $ImageType      = $_FILES['myfile']['type']; //"image/png", image/jpeg etc.
        $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
        $ImageExt       = str_replace('.','',$ImageExt);
        $ImageName      = substr(md5(uniqid(mt_rand(), true)), 0, 5);
        $NewImageName = $listing_ID.'_'.$ImageName.'.'.$ImageExt;
        move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $NewImageName);
        echo "<br> Error: ".$_FILES["myfile"]["error"];
        $ret['fileName']= $output_dir.$NewImageName;
        $image_entry = new Images();
        $image_entry->image = $NewImageName;
        $image_entry->listing_ID = $listing_ID;
        if (!$image_entry->save()) {
            print_r($image_entry->getErrors());
        }
    }
    else
    {
        $fileCount = count($_FILES["myfile"]['name']);
        for($i=0; $i < $fileCount; $i++)
        {
            $ImageName      = str_replace(' ','-',strtolower($_FILES['myfile']['name'][$i]));
            $ImageType      = $_FILES['myfile']['type'][$i]; //"image/png", image/jpeg etc.
            $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
            $ImageExt       = str_replace('.','',$ImageExt);
            $ImageName      = substr(md5(uniqid(mt_rand(), true)), 0, 5);
            $NewImageName = $listing_ID.'_'.$ImageName.'.'.$ImageExt;
            $ret[$NewImageName]= $output_dir.$NewImageName;
            move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$NewImageName );
            $image_entry = new Images();
            $image_entry->image = $NewImageName;
            $image_entry->listing_ID = $listing_ID;
            if (!$image_entry->save()) {
                print_r($image_entry->getErrors());
            }
        }
    }
}
echo json_encode($ret);
}
?>

我可以看到您的错误,这是因为您在代码的第一行中使用了相对路径。 使用绝对路径,则不会看到错误。 我猜你使用的是yii1版本。因此,这可能是语法,也可以通过谷歌搜索basepath的语法

在第一行中使用此代码:-
Yii::app()->basePath。”/图片/上传/'

现在,如果要打印$output\u dir。它将显示路径,如
var/www/html/your_app/images/uploads


请让我知道这是否对您有帮助。

结果为“PHP致命错误'yii\base\ErrorException'并显示消息'Class'Images'not found'”。请检查目录是否存在并授予权限。另外,打印$output_dir并查看绝对路径,然后检查它是否存在。文件夹存在,文件正在上载到文件夹中。它仍然会响应一个错误。您很可能会按照以下故障排除清单找到问题的原因:。我认为您的代码中有几个问题。其中一个似乎是相对路径,另一个是包含路径