Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading file on server

previously i was able to do this but suddenly it does not work anymore, i have no idea what i changed or what went wrong, i am simply trying to upload an image to my bluehost sever 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;
    }
}

The above file works on my local machine which is windows 8 but not on my Linux VPS server. I have not run out of disk space.

like image 689
user3718908x100 Avatar asked Nov 22 '25 04:11

user3718908x100


1 Answers

The problem is with the directory separator not being the same on the two OS (Linux and Windows).

You really should use the constant DIRECTORY_SEPARATOR instead.

$destinationPath = public_path() . DIRECTORY_SEPARATOR .
                   "uploads" . DIRECTORY_SEPARATOR .
                   "data" . DIRECTORY_SEPARATOR .
                   "users" . DIRECTORY_SEPARATOR .
                   Auth::user()->getId() . DIRECTORY_SEPARATOR;
like image 198
β.εηοιτ.βε Avatar answered Nov 24 '25 20:11

β.εηοιτ.βε