private function putFileInDirectory($directory, $localFile, $destinationName) { $createDirectory = false; // TODO confirm is this is the best way to do this // it maybe a noop but calling get_file_info might be faster try { $newFolder = new Box_Client_Folder(); $newFolder->attr('name', $directory); $newFolder->attr('parent_id', $this->boxFolderId); $newFolder->attr('share', 0); $status = $this->box->create($newFolder); if ($status !== self::statusCreatedOk && $status !== self::statusFolderExists) { getLogger()->warn(sprintf('Box API returned an unexpected response of (%s) from folder create call', $status)); return false; } } catch (Box_Rest_Client_Exception $e) { getLogger()->warn('Box exception from folder create call', $e); return false; } try { // The way Box_Rest_Client works it uses the file's name as the display name $file = new Box_Client_File($localFile, $destinationName); $file->attr('folder_id', $newFolder->attr('folder_id')); $result = $this->box->upload($file, array('new_copy' => '1'), true); if ($result === self::statusUploadOk) { $this->metaDataMap[$localFile] = array('boxFileId' => $file->attr('id')); getLogger()->info(sprintf('Successfully stored file (%s) on Box.', $destinationName)); return true; } else { getLogger()->crit('Could not put file on Box.', $e); return false; } } catch (Box_Rest_Client_Exception $e) { getLogger()->warn('Box exception from upload call', $e); return false; } }
/** * * Uploads the file to the specified folder. You can set the parent_id * attribute on the file for this to work. Because of how the API currently * works, be careful!! If you upload a file for the first time, but a file * of that name already exists in that location, this will automatically * overwrite it. * * If you use this method of file uploading, be warned! The file will bounce! * This means that the file will FIRST be uploaded to your servers and then * it will be uploaded to Box. If you want to bypass your server, call the * "upload_url" method instead. * * @param Box_Client_File $file * @param array $params A list of valid input params can be found at the Download * and upload method list at http://developers.box.net * @param bool $upload_then_delete If set, it will delete the file if the upload was successful * */ public function upload(Box_Client_File &$file, array $params = array(), $upload_then_delete = false) { if (array_key_exists('new_copy', $params) && $params['new_copy'] && intval($file->attr('id')) !== 0) { // This is a valid file for new copy, we can new_copy $url = $this->upload_url('new_copy', $file->attr('id')); } else { if (intval($file->attr('file_id')) !== 0 && !$new_copy) { // This file is overwriting another $url = $this->upload_url('overwrite', $file->attr('id')); } else { // This file is a new upload $url = $this->upload_url('upload', $file->attr('folder_id')); } } // assign a file name during construction OR by setting $file->attr('filename'); // manually $split = explode(DIRECTORY_SEPARATOR, $file->attr('localpath')); $split[count($split) - 1] = $file->attr('filename'); $new_localpath = implode(DIRECTORY_SEPARATOR, $split); // only rename if the old filename and the new filename are different if ($file->attr('localpath') != $new_localpath) { if (!copy($file->attr('localpath'), $new_localpath)) { throw new Box_Rest_Client_Exception('Uploaded file could not be renamed.'); } else { $file->attr('localpath', $new_localpath); } } $params['file'] = '@' . $file->attr('localpath'); $res = Rest_Client::post($url, $params); // This exists because the API returns malformed xml.. as soon as the API // is fixed it will automatically check against the parsed XML instead of // the string. When that happens, there will be a minor update to the library. $failed_codes = array('wrong auth token', 'application_restricted', 'upload_some_files_failed', 'not_enough_free_space', 'filesize_limit_exceeded', 'access_denied', 'upload_wrong_folder_id', 'upload_invalid_file_name'); if (in_array($res, $failed_codes)) { return $res; } else { $res = $this->parse_result($res); } // only import if the status was successful if ($res['status'] == 'upload_ok') { $file->import($res['files']['file']); // only delete if the upload was successful and the developer requested it if ($upload_then_delete) { unlink($file->attr('localpath')); } } return $res['status']; }