Sari Rahal
Sari Rahal

Reputation: 1955

yii2 upload directory doesn't exist

I am trying to post files for upload and I keep getting "failed to open stream: No such file or directory". I have narrowed it down to my output directory, and not really sure why it says it doesn't exist. I'm not sure if yii2 is trying to re-rout my path or what's going on. Any info would be helpful, Thanks.

Folder structure:

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

index.php Code:

<?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);
}
?>

Upvotes: 2

Views: 985

Answers (1)

Sudhanshu sharma
Sudhanshu sharma

Reputation: 1967

As i can see your error, It happens because you are using relative path in the first line of your code. Use absolute path then you will not see error. I guess you are using yii 1 version. so this may the syntax or you can google syntax for basepath.

Use this code in first line :- Yii::app()->basePath . '/images/uploads/';

Now if you will print $output_dir. It will show path like var/www/html/your_app/images/uploads

Please let me know if this helps you.

Upvotes: 1

Related Questions