Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

laravel url download file

    public static function saveFile($file, int $id, string $file_path = 'user_photo', $type = 'url'): string
    {
        if (is_null($file)) {
            return '';
        }

        switch ($file_path) {
            case 'user_photo':
                $path = '/assets/images/'.$file_path;
                break;
            default:
                return '';
        }
        $extension = pathinfo($file, PATHINFO_EXTENSION);
        $file_path = $path.'/'.$id.'.'.$extension;
        if ($type === 'url') {
            if (!file_exists(public_path($path))) {
                if (!mkdir($concurrentDirectory = public_path($path), 0777, true) && !is_dir($concurrentDirectory)) {
                    throw new RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
                }
            }
            copy($file, public_path($file_path));

            return $file_path;
        }

        return false;
    }
 
PREVIOUS NEXT
Tagged: #laravel #url #download #file
ADD COMMENT
Topic
Name
1+7 =