Skip to content

Commit 05d6b3b

Browse files
author
Cerlin
committed
Added option to upload a file with custom name.
1 parent ba63402 commit 05d6b3b

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ So far this SDK has only machine to machine authentication mechanism and the fol
6060
2. Methods available in `$file_service`
6161

6262
1. uploadPreFlight($file_path = "", $folder_id = 0);
63-
2. upload($file_path = "", $folder_id = 0);
63+
2. upload($file_path = "", $folder_id = 0, $filename = null); // If file name is null, filename will be derived from actual file name.
6464
3. getEmbedUrl($file_id)
6565
4. delete($file_id)
6666
5. destroyTrashedFile($file_id)

src/Box/Services/Files/FileService.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,16 @@ private function readFile($file_path)
7474
* Method to upload the file. By default it uploads to the root folder which is id 0
7575
* @param string $file_path
7676
* @param int $folder_id
77+
* @param string $filename
7778
* @return \GuzzleHttp\Psr7\Response
7879
*/
79-
public function upload($file_path = "", $folder_id = 0)
80+
public function upload($file_path = "", $folder_id = 0, $filename = null)
8081
{
8182
Assert::integerish($folder_id, "The folder id must be an integer. Got: %s");
8283

8384
$handle = $this->readFile($file_path);
8485

85-
$file_name = basename($file_path);
86+
$filename = ((!is_null($filename)) ? $filename : basename($file_path));
8687

8788
return $this->guzzle_client->request(
8889
'POST',
@@ -92,7 +93,7 @@ public function upload($file_path = "", $folder_id = 0)
9293
[
9394
"name" => "attributes",
9495
"contents" => json_encode([
95-
"name" => $file_name,
96+
"name" => $filename,
9697
"parent" => [
9798
"id" => $folder_id
9899
]

0 commit comments

Comments
 (0)