Webiondev123's avatar

Unable to upload file on server : failed to open stream: Is a directory

Works on localhost. But when I upload on server it gives error

Here is my error code:

ErrorException in LecturerController.php line 1081:
file_put_contents(/var/www/k2b/data/www/laravel/dev.oasis-portal.my\files\tasks\dev site login.txt.txt): failed to open stream: Is a directory

My code:


    $title = Input::get('title');
    $description = Input::get('description');
    $group_id = Input::get('group_id');
    $date = Input::get('duedate');
    $filename=Input::get('filename');
    $lecturer_id = Input::get('lecturer_id');

    $timestamps = new \DateTime();

    $ext = Input::get('ext');
    $file =$filename.'.'.$ext;
    //$path = str_replace("/laravel","",base_path()).'/files/tasks/'.$file;

     $path = str_replace("\laravel\dev.oasis-portal.my","\dev.oasis-portal.my",base_path()).'\files\tasks\'.$file;

    $data = Request::get('file');

   

    list($type, $data) = explode(';', $data);
    list(, $data)      = explode(',', $data);

    $data = base64_decode($data);

    

    if (!is_dir(str_replace("\laravel\dev.oasis-portal.my","\dev.oasis-portal.my",base_path()).'\files\tasks\')) {

      File::makeDirectory($path,0777,true);
    }

    //Write to File
    $check = file_put_contents($path, $data);

     


    $fileinformation = array('title' => $title, 'description'=> $description, 'duedate'=>$date, 
      'lecturer_id'=>$lecturer_id, 'file' => $file, 'group_id' => $group_id, 'created_at'=>$timestamps, 'updated_at'=>NULL);


0 likes
4 replies
neilherbertuk's avatar

The directions of your slashes in file paths matter on Linux hosts. You are using a combination of both forward and back slashes. I'm going to guess your Dev environment is a Windows box? As it tends not matter on windows. Change your back slashes to forward slashes and see where that gets you.

You are also making a directory directly on the file you are trying to upload. Your path includes the file name, yet you are running File::makeDirectory($path,0777,true); on the filename.

Neil

Webiondev123's avatar

what is the correct syntax? i mean what should I include in makdeDirectory?

Snapey's avatar

I think the problem is possibly also that your filename contains spaces but when you use it you are not quoting the filename

try with a filename that does not have any spaces

I also notice that this line; $data = Request::get('file'); is unlikely to work unless the file is actually called 'file' with no path...

neilherbertuk's avatar
Level 9

Webiondev123

what is the correct syntax?

As I said in my reply, change your \ to / in your file paths.

If your file path is /var/www/project/folder/file.pdf, your script is trying to make that as a folder. Which will then fail to upload because you are trying to save it over something that already exists. You need to do your make directory command without the filename in the path. The if statement that checks to see if the folder exists doesn't include it, so your conditional doesn't even match what folder you are trying to make.

Please or to participate in this conversation.