Development #php#ftp 3 min

Doing an FTP from PHP

Translation of the original in Catalan. Read it in: CatalàCastellano

This is not a manual on how to do an FTP from PHP, as the title seems to suggest. For that there is already the PHP website, where you can look for information about the function ftp_put (es) for example.

What I want to explain is how the function ftp_put works internally because it is not clear at all in the PHP manual or in any help forum. Why is it so important to know this? well because otherwise the examples that are on the PHP websites or on the forums don't work at all.

I will focus the whole explanation on the example of the website. On the website there are two pieces of code one .html and .php, the first shows the form to upload the file and the second the code that does the ftp to upload the file:


<html>
	<body marginwidth=4 marginheight=4 topmargin=4 leftmargin=4 bgcolor=white vlink="#0000ff" link="#0000ff">
	<form name="Attachments"  method=POST action="sendimage.php" enctype="multipart/form-data">
	<input type=hidden name=box value="">
	<tr>
<td nowrap width="1%">&nbsp;&nbsp;<b>Image:</b></td>
<td colspan=2>
	<input type=file name=source_file size=20> <br>
	</td>
</tr>
<input type=submit name=btnSubmit value=Submit size=20 style="border: 1px solid #0000FF"></form>
	</body>
</html>

From this code it is important to notice, apart from what the website already explains, that the input field is of type 'file' and we give this field the name: 'source_file'. It would be normal to think that the type 'file' is equivalent to a string, but it is not like that, rather it is a table (array). Here you have an example of a value that the table of type 'file' called $sourcefile can take:


Array
(
    [name] => foto.jpg
    [type] => image/jpeg
    [tmp_name] => /tmp/phpFpMtnb
    [error] => 0
    [size] => 44050
)

From here it is important to realise that "name" contains the name of the file at the 'source site', the type contains the type of file that is being uploaded according to MIME, "tmp_name" (very important) contains the name of the temporary file, with which our file is saved on the PHP server. This file is very important because it is the file that will really be uploaded by FTP to the destination site.

In fact, it seems strange but if we think about it well it is obvious. Because PHP cannot do an ftp from our source directory, since it is code that runs on the server. Therefore, when PHP wants to do an FTP it can only do it with files that are on the server and not on our PC. I suppose now it is clear, the reason why.

In the "errors" field of the table the error code that the function ftp_put returns when doing the ftp is saved. Finally "size" tells us the size of the file.

The code of sendimage.php which is the file that the previous form calls is the following:


Hem de capturar l'array del form
$source_file=$_FILES["source_file"];
	$ftp_server='example.com';//serverip
$conn_id = ftp_connect($ftp_server);
	// login with username and password
$user="user";
$passwd="password";
$login_result = ftp_login($conn_id, $user, $passwd);
	// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
die;
} else {
echo "Connected to $ftp_server, for user $user";
}
	// Posem el mateix nom de destí que d'origen
$destination_file=basename($source_file["name"]);
echo ("");
print $destination_file;
	echo ("");
	// Pugem el fitxer temporal tal com hem explicat abans
$upload = ftp_put($conn_id, $destination_file, $source_file["tmp_name"], FTP_BINARY);
	// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file["name"] to $ftp_server as $destination_file";
}
	// close the FTP stream
ftp_close($conn_id);
?>

From this code it is important to notice two things:

  1. in the first line we capture the table (array) of type 'file' that the form sends us, this table (array) as we have already said is what contains the information of the file to send.

  2. the line of the ftp_put, you have to notice that as the source file we pass the field 'tmp_file' of the array of type 'file' called 'source_file'.

I suppose the rest of the code is very clear, if it is not then you know what you have to do: ask.

1 comment

Comments from the old blog, as they were written. Comments are closed.

  1. datarecovery