Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
ftp_alloc() function in PHP
The ftp_alloc() function allocates space for a file to be uploaded to the FTP server. This function is useful when uploading large files to ensure sufficient disk space is available on the server before attempting the transfer.
Syntax
ftp_alloc(connection, size_of_file, res);
Parameters
connection − The FTP connection resource to use
size_of_file − The number of bytes to allocate on the server
res − Optional variable to store the server response message
Return Value
The ftp_alloc() function returns TRUE on success or FALSE on failure.
Example
The following example demonstrates how to allocate space before uploading a file −
<?php
$myFile = "demo.txt";
$con = ftp_connect('192.168.0.4');
$login_result = ftp_login($con, 'username', 'password');
if (ftp_alloc($con, filesize($myFile), $res)) {
echo "Sending $myFile<br>";
ftp_put($con, 'remote_file.txt', $myFile, FTP_BINARY);
} else {
echo "Failed in allocating space! Message = $res<br>";
}
ftp_close($con);
?>
Key Points
Not all FTP servers support the
ALLOcommand that this function usesThe function is primarily useful for large file transfers
Always check the return value before proceeding with file upload
The
$resparameter captures server response messages for debugging
Conclusion
The ftp_alloc() function helps ensure sufficient server space before file uploads. While not supported by all FTP servers, it's a good practice for large file transfers to prevent upload failures due to insufficient disk space.
