Download move_to_folder.php from Urban-Guy/Drive: direct link, hf CLI and curl.
- Browser
- Download file 2.3 kB
-
https://huggingface.co/spaces/Urban-Guy/Drive/resolve/main/move_to_folder.php
- Command line
-
hf download hf://spaces/Urban-Guy/Drive/move_to_folder.php
-
curl -L -o move_to_folder.php https://huggingface.co/spaces/Urban-Guy/Drive/resolve/main/move_to_folder.php
2.3 kB
| session_start(); | |
| $metadataFile = 'data/files.json'; | |
| function loadMetadata() { | |
| global $metadataFile; | |
| if (!file_exists($metadataFile)) return ['files' => []]; | |
| return json_decode(file_get_contents($metadataFile), true); | |
| } | |
| function saveMetadata($metadata) { | |
| global $metadataFile; | |
| file_put_contents($metadataFile, json_encode($metadata, JSON_PRETTY_PRINT)); | |
| } | |
| if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['fileId']) && isset($_POST['folderId'])) { | |
| $fileId = $_POST['fileId']; | |
| $folderId = $_POST['folderId']; | |
| $metadata = loadMetadata(); | |
| // Find the file and target folder | |
| $fileToMove = null; | |
| $targetFolder = null; | |
| $fileKey = null; | |
| foreach ($metadata['files'] as $key => &$file) { | |
| if ($file['id'] === $fileId) { | |
| $fileToMove = &$file; | |
| $fileKey = $key; | |
| } | |
| if ($file['id'] === $folderId) { | |
| $targetFolder = $file; | |
| } | |
| } | |
| if ($fileToMove && $targetFolder && $targetFolder['type'] === 'folder') { | |
| // Move the actual file or folder | |
| $oldPath = $fileToMove['path']; | |
| $fileName = basename($oldPath); | |
| $newPath = $targetFolder['path'] . '/' . $fileName; | |
| // Check if destination already exists | |
| if (file_exists($newPath)) { | |
| echo json_encode([ | |
| 'success' => false, | |
| 'error' => 'A file or folder with this name already exists in the destination' | |
| ]); | |
| exit; | |
| } | |
| if (rename($oldPath, $newPath)) { | |
| // Update metadata | |
| $fileToMove['path'] = $newPath; | |
| $fileToMove['parentFolder'] = $folderId; | |
| $fileToMove['modified'] = time(); | |
| saveMetadata($metadata); | |
| echo json_encode([ | |
| 'success' => true, | |
| 'message' => 'File moved successfully' | |
| ]); | |
| } else { | |
| echo json_encode([ | |
| 'success' => false, | |
| 'error' => 'Failed to move file on disk' | |
| ]); | |
| } | |
| } else { | |
| echo json_encode([ | |
| 'success' => false, | |
| 'error' => 'File or folder not found' | |
| ]); | |
| } | |
| exit; | |
| } | |
| echo json_encode([ | |
| 'success' => false, | |
| 'error' => 'Invalid request' | |
| ]); | |