1. Go to this page and download the library: Download webfiori/file library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use WebFiori\File\FileUploader;
$uploader = new FileUploader('/uploads');
// Configure allowed file types
$uploader->addExts(['jpg', 'png', 'gif', 'pdf', 'docx']);
// Remove specific extensions
$uploader->removeExt('gif');
// Set form input name
$uploader->setAssociatedFileName('user_files');
// Get current configuration
$allowedTypes = $uploader->getExts();
$uploadDir = $uploader->getUploadDir();
$inputName = $uploader->getAssociatedFileName();
// Get maximum upload size (from PHP configuration)
$maxSize = FileUploader::getMaxFileSize(); // Returns size in KB
use WebFiori\File\File;
$file = new File('image.jpg');
$file->read();
// Get Base64 encoded content
$encoded = $file->getRawData(true);
echo "Base64: " . $encoded;
// Decode Base64 data
$file2 = new File();
$file2->setRawData($encoded, true); // true = decode from Base64
$decoded = $file2->getRawData();
// Write encoded file
$file->writeEncoded(); // Creates 'image.jpg.bin' with Base64 content
// Read and decode encoded file
$encodedFile = new File('image.jpg.bin');
$encodedFile->readDecoded();
$originalData = $encodedFile->getRawData();
use WebFiori\File\File;
// Serve file for download
$file = new File('/path/to/document.pdf');
$file->read();
$file->view(true); // true = force download
// Serve file inline (display in browser)
$file->view(false); // false = display inline
// The view() method automatically:
// - Sets appropriate Content-Type header
// - Handles Content-Range for partial requests
// - Sets Content-Disposition header
// - Outputs file content
use WebFiori\File\File;
// Process large file in chunks
$file = new File('/path/to/large-file.mp4');
$file->read();
// Get 1KB chunks, Base64 encoded
$chunks = $file->getChunks(1024, true);
foreach ($chunks as $index => $chunk) {
echo "Processing chunk " . ($index + 1) . "/" . count($chunks) . "\\n";
// Store chunk in database or process separately
// Each chunk is Base64 encoded and 1KB in size
storeChunkInDatabase($index, $chunk);
}
// Get raw chunks (not encoded)
$rawChunks = $file->getChunks(1024, false);
public function __construct(string $fName = '', string $fPath = '')
use WebFiori\File\File;
use WebFiori\File\Exceptions\FileException;
try {
$file = new File('/path/to/nonexistent.txt');
$file->read();
} catch (FileException $e) {
echo "Error: " . $e->getMessage();
// Handle specific error scenarios
switch ($e->getCode()) {
case 0:
// File not found
break;
default:
// Other errors
break;
}
}
use WebFiori\File\FileUploader;
$uploader = new FileUploader('/secure/uploads');
// 1. Restrict file types
$uploader->addExts(['jpg', 'png', 'pdf']); // Only allow safe file types
// 2. Validate file size (handled by PHP settings)
// Set in php.ini: upload_max_filesize = 5M
// 3. Use secure upload directory outside web root
// Store files outside public_html or www directories
// 4. Sanitize file names (handled automatically)
// The library normalizes file paths and names
use WebFiori\File\File;
// For large files, use chunked processing
$file = new File('/path/to/large-video.mp4');
$file->read();
// Process in 1MB chunks to avoid memory issues
$chunks = $file->getChunks(1024 * 1024, false);
foreach ($chunks as $chunk) {
// Process each chunk separately
processChunk($chunk);
}
use WebFiori\File\File;
$file = new File('/path/to/document.pdf');
$file->read();
// Enable range requests for better streaming
$file->view(false); // Serves with proper HTTP headers
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.