Hits : 2703

File Upload



Τα παρακάτω προγράμματα υλοποιούν την διαδικασία του file upload με δύο τρόπους.
Ο πρώτος είναι σαφέστατα ο πιο απλός, ο δεύτερος πιο "επαγγελματικός" καθώς
γίνεται η αποθήκευση ενός αρχείου από το μηχάνημα μας στον διακομιστή μέσω ftp.

Web File Upload


<?php
if ( empty($_FILES['uploadedfile']['name']) ) {
?>
 
<form enctype="multipart/form-data" action="" method="POST">
Choose a file to upload : <input name="uploadedfile" type="file" />
<input type="submit" value="Upload File" />
</form>
 
<?php
} else {
 
$target_path = "uploads/";
 
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name'])." has been uploaded";
} else
    echo "There was an error uploading the file, please try again!";
 
}
?>

top


Ftp File Upload


<?php
if ( empty($_FILES['uploadedfile']['name']) ) {
?>
 
<form enctype="multipart/form-data" action="" method="POST">
Choose a file to upload : <input name="uploadedfile" type="file" />
<input type="submit" value="Upload File" />
</form>
 
<?php
} else {
 
// configuration
$source_file = $_FILES['uploadedfile']['tmp_name'];
$ftp_path = "ftp/files/";
$destination_file = $ftp_path.$_FILES['uploadedfile']['name'];
 
$ftp_server = localhost;
$ftp_user_name = nobody;
$ftp_user_pass = lampp;
 
// set up basic connection
$conn_id = ftp_connect($ftp_server); 
 
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
 
// check connection
if ((!$conn_id) || (!$login_result)) {
  echo "<span style='color:#FF0000'><h2>FTP connection has failed!</h2></span> <br />";
  echo "Attempted to connect to $ftp_server for user $ftp_user_name";
  exit; // end of program
} else
  echo "Connected to <b>$ftp_server</b>, for user <b>$ftp_user_name</b>";
 
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
 
if (!$upload)  // check upload status
    echo "<span style='color:#FF0000'><h2>FTP upload of $destination_file has failed!</h2></span> <br />";
else
    echo "<span style='color:#339900'><h2>Uploading $name Completed Successfully!</h2></span><br /><br />";
 
// close the FTP stream 
ftp_close($conn_id);
 
}
 
?>

top