write an index.php that takes a file as an upload and saves a folder incrementally in…
- User
write an index.php that takes a file as an upload and saves a folder incrementally in this folder on the server as a..z0..9, then of two characters, then of three, etc. Each folder will contain an index.php showing the initial name of the uploaded file and the /download/ button.
Assistant
Here's a basic
index.phpscript that allows users to upload a file, and then saves it in an incremental folder structure (a,b, ..., z, 0, ..., 9, thenaa,ab`, etc.). Each folder will contain:- The uploaded file.
- An
index.phpfile inside the folder that shows the original filename and a "Download" button.
✅ Folder Structure Logic
Folders are generated incrementally like this:
upload/ ├── a/ │ ├── index.php │ └── userfile.ext ├── b/ │ ├── index.php │ └── userfile.ext ... ├── z/ ├── 0/ ... ├── 9/ ├── aa/ ├── ab/ ...Each folder contains:
- The uploaded file (renamed to something unique if needed).
- An
index.phpfile showing the original name and a download link.
📁 File:
index.php(main upload form)<?php function generateNextFolderName($dir) { $chars = array_merge(range('a', 'z'), range(0, 9)); $folders = glob($dir . '*', GLOB_ONLYDIR); $lastFolder = end($folders); if (!$lastFolder) { return $dir . 'a/'; } $folderName = basename($lastFolder); $len = strlen($folderName); $pos = $len - 1; while ($pos >= 0) { $currentChar = $folderName[$pos]; $index = array_search($currentChar, $chars); if ($index !== false && $index < count($chars) - 1) { // Increment character $newChar = $chars[$index + 1]; $folderName = substr_replace($folderName, $newChar, $pos, 1); // Reset characters after current position if not at start for ($i = $pos + 1; $i < $len; $i++) { $folderName[$i] = $chars[0]; } return $dir . $folderName . '/'; } $pos--; } // All characters rolled over, add new level return $dir . str_repeat($chars[0], $len + 1) . '/'; } $uploadDir = 'uploads/'; if (!is_dir($uploadDir)) { mkdir($uploadDir, 0777, true); } if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) { $file = $_FILES['file']; if ($file['error'] === UPLOAD_ERR_OK) { $originalName = basename($file['name']); $targetFolder = generateNextFolderName($uploadDir); if (!is_dir($targetFolder)) { mkdir($targetFolder, 0777, true); } $targetFile = $targetFolder . 'upload' . strtolower(strrchr($originalName, '.')); if (move_uploaded_file($file['tmp_name'], $targetFile)) { // Create index.php inside the folder $indexContent = <<<EOT <?php \$originalName = "$originalName"; echo "<h2>Original Filename: \$originalName</h2>"; echo "<a href='$targetFile'><button>Download</button></a>"; ?> EOT; file_put_contents($targetFolder . 'index.php', $indexContent); echo "<p>File uploaded successfully to: <a href='$targetFolder'>/$targetFolder</a></p>"; } else { echo "<p>Error moving the uploaded file.</p>"; } } else { echo "<p>Error uploading file. Code: {$file['error']}</p>"; } } ?> <!DOCTYPE html> <html> <head> <title>Incremental Upload Folder</title> </head> <body> <h1>Upload a File</h1> <form method="post" enctype="multipart/form-data"> <input type="file" name="file" required> <button type="submit">Upload</button> </form> </body> </html>
📂 Example Output
After several uploads, your
uploads/directory may look like this:uploads/ ├── a/ │ ├── upload.jpg │ └── index.php ├── b/ │ ├── upload.pdf │ └── index.php └── c/ ├── upload.docx └── index.phpEach
index.phpdisplays the original name and a download button.
🔐 Security Notes
This is a basic demo. For production use:
- Validate and sanitize file types.
- Restrict file size.
- Use secure permissions.
- Consider renaming files with UUIDs or hashes.
- Avoid allowing direct execution of PHP files from uploads.
Would you like me to help secure this further?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.