Jump to content

[SOLVED] FTP Uploader via Web


nouseforaname

Recommended Posts

Hi,

 

I'm totally newbie in PHP, here I just found a PHP code + HTML which can upload about 10 files to web hosting via web (FTP web browser). And here's the code:

 

HTML

<html>
<head>
<title>VainSoft - Upload Form</title>
</head>
<body>

<p>Upload Images here, 10 images at a time, hit back after upload to upload more. (100M MAX)</p>
<p>
<form name="fileup" method="post" enctype="multipart/form-data" action="upload.php">
username<input type="text" name="username"><br>
urlname<input type="text" name="urlname"><br>
passwd<input type="password" name="passwd"><br>
<input type="file" name="userfiles[]"><br>
<input type="file" name="userfiles[]"><br>
<input type="file" name="userfiles[]"><br>
<input type="file" name="userfiles[]"><br>
<input type="file" name="userfiles[]"><br>
<input type="file" name="userfiles[]"><br>
<input type="file" name="userfiles[]"><br>
<input type="file" name="userfiles[]"><br>
<input type="file" name="userfiles[]"><br>
<input type="file" name="userfiles[]"><br>
<br>
<!-- change below to your max -->
<input type="hidden" name="MAX_FILE_SIZE" value="1000000000">
<input type="submit" value="submit" name="submit">
</form>

 

PHP

<?
//uses $_FILES[] global array
//see manual for older PHP version info

//This function will be used to get the extension from the filename
Function get_extension($file,$length=-1){
$p = strrpos($file,".");
$p++;
If($length!=-1){
$ext = substr($file,$p,$length);
}
If($length==-1){
$ext = substr($file,$p);
}
$ext = strtolower($ext);
Return $ext;
}

//Not good practice, but here anyway
//change to suit your needs
//also some have to be set in the ini
//for this to correctly work

//2meg max
Ini_set("upload_max_filesize","100M");

//turn on file uploads
Ini_set("file_uploads","1");

//set your temp dir
Ini_set("upload_tmp_dir","/tmp");

//set post size large enough to accomidate
//3 100meg files and some overhead
Ini_set("post_max_size","180M");

?>

</p>
<?
//check to see if we have submited yet
If($_POST["submit"]!="submit"){
//not yet so lets make the form
?>

<?
}
//see if we have submited and that the files array has been set
If(($_POST["submit"]=="submit")&&(is_array($_FILES['userfiles']))){
echo $_POST['username'];
echo $_POST['urlname'];
echo $_POST['passwd'];
$ftp_user_name=$_POST['username']; //change to ftp username $username = 
$ftp_user_pass=$_POST['passwd']; //change to ftp password
$ftp_server=$_POST['urlname']; //change to ftp url
$ftp_dump_dir="/upload"; //change to destination directory

//go through all the files
For($x=0;$x<count($_FILES['userfiles']['name']);$x++){

//now we do some file checking

//check to see if file is there
If($_FILES['userfiles']['name'][$x]!="none"){
//file has a name
//check filesize
If($_FILES['userfiles']['size'][$x]!=0){
//file is larger than 0 bytes
//Check to see if it is uploaded
If(is_uploaded_file($_FILES['userfiles']['tmp_name'][$x])){
//file has been uploaded!
//let the user know their file has be uploaded
Echo "file ".$_FILES['userfiles']['name'][$x]." uploaded!<br>";
//conect to ftp server
$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 "FTP connection has failed!<br>";
Echo "Attempted to connect to $ftp_server for user $ftp_user_name";
Exit;
} else {
Echo "Connected to $ftp_server! <br>";
//set PASV mode
If(!ftp_pasv($conn_id,TRUE)){
Echo "Could not enter PASV mode!";
}
//rename to file#_date.Ext
$filename = $_FILES['userfiles']['name'][$x];
//$filename.= ".".Get_extension($_FILES['userfiles']['name'][$x],3);

//change directory
If (@ftp_chdir($conn_id, $ftp_dump_dir)) {
//maybe you want to make sure we are in the correct directory
Echo "Current directory is now : ", ftp_pwd($conn_id), "\and";
} else {
//you want to know if it didn't work
Echo "Couldn't change directory\and";
}

//upload the file and let the user know what happened
If(ftp_put($conn_id,$filename,$_FILES['userfiles']['tmp_name'][$x],FTP_BINARY)){
Echo "File ".$_FILES['userfiles']['name'][$x]." was sent successfully<br>";
Echo "File was named ".$filename."<br>";
}else{
Echo "There was a problem sending file ".$_FILES['userfiles']['name'][$x]."<br>";;
}
}
// close the FTP stream
Ftp_close($conn_id);
}
Else echo"File was not uploaded!<br>";
}
}
Echo "<br>";

}//end for loop

}
//That's all folks!
?>

 

This script is working, but only can upload the files from the computer source.

 

What I want here is I also can select a file from another web files. Let's say I have a webhosting which it is saving the templates. When people using this script, people will enter their details (Username, Password and ftp.urlname.com). Once they click on submit, all the web templates from my webhosting will be copied to their webhosting.

 

It's like I'm giving a free templates to my user, I just want to simplify it because some of them doesn't know how to use FTP program. With this, they can easily have their own website in no time.

 

Thanks in advance! :)

Link to comment
Share on other sites

there are php functions for ftp to move/copy files and create folders from server to server, here's some function I've used in the past and examples of there use should be helpful.

 

Functions

 

<?php
//------------- functions

//--------------------------- connect and login to server
function ftpconnect($host,$user,$pass)
{
global $conn;
$conn=ftp_connect($host) or die("Could not connect to $host");
//login to server
ftp_login($conn,$user,$pass);
}

//--------------------------  close ftp connections
function ftpClose($conn)
{
//close ftp connection
ftp_close($conn);
}

//------------------------ create directory
function ftpMkDir($conn,$dirToCreate)
{
 @ftp_mkdir($conn,$dirToCreate);	

 if(file_exists($dirToCreate))
 {
 	echo "<p>Sorry the folder: $dirToCreate already exists</p>\n";
 } else {
 	echo "<p>Folder: $dirToCreate created</p>\n";
 }
}

//------------------------ set directory permissions
function ftpFolderPermission($conn,$folderChmod)
{
if (ftp_chmod($conn, 0777, $folderChmod) !== false)
{
		echo "<p>$folderChmod chmoded successfully to 0777</p>\n";
}
}

//---------------------- copy file to new server
function copyFile($conn,$remoteFile,$localFile)
{
if (ftp_put($conn,$remoteFile,$localFile,FTP_ASCII))
{
		echo "<p>successfully uploaded $localFile to $remoteFile</p>\n";
} else {
		echo "<p>There was a problem while uploading $remoteFile</p>\n";
}
}


//----------- end of functions -------------------------
?>

 

Example of usage:

 

<?php
//connect to server
ftpconnect("ftp.server.com","username","password");

echo "<h2>Create Folders</h2>\n";
//create directory
ftpMkDir($conn,"path/to/folder");

echo "<h2>Set Folder Permissions</h2>\n";
//change folder mermissions to 0777
ftpFolderPermission($conn,"path/to/folder");

echo "<h2>Upload Files</h2>\n";
//copy file to new server
//connection then path to copy file to then path to file on this server
copyFile($conn,"path/to/file/filename.ext","filename.ext");

//close ftp connection
ftpClose($conn);
?>

Link to comment
Share on other sites

I'm sorry to ask again, can I have the full script including the HTML file. I'm kinda too new to PHP and I have no idea passing all the arguments or connecting everything. And if can, would ya please rename the file so I wont get confused.

 

As for example:  upload.php, ftp.html etc.

 

Thanks in advance

Link to comment
Share on other sites

thats all the files I made for it, it's a very basic there is no html files/interface as when I made it, it was just to copy files from one server to another so I din't need any sort of visuals for it.

 

The functions are there to make is easier to reuse the code, to say copy a file from one server to another, you use the ftp connect function and supply the ftp login to the remote server you want to copy to then use the copyfile function and pass the folder to where you want to copy the file to then the path to where the file is on the current server.

 

ftpconnect("ftp.server.com","username","password");

copyFile($conn,"path/to/file/filename.ext","filename.ext");

Link to comment
Share on other sites

Garethp, actually I just need a bit of manipulation from code I add above. I've tried to do

 

<input type="text" src="http://www.example-source.com/templates.html" name="userfiles[]">

 

But it just won't able to detect the source. Btw, here's an example you might want to look:

http://www.xxlincome.com/ftp.php

Link to comment
Share on other sites

there are php functions for ftp to move/copy files and create folders from server to server, here's some function I've used in the past and examples of there use should be helpful.

 

Functions

 

<?php
//------------- functions

//--------------------------- connect and login to server
function ftpconnect($host,$user,$pass)
{
global $conn;
$conn=ftp_connect($host) or die("Could not connect to $host");
//login to server
ftp_login($conn,$user,$pass);
}

//--------------------------  close ftp connections
function ftpClose($conn)
{
//close ftp connection
ftp_close($conn);
}

//------------------------ create directory
function ftpMkDir($conn,$dirToCreate)
{
 @ftp_mkdir($conn,$dirToCreate);	

 if(file_exists($dirToCreate))
 {
 	echo "<p>Sorry the folder: $dirToCreate already exists</p>\n";
 } else {
 	echo "<p>Folder: $dirToCreate created</p>\n";
 }
}

//------------------------ set directory permissions
function ftpFolderPermission($conn,$folderChmod)
{
if (ftp_chmod($conn, 0777, $folderChmod) !== false)
{
		echo "<p>$folderChmod chmoded successfully to 0777</p>\n";
}
}

//---------------------- copy file to new server
function copyFile($conn,$remoteFile,$localFile)
{
if (ftp_put($conn,$remoteFile,$localFile,FTP_ASCII))
{
		echo "<p>successfully uploaded $localFile to $remoteFile</p>\n";
} else {
		echo "<p>There was a problem while uploading $remoteFile</p>\n";
}
}


//----------- end of functions -------------------------
?>

 

Example of usage:

 

<?php
//connect to server
ftpconnect("ftp.server.com","username","password");

echo "<h2>Create Folders</h2>\n";
//create directory
ftpMkDir($conn,"path/to/folder");

echo "<h2>Set Folder Permissions</h2>\n";
//change folder mermissions to 0777
ftpFolderPermission($conn,"path/to/folder");

echo "<h2>Upload Files</h2>\n";
//copy file to new server
//connection then path to copy file to then path to file on this server
copyFile($conn,"path/to/file/filename.ext","filename.ext");

//close ftp connection
ftpClose($conn);
?>

 

Thanks a lot man, it working.. just a minor error when I tried to get the credentials by using variable. Btw here's the codes I manipulated:

 

HTML

<form name="fileup" method="post" enctype="multipart/form-data" action="upload3.php">
username<input type="text" name="username"><br>
urlname<input type="text" name="urlname"><br>
passwd<input type="password" name="passwd"><br>

 

PHP

<?php
//------------- functions

//--------------------------- connect and login to server
function ftpconnect($host,$user,$pass)
{
   global $conn;
   $conn=ftp_connect($host) or die("Could not connect to $host");
   //login to server
   ftp_login($conn,$user,$pass);
}

//--------------------------  close ftp connections
function ftpClose($conn)
{
   //close ftp connection
   ftp_close($conn);
}

//------------------------ create directory
function ftpMkDir($conn,$dirToCreate)
{
    @ftp_mkdir($conn,$dirToCreate);   
    
    if(file_exists($dirToCreate))
    {
       echo "<p>Sorry the folder: $dirToCreate already exists</p>\n";
    } else {
       echo "<p>Folder: $dirToCreate created</p>\n";
    }
}

//------------------------ set directory permissions
function ftpFolderPermission($conn,$folderChmod)
{
   if (ftp_chmod($conn, 0777, $folderChmod) !== false)
   {
       echo "<p>$folderChmod chmoded successfully to 0777</p>\n";
   }
}

//---------------------- copy file to new server
function copyFile($conn,$remoteFile,$localFile)
{
   if (ftp_put($conn,$remoteFile,$localFile,FTP_ASCII))
   {
       echo "<p>successfully uploaded $localFile to $remoteFile</p>\n";
   } else {
       echo "<p>There was a problem while uploading $remoteFile</p>\n";
   }
}


//----------- end of functions -------------------------
?>



<?php
//connect to server
ftpconnect($host=$_POST['urlname'],$user=$_POST['username'],$pass=$_POST['passwd']);

echo "<h2>Create Folders</h2>\n";
//create directory
ftpMkDir($conn,"folder");

echo "<h2>Set Folder Permissions</h2>\n";
//change folder mermissions to 0777
ftpFolderPermission($conn,"folder");

echo "<h2>Upload Files</h2>\n";
//copy file to new server
//connection then path to copy file to then path to file on this server
copyFile($conn,"folder/aaa.html","http://www.example-of-url.com/aaa.html");

//close ftp connection
ftpClose($conn);
?>

 

Got this only (minor error), FYI the folder is first time created.

 

Create Folders

 

Sorry the folder: folder already exists

 

If anyone can try out to fix this, thanks :)

 

Link to comment
Share on other sites

No wonder it didn't read the function, i read it from upload3.php and not upload4.php. All the data from HTML been sent to upload3.php before while I working on upload4.php and now I get new error:

 

ftpconnect($host=$_POST['urlname'],$user=$_POST['username'],$pass=$_POST['passwd']);

 

Sigh, I'm just too noob in this. Btw thanks to you nuttycoder, you are really helping me out.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.