FTP address - ftp9.Jabry.com
Username - florinsteaua
Password - florinsteaua
Click here for larger image
Figure 1. Setting the FileZilla Site Manager by providing the corresponding information
Status: Resolving address of ftp9.Jabry.com
Status: Connecting to 67.208.91.118:21...
Status: Connection established, waiting for welcome message...
Response: 220 users9 V2 WS_FTP Server 5.0.4 (0)
Command: USER florinsteaua
Response: 331 Password required
Command: PASS ************
Response: 230 user logged in
Command: SYST
Response: 215 UNIX
Command: FEAT
Response: 500 illegal command
Status: Connected
Status: Retrieving directory listing...
Command: PWD
Response: 257 "/florinsteaua" is current directory
Command: TYPE I
Response: 200 Type set to IMAGE.
Command: PASV
Response: 227 Entering Passive Mode (67,208,91,118,14,129).
Command: LIST
Response: 150 Opening ASCII data connection for directory listing
Response: 226 transfer complete
Status: Directory listing successful
Click here for larger image
Figure 2. Using the FileZilla client to see the main folder of the site
<html>
<body>
<form action="ftp.php" method="post" enctype="multipart/form-data">
<div>
<label for="upload">Select file</label>
<input name="upload" type="file" />
<input type="submit" name="Submit" value="Upload" />
</div>
</form>
</body>
</html>
Click here for larger image
Figure 3. The HTML form used to browse the file that will be uploading
<?php
if (isset($_POST['Submit'])) {
if (!empty($_FILES['upload']['name'])) {
//Initialize a cURL session
$ch = curl_init();
$localpicture = $_FILES['upload']['tmp_name'];
$fp = fopen($localpicture, 'r');
//Set the URL for cURL to work with
curl_setopt($ch, CURLOPT_URL, 'ftp://florinsteaua:florinsteaua@ftp9.Jabry.com/'.$_FILES['upload']['name']);
// Sets the TRUE option to prepare for an upload
curl_setopt($ch, CURLOPT_UPLOAD, TRUE);
//Sets the input file for the transfer
curl_setopt($ch, CURLOPT_INFILE, $fp);
//Sets the size of the file to be sent, in bytes
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localpicture));
//Executing the upload
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'File upload error.';
}
} else {
$error = 'Please select a file.';
}
echo $error;
}
?> $error_no=0 then everything worked fine ( you can find all the error corresponding to their number of the error here and you should get the message : 'File uploaded successfully.', like in the next figure:
Click here for larger image
Figure 4. The FTP OK message listed into the browser
Click here for larger image
Figure5. The site structure and content after uploading the picture_to_upload.jpg picture
<?php
if (isset($_POST['Submit'])) {
if (!empty($_FILES['upload']['name'])) {
$ch = curl_init();
$localfile = $_FILES['upload']['tmp_name'];
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp://florinsteaua:florinsteaua@ftp9.Jabry.com/'.$_FILES['upload']['name']);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
//SSL settings
//To stop cURL from verifying the peer's certificate use the value 0 or False.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//1 to check the existence of a common name in the SSL peer certificate.
//2 to check the existence of a common name and also verify that it matches the hostname provided. 2 is the default value.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
//The FTP authentication method : CURLFTPAUTH_SSL (try SSL first), CURLFTPAUTH_TLS (try TLS first), or CURLFTPAUTH_DEFAULT (let cURL decide).
curl_setopt($ch, CURLOPT_FTP_SSL, CURLOPT_FTPSSLAUTH);
//Try using SSL, proceed as normal otherwise.
curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
//Sets the username and password to use for the connection.
curl_setopt($ch, CURLOPT_USERPWD, 'florinsteaua:florinsteaua');
//Sets the SSL version
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
//end SSL
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
$error_msg = curl_error($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'File upload error.';
}
}
echo $error;
curl_close ($ch);
}
?> <?php
// Initialize session and set URL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'file:///D:/Apache2.2/htdocs/php/cURL_protocols/FILE.txt');
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get the response and close the channel.
$response = curl_exec($ch);
echo $response;
curl_close($ch);
?>
Click here for larger image
Figure6. Using the FILE protocol
<?php
//Initializes a new session and return a cURL handle for use with the curl_setopt(), curl_exec() and curl_close() functions
$curl = curl_init();
//Sets an option on the given cURL session handle like url, timeout, return transfer
curl_setopt($curl, CURLOPT_URL, 'http://www.google.ro');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Execute the given cURL session (get the content of the url and put it into the output variable)
$output = curl_exec($curl);
// Outputs the result
echo $output;
// Print the curl info like http response code, content type etc.
// print_r (curl_getinfo($curl));
// close the curl handle to free system resources
curl_close($curl);
?>
Click here for larger image
Figure 7. Outputting a simple HTTP session using cURL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);) and one that checks the peer certificate, verify the host and then checks the certificate used to verify the peer. This certificate can be obtain from the restricted page like this: in the Tools menu of the browser (Mozilla Firefox in this case), choose the option Page Info -> Security Tab -> Details Tab -> View Certificate and select the certificate at the top of the hierarchy and then Export it into your corresponding folder to your application. I choose to put it together with my https.php script, also listed next:
Click here for larger image
Figure 8. The folder containing the certificate and the https.php script
<?php
// Initialize session and set URL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://encrypted.google.com/');
curl_setopt($ch, CURLOPT_TIMEOUT, 80);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Stops cURL from verifying the peer's certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Sets the cURL to verify the peer's certificate
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
// 1 to check the existence of a common name in the SSL peer certificate.
//2 to check the existence of a common name and also verify that it matches the hostname provided.
//2 is the default value
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
//The name of a file holding one or more certificates to verify the peer with. This only makes sense when used in combination with CURLOPT_SSL_VERIFYPEER.
//curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "GTECyberTrustGlobalRoot.crt");
// Get the response and close the channel.
$response = curl_exec($ch);
echo $response;
print_r (curl_getinfo($ch));
curl_close($ch);
?>
Click here for larger image
Figure 9. Using the HTTPS protocol to and also display the page info using the curl_getinfo function