Forum Moderators: coopster

Message Too Old, No Replies

ftp_put() not finding source file

         

tryan

2:37 am on Aug 13, 2004 (gmt 0)

10+ Year Member



[htmlhelp.com ] says that current browsers generally ignore type=file attributes, even when ENCTYPE="multipart/form-data" is in the <FORM> tag. And my php file doesn't seem to be receiving the files that I thought I was POSTing from my html form. I suspect these two points are connected.

However, I want to ftp_put() files to my server and I'm not entirely sure how else to allow my user to find the files.

Any workarounds? Or am I likely just doing it wrong? How do you all make the ftp_put() know what file you want to upload from your hard disk?

BTW, I am successfully connecting, and a file does, in fact, show up where I want it to on my server with the name I give it, but it's not the one I tell it to upload in my <input type="file"...> tag.

Tom

tryan

2:56 am on Aug 13, 2004 (gmt 0)

10+ Year Member



I can do better than that.
Here's the html form:
-------------------------------------
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="ftp.php" method="post" name="ftp_form" enctype="multipart/form-data">
<p>Host Name:<br/>
<input name="ftp_server" type="text" value="www.servername.com">
</p>
<p> User Name:<br/>
<input name="user_name" type="text">
</p>
<p> Password:<br/>
<input name="user_pass" type="text">
</p>
<p><input name="file_name" type="file"></p>
<input name="submit" type="submit" value="upload">
</form>
</body>
</html>
-------------------------------------
and the php that should upload it:
-------------------------------------
<?php

$ftp_server=$_POST['ftp_server'];
$conn_id = ftp_connect($ftp_server);

$ftp_user_name=$_POST['user_name'];
$ftp_user_pass=$_POST['user_pass'];

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

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";
}

ftp_chdir($conn_id, "htdocs");
ftp_chdir($conn_id, "hostPics");

$destination_file = "x.txt";

$source_file = $_POST['file_name'];

$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $_POST['file_name']";
}

ftp_close($conn_id);
?>
---------------------------------
Again, it does upload a file called x.txt into the directory called hostPics on the server, but x.txt isn't the same as the file I selected from my hard disk.

I know, most of you have probably seen this code before and I am aware of the danger of cutting and pasting found-on-the-internet code, but it all looks good and mostly works.

Any ideas?

Thanks in advance,

Tom

tryan

2:49 pm on Aug 13, 2004 (gmt 0)

10+ Year Member



Or is it just wishful thinking that I can upload a file from my local computer to a remote webserver via php's ftp functions.

I'm thinking that may be the case.

coopster

4:00 am on Aug 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What is it exactly that you are trying to do? It sounds like you want to allow a user to upload a file to server A, then have the script in the form action on server A initiate a file transfer from server A to server B?

tryan

2:21 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



Nope. I'm basically trying to write a simple upload-only ftp client that I can have appear on a page within my customer's site. The code would be in a php script on the server, using php's ftp functions.

I want my customer to be able to upload pictures from his own computer to a remote server. This server hosts his site. But I want him to be able to open a password protected page on his site from any computer, and upload files from that computer to his site.

So, to modify your example, I want him to upload from Client A to Web-Server B.

But I'm wondering if that's even possible. I've downloaded a few php apps, like online photo albums, and they don't allow for uploading... they thumbnail and manipulate, but they normally say to upload files "with your favourite ftp program"

Maybe I should say the same thing.

?

Tom

coopster

3:41 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Handling file uploads [php.net] is a very common practice, and no, it does not require you to use FTP.

tryan

5:52 pm on Aug 17, 2004 (gmt 0)

10+ Year Member



Handling file uploads is a very common practice, and no, it does not require you to use FTP.

Interesting. Thanks, I'd never seen that.

Tom