Uploading a file to the server

I used to be able to do this, but suddenly it doesn't work anymore, I have no idea what I changed or what went wrong, I am just trying to upload an image to my blue host and this is the error I get:
Could not move the file "/tmp/phptcHTsU" to "\uploads\data\users\34/Ns3RJWE8FGZLG2gz.jpg" () 

      

This is my file upload code:

if (Input::file('upload')->isValid()) {
    $file = Input::file('upload');
    $ext = $file->getClientOriginalExtension();

    // Setting allowed file extensions
    $allowed = array('JPG', 'jpg', 'JPEG', 'jpeg', 'GIF', 'gif', 'PNG', 'png', 'BMP', 'bmp');

    if (!in_array($ext, $allowed)) {
        echo "Not Allowed!";
    }

    // Creating image upload path
    $destinationPath = public_path() . sprintf("\\uploads\\data\\users\\%d\\", Auth::user()->getId());
    $realPath = sprintf("uploads/data/users/%d/", Auth::user()->getId());

    if (!file_exists($destinationPath)) {
        mkdir($destinationPath, 0777, true);
    }

    $fileName = str_random(16);
    $image_url = $realPath . '/' . $fileName . '.' . $ext;

    if ($file->move($destinationPath, $fileName . '.' . $ext)) {
        $p->display_pic = $image_url;
    }
}

      

This file works on my local machine, which is window 8, but not on my Linux VPS server. I have no free disk space left.

+3


source to share


1 answer


The problem is that the directory separator is not the same on both OS (Linux and Windows).

You really need to use a constant DIRECTORY_SEPARATOR

.



$destinationPath = public_path() . DIRECTORY_SEPARATOR .
                   "uploads" . DIRECTORY_SEPARATOR .
                   "data" . DIRECTORY_SEPARATOR .
                   "users" . DIRECTORY_SEPARATOR .
                   Auth::user()->getId() . DIRECTORY_SEPARATOR;

      

+3


source







All Articles