Jump to content

Upload to FTP via PHP?


dhorn spm

Recommended Posts

Hello,

 

I need to make a page on my website that allows users to login with a username and password, then allows them to browse their computer for a file and upon clicking submit it should send their file to a folder on an FTP site, and the folder submitted to is decided by the username they used to login (different people have different file drop locations). It doesn't need a registering function, we will manual add names/passwords to the database.

 

Any ideas, suggestions, etc.?

 

Is this possible with PHP?

 

I've been working really hard on this and so far have no results.

 

Thanks,

Daniel

Link to comment
Share on other sites

It is possible with PHP, yes. If you want to just upload the file to a folder on your server you'll need to look into PHP Uploads. There are plenty of tutorials on this on the web just do a simple Google search and you should plenty information to get you going.

 

If it's FTP you're after consult this link...

 

http://www.w3schools.com/PHP/php_ref_ftp.asp

 

 

Link to comment
Share on other sites

If the user has a unique directory for the upload I would include it on the database record.

 

if the directory is just the user name then you can use the user name as the path i.e /web/user_area/$username/$uploaded_file

 

Keith

Link to comment
Share on other sites

It is possible with PHP, yes. If you want to just upload the file to a folder on your server you'll need to look into PHP Uploads. There are plenty of tutorials on this on the web just do a simple Google search and you should plenty information to get you going.

 

If it's FTP you're after consult this link...

 

http://www.w3schools.com/PHP/php_ref_ftp.asp

 

 

 

I've done several google searches and I've come close a few times but haven't really found what I'm looking for.  I'm brand spanking new to PHP and it seems to me that you would need a lot of PHP coding knowledge and experience, which I do not have. I'll keep trying, and I appreciate your help.

 

Please continue to give input on this issue you guys!

 

Thanks!

Link to comment
Share on other sites

// set up basic connection
$ftp_server = 'my.server.com';
$ftp_user_name = 'ftp_user_name';
$ftp_user_pass = 'ftp_user_password';
$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! ";
        echo " Attempted to connect to $ftp_server for user $ftp_user_name "; 
        exit; 
    } else {
        echo " Connected to $ftp_server, for user $ftp_user_name ";
    }

foreach ($_FILES["file"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
	$tmp_name = $_FILES["file"]["tmp_name"][$key];
	if($upload = ftp_put($conn_id, "/upload/dir/image.jpg", $tmp_name, FTP_BINARY)){
		echo 'File uploaded';
	}else{
		echo 'Upload File Error';
	}
}
}

Link to comment
Share on other sites

It may seem like a hard thing to do but that's only because you're thinking of the project as a whole. Try breaking it up and doing little parts and combining them and it will seem much easier.

 

I know. I didn't just start working on this. This is the third week, and it's becoming very frustrating. I haven't even really gotten to the FTP part yet I'm trying to get my login/passwords to work. I know how to go about doing it, but Dreamweaver won't let me connect to MySQL.

 

It keeps giving me an error message saying "HTML Error Message 405 - Method Not Allowed." I googled it with no help. I then called Adobe support who also couldn't help and said they'd have a Senior Technician call me. Well he called me, couldn't resolve the issue, told me to try installing PHP on a new machine and said that he'd send me instructions on how to do that. He also said he'd call back the next day to make sure I had everything working. Well I never got the instructions even though I have him two email addresses, and I never received a call back.

 

I eventually looked up a guide on installing PHP into a web server because the first time I installed it I just did it. The guide was helpful, but I'm still getting error messages when trying to connect Dreamweaver on my MySQL server. Worst part is this is just the local part on my machine; after I get this figured out I'll have to go to our internet host and set it all up there.

Link to comment
Share on other sites

// set up basic connection
$ftp_server = 'my.server.com';
$ftp_user_name = 'ftp_user_name';
$ftp_user_pass = 'ftp_user_password';
$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! ";
        echo " Attempted to connect to $ftp_server for user $ftp_user_name "; 
        exit; 
    } else {
        echo " Connected to $ftp_server, for user $ftp_user_name ";
    }

foreach ($_FILES["file"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
	$tmp_name = $_FILES["file"]["tmp_name"][$key];
	if($upload = ftp_put($conn_id, "/upload/dir/image.jpg", $tmp_name, FTP_BINARY)){
		echo 'File uploaded';
	}else{
		echo 'Upload File Error';
	}
}
}

 

Where did you find this? It looks familiar. I think I saw this or something similar on About.com PHP tutorials but I never got it to work.

Link to comment
Share on other sites

it uses an array of files to upload I forgot to mention in the previous post.

 

so...

 

<form action="upload.php" method="post" enctype="multipart/form-data">
File 1: <input type="file" name="files[]" /><br />
File 2: <input type="file" name="files[]" /><br />
<input type="submit" value="Upload" />
</form>

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.