CS  beginner
CS beginner

Reputation: 23

Upload a file to a server using FTP using php

How do I a upload a file to xampp server using FTP and php?

<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

$file = "localfile.txt";

// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
  {
  echo "Successfully uploaded $file.";
  }
else
  {
  echo "Error uploading $file.";
  }

// close connection
ftp_close($ftp_conn);
?>

Thats all I found. Can't create my own server and access it.

Upvotes: 1

Views: 1180

Answers (2)

islamoc
islamoc

Reputation: 36

First of you need to setup FileZilla in Xampp(this is for Windows)

  1. Start Xampp and Start FileZilla server from the Control Panel and go to the C:\XAMPPFOLDER\FileZillaFTP
  2. Start The server interface
  3. Go to Edit->Users and add a new User
  4. Use 127.0.0.1 as the server name 21 as the port and the added user and password as the login details

Upvotes: 0

Chris G
Chris G

Reputation: 780

$file = "localfile.txt";
$tmp_name = $file["tmp_name"];
$name = $file["name"];
define ('SITE_ROOT', realpath(dirname(__FILE__)));  <-- Goes to current folder where your files is located.
move_uploaded_file($tmp_name, SITE_ROOT."/FOLDERTOUPLOADTO/$name");

If you want to, I can give you a script where you can use multiple input files and put them in your FTP. Just contact me in a message. And this way, you don't need the FTP login and such.

Upvotes: 1

Related Questions