Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Upload file with ftp instead of http..

Status
Not open for further replies.

mrdance

Programmer
Apr 17, 2001
308
SE
I am using this script for ftp upload. It seems that the ftp connection is working the only problem that is left is that it doesn't find the file. I get the error: Warning: fopen("C:\\a.txt","r") - No such file or directory in C:\web\neteject\pictureuploader\ftp.php on line 31

I thought that I could use the "browse". A strange thing is that it doesn't find the file when I hardcode it neither.


$fp=fopen("$UploadedFile", "r");
ftp_fput($ftp,"$UploadedFile", $fp, FTP_ASCII);
fclose($fp);
ftp_quit($ftp);

<FORM METHOD=post ACTION=ftp.php>
<input CLASS=xxblabgre3t type=&quot;file&quot; size=40 name=&quot;UploadedFile&quot;>
<input CLASS=xxblabgre3 type=submit name=upload value=&quot;UPLOAD FILE&quot;>
</FORM>

Any help would be appreciated!

// Henrik
 
Try this code from the PHP manual. Since PHP has FTP functions built in, I think that you will find them much better to use:

[tt]
<?php
// set up basic connection
$conn_id = ftp_connect(&quot;$ftp_server&quot;);

// login with username and password
$login_result = ftp_login($conn_id, &quot;$ftp_user_name&quot;, &quot;$ftp_user_pass&quot;);

// check connection
if ((!$conn_id) || (!$login_result)) {
echo &quot;Ftp connection has failed!&quot;;
echo &quot;Attempted to connect to $ftp_server for user $user&quot;;
die;
} else {
echo &quot;Connected to $ftp_server, for user $user&quot;;
}

// upload the file
$upload = ftp_put($conn_id, &quot;$destination_file&quot;, &quot;$source_file&quot;, FTP_BINARY);

// check upload status
if (!$upload) {
echo &quot;Ftp upload has failed!&quot;;
} else {
echo &quot;Uploaded $source_file to $ftp_server as $destination_file&quot;;
}

// close the FTP stream
ftp_quit($conn_id);
?>

[/tt]

Also, in your form tag, you must have the following code:

[tt]
ENCTYPE=&quot;multipart/form-data&quot; so it looks like:
<form action=&quot;ftp.php&quot; method=&quot;POST&quot; ENCTYPE=&quot;multipart/form-data&quot;>
[/tt]


Hope this helps.

-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
Thank you, but why should I put ENCTYPE=&quot;multipart/form-data&quot; in the form? I understand it when I use a HTTP upload when the upload actually is connected to the form. In ftp-mode I only use the type=file of the inputbox to get a browser window which results in a filename which I can use later, not in the actual post. Am I wrong?

// Henrik
 
I believe, and I am not 100% sure on this, that you put the ENCTYPE=&quot;multitype/form-data&quot; to be able to upload binary files (such as images).

Thats just what I have heard. And I have always been told to add that when making an upload script.

Sorry I could not of been of more help.

-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
Here is a simple script which will allow you to utilize ftp instead of http for file uploads. it will also allow you to upload files to specific directories (don't forget to set permissions correctly on dirs/files).

If you look closely at the script, you will notice that there is no enctype=&quot;multitype/form-data&quot; required.

<?php

// quick &quot;die&quot; function
function funk_die($msg) {
if($msg=='normal') {
return &quot;<html><body><a href=\&quot;ftp.php\&quot;>back</a></body></html>&quot;;
}
}

// function to give dir listing stuff
function dir_funk () {

// cookie?
global $ftp;

// dir name
$dir=ftp_pwd($ftp);

// dir listing array
$dirlist=ftp_rawlist($ftp,$dir);

// print the directory listing
$funk = &quot;<html><body><p>current directory: <b> $dir </b></p>&quot;;
$funk .= &quot;<p><a href=\&quot;ftp.php?&updir=$dir\&quot;>go up one level</a>
</p>&quot;;

$funk .= &quot;<p>
<table cellspacing=0 cellpadding=2 border=1>
<tr><td><b>permissions</b></td><td><b>file/directory</b></td>
<td><b>download</b></td><td><b>delete</b></td>
<td><b>chmod</b></td><td><b>rename</b></td>
</tr>
&quot;;

while(list($i,$thingy)=each($dirlist)) {

$thingy=preg_replace(&quot;/\s+/&quot;, &quot; &quot;, $thingy);

if($i==0) {
echo &quot;&quot;;
} else {
list($chmod,$thing,$owner,$group,$size,$month,$day,$time,$filename)=explode(&quot; &quot;,$thingy);
if(ereg(&quot;d&quot;,$chmod)) {
$funk .= &quot;<tr><td>$chmod</td><td><a href=\&quot;ftp.php?dir=$dir/$filename\&quot;>$filename</a></td>
<td>dir</td>
<td><a href=\&quot;ftp.php?delete=dir&thingy=$dir/$filename\&quot;>delete</a></td>
<td><a href=\&quot;ftp.php?chmod=1&thingy=$dir/$filename\&quot;>chmod</a></td>
<td><a href=\&quot;ftp.php?rename=$dir/$filename\&quot;>rename</a></td>
</tr>&quot;;
} else {
$funk .= &quot;<tr><td>$chmod</td><td>$filename</td>
<td><a href=\&quot;ftp.php?dl=1&file=$dir/$filename\&quot;>d/l ($size)</a></td>
<td><a href=\&quot;ftp.php?delete=file&thingy=$dir/$filename\&quot;>delete</a></td>
<td><a href=\&quot;ftp.php?chmod=1&thingy=$dir/$filename\&quot;>chmod</a></td>
<td><a href=\&quot;ftp.php?rename=$dir/$filename\&quot;>rename</a></td>
</tr>&quot;;
}
}

}

$funk .= &quot;</table>
</p>&quot;;

$funk .= &quot;<p>Create a new directory<br>
<form action=\&quot;ftp.php\&quot; method=\&quot;POST\&quot;>
<input size=\&quot;15\&quot; name=\&quot;name\&quot;>
<input type=\&quot;hidden\&quot; name=\&quot;current_dir\&quot; value=\&quot;$dir\&quot;>
<input type=\&quot;submit\&quot; name=\&quot;newdir\&quot; value=\&quot;create_dir\&quot;>
</form>
</p>&quot;;

$funk .= &quot;<p>Upload a local file<br>
<form action=\&quot;ftp.php\&quot; method=\&quot;POST\&quot;>
<input type=\&quot;file\&quot; name=\&quot;localfile\&quot;><br>
<input type=\&quot;hidden\&quot; name=\&quot;current_dir\&quot; value=\&quot;$dir\&quot;>
<input type=\&quot;submit\&quot; name=\&quot;upload\&quot; value=\&quot;upload_file\&quot;>
</form>
</p>&quot;;

$funk .= &quot;</body></html>&quot;;

return $funk;
}


if(!$login) {
// print the login form

echo &quot;<html><body><p><b>ftp login</b><hr></p>
<form action=\&quot;ftp.php\&quot; method=\&quot;POST\&quot;>
<p>Your server (eg ftp.yoursite.com)<br> <input size=\&quot;30\&quot; name=\&quot;server\&quot;></p>
<p>Username <br> <input size=\&quot;30\&quot; name=\&quot;username\&quot;></p>
<p>Password <br> <input size=\&quot;30\&quot; name=\&quot;password\&quot;></p>
<p><input type=\&quot;submit\&quot; name=\&quot;login\&quot; value=\&quot;login\&quot;></p>
</form></body></html>&quot;;
} else {

// try to connect or die with an error
$ftp=ftp_connect($server) or die(&quot;invalid server&quot;);
// try to login or die with error
$conn=ftp_login($ftp,$username,$password) or die(&quot;login failed for some reason&quot;);
// set a cookie so the !$login form above is never displayed
setcookie(&quot;login&quot;,&quot;active&quot;);
// set goody cookies
setcookie(&quot;server&quot;,$server);
setcookie(&quot;username&quot;,$username);
setcookie(&quot;password&quot;,$password);

if($dir) {

ftp_chdir($ftp,$dir);
echo dir_funk();

} elseif($dl) {

if($get) {

if(!$local) {
die(&quot;please enter a value&quot;);
}

ftp_get($ftp, $local, $file, FTP_BINARY);
echo funk_die(normal);

} else {
echo &quot;<html><body>&quot;;
echo &quot;download file&quot;;
$size=ftp_size($ftp,$file);
$time=ftp_mdtm($ftp,$file);
$time=date(&quot;d M Y, H i&quot;,$time);
echo &quot;<p><table cellspacing=0 cellpadding=2 border=1>
<tr><td><b>filename</b></td><td><b>size</b></td>
<td><b>date modified</b></td></tr>
<tr><td>$file</td><td>$size</td><td>$time</td></tr></table>&quot;;
echo &quot;<form action=\&quot;ftp.php\&quot; method=\&quot;POST\&quot;>
where to download the file to (/home/me/file.txt or c:/file.txt):<br>
<INPUT NAME=\&quot;local\&quot; size=\&quot;40\&quot;>
<input type=\&quot;hidden\&quot; name=\&quot;dl\&quot; value=\&quot;1\&quot;>
<input type=\&quot;hidden\&quot; name=\&quot;file\&quot; value=\&quot;$file\&quot;><br>
<INPUT TYPE=\&quot;submit\&quot; name=\&quot;get\&quot; VALUE=\&quot;download file\&quot;>
</FORM>&quot;;
echo &quot;</body></html>&quot;;

}

} elseif($rename) {

if($do) {

if(!$val) {
die(&quot;please enter a value&quot;);
}

ftp_rename($ftp, $rename, $val);
echo funk_die(normal);

} else {

echo &quot;<html><body>
rename this file: $rename
<p>
<form action=\&quot;ftp.php\&quot; method=\&quot;POST\&quot;>
Rename to: <input size=\&quot;50\&quot; name=\&quot;val\&quot; value=\&quot;$rename\&quot;><br>
<input type=\&quot;hidden\&quot; name=\&quot;rename\&quot; value=\&quot;$rename\&quot;>
<input type=\&quot;submit\&quot; name=\&quot;do\&quot; value=\&quot;rename\&quot;>
</form>
</body></html>&quot;;

}

} elseif($chmod) {

if($do) {

if(!$val) {
die(&quot;please enter a value&quot;);
}

$cmd=&quot;CHMOD $val $file&quot;;
ftp_site($ftp, $cmd);
echo funk_die(normal);

} else {

echo &quot;<html><body>
chmod this file: $thingy
<p>
<form action=\&quot;ftp.php\&quot; method=\&quot;POST\&quot;>
<input size=\&quot;4\&quot; name=\&quot;val\&quot; maxlength=\&quot;4\&quot;> CHMOD value<br>
<input type=\&quot;hidden\&quot; name=\&quot;file\&quot; value=\&quot;$thingy\&quot;>
<input type=\&quot;hidden\&quot; name=\&quot;chmod\&quot; value=\&quot;1\&quot;>
<input type=\&quot;submit\&quot; name=\&quot;do\&quot; value=\&quot;chmod\&quot;>
</form>
</body></html>&quot;;

}

} elseif($delete) {

if($delete=='file') {
ftp_delete($ftp, $thingy);
} elseif($delete=='dir') {
ftp_rmdir($ftp, $thingy);
}
echo funk_die(normal);

} elseif($upload) {

if(!$localfile) { die(&quot;please enter a file&quot;); }

$file = substr( strrchr( $localfile, &quot;/&quot; ), 1 );
$put_thingy = $current_dir . &quot;/&quot; . $file;
ftp_put($ftp, $put_thingy, $localfile, FTP_BINARY);
echo funk_die(normal);

} elseif($newdir) {

if(!$name) { die(&quot;please enter a name&quot;); }

$newdir = $current_dir . &quot;/&quot; . $name;
ftp_mkdir($ftp,$newdir);
echo funk_die(normal);

} elseif($updir) {

$newdir = str_replace( substr( strrchr( $updir, &quot;/&quot; ), 1 ) , &quot;&quot;, $updir);

ftp_chdir($ftp,$newdir);
echo dir_funk();

} else {

echo dir_funk();

}
}

?>
 
Thanks, but I have errors when trying to upload a file;

Warning: ftp_put: pictureuploader: Cannot create file. in C:\web\neteject\ftp.php on line 214

and when logging in I get this error sometimes;

Warning: Variable passed to each() is not an array or object in C:\web\neteject\ftp.php on line 35


I have read/write permissions on both the directory and ftp-user.


Thanks/ Henrik

 
Sorry, did not test this before submitting it.

Here is the updated script. I did test this one and it worked like a charm.

chad.


<?php

// quick &quot;die&quot; function
function funk_die($msg) {
if($msg=='normal') {
return &quot;<html><body><a href=\&quot;ftp.php\&quot;>back</a></body></html>&quot;;
}
}

// function to give dir listing stuff
function dir_funk () {

// cookie?
global $ftp;

// dir name
$dir=ftp_pwd($ftp);

// dir listing array
$dirlist=ftp_rawlist($ftp,$dir);

// print the directory listing
$funk = &quot;<html><body><p>current directory: <b> $dir </b></p>&quot;;
$funk .= &quot;<p><a href=\&quot;ftp.php?&updir=$dir\&quot;>go up one level</a>
</p>&quot;;

$funk .= &quot;<p>
<table cellspacing=0 cellpadding=2 border=1>
<tr><td><b>permissions</b></td><td><b>file/directory</b></td>
<td><b>download</b></td><td><b>delete</b></td>
<td><b>chmod</b></td><td><b>rename</b></td>
</tr>
&quot;;

while(list($i,$thingy)=each($dirlist)) {

$thingy=preg_replace(&quot;/\s+/&quot;, &quot; &quot;, $thingy);

if($i==0) {
echo &quot;&quot;;
} else {
list($chmod,$thing,$owner,$group,$size,$month,$day,$time,$filename)=explode(&quot; &quot;,$thingy);
if(ereg(&quot;d&quot;,$chmod)) {
$funk .= &quot;<tr><td>$chmod</td><td><a href=\&quot;ftp.php?dir=$dir/$filename\&quot;>$filename</a></td>
<td>dir</td>
<td><a href=\&quot;ftp.php?delete=dir&thingy=$dir/$filename\&quot;>delete</a></td>
<td><a href=\&quot;ftp.php?chmod=1&thingy=$dir/$filename\&quot;>chmod</a></td>
<td><a href=\&quot;ftp.php?rename=$dir/$filename\&quot;>rename</a></td>
</tr>&quot;;
} else {
$funk .= &quot;<tr><td>$chmod</td><td>$filename</td>
<td><a href=\&quot;ftp.php?dl=1&file=$dir/$filename\&quot;>d/l ($size)</a></td>
<td><a href=\&quot;ftp.php?delete=file&thingy=$dir/$filename\&quot;>delete</a></td>
<td><a href=\&quot;ftp.php?chmod=1&thingy=$dir/$filename\&quot;>chmod</a></td>
<td><a href=\&quot;ftp.php?rename=$dir/$filename\&quot;>rename</a></td>
</tr>&quot;;
}
}

}

$funk .= &quot;</table>
</p>&quot;;

$funk .= &quot;<p>Create a new directory<br>
<form action=\&quot;ftp.php\&quot; method=\&quot;POST\&quot;>
<input size=\&quot;15\&quot; name=\&quot;name\&quot;>
<input type=\&quot;hidden\&quot; name=\&quot;current_dir\&quot; value=\&quot;$dir\&quot;>
<input type=\&quot;submit\&quot; name=\&quot;newdir\&quot; value=\&quot;create_dir\&quot;>
</form>
</p>&quot;;

$funk .= &quot;<p>Upload a local file<br>
<form action=\&quot;ftp.php\&quot; method=\&quot;POST\&quot; enctype=\&quot;multipart/form-data\&quot;>
<input type=\&quot;file\&quot; name=\&quot;localfile\&quot;><br>
<input type=\&quot;hidden\&quot; name=\&quot;current_dir\&quot; value=\&quot;$dir\&quot;>
<input type=\&quot;submit\&quot; name=\&quot;upload\&quot; value=\&quot;upload_file\&quot;>
</form>
</p>&quot;;

$funk .= &quot;</body></html>&quot;;

return $funk;
}


if(!$login) {
// print the login form

echo &quot;<html><body><p><b>ftp login</b><hr></p>
<form action=\&quot;ftp.php\&quot; method=\&quot;POST\&quot;>
<p>Your server (eg ftp.yoursite.com)<br> <input size=\&quot;30\&quot; name=\&quot;server\&quot;></p>
<p>Username <br> <input size=\&quot;30\&quot; name=\&quot;username\&quot;></p>
<p>Password <br> <input size=\&quot;30\&quot; name=\&quot;password\&quot;></p>
<p><input type=\&quot;submit\&quot; name=\&quot;login\&quot; value=\&quot;login\&quot;></p>
</form></body></html>&quot;;
} else {

// try to connect or die with an error
$ftp=ftp_connect($server) or die(&quot;invalid server&quot;);
// try to login or die with error
$conn=ftp_login($ftp,$username,$password) or die(&quot;login failed for some reason&quot;);
// set a cookie so the !$login form above is never displayed
setcookie(&quot;login&quot;,&quot;active&quot;);
// set goody cookies
setcookie(&quot;server&quot;,$server);
setcookie(&quot;username&quot;,$username);
setcookie(&quot;password&quot;,$password);

if($dir) {

ftp_chdir($ftp,$dir);
echo dir_funk();

} elseif($dl) {

if($get) {

if(!$local) {
die(&quot;please enter a value&quot;);
}

ftp_get($ftp, $local, $file, FTP_BINARY);
echo funk_die(normal);

} else {
echo &quot;<html><body>&quot;;
echo &quot;download file&quot;;
$size=ftp_size($ftp,$file);
$time=ftp_mdtm($ftp,$file);
$time=date(&quot;d M Y, H i&quot;,$time);
echo &quot;<p><table cellspacing=0 cellpadding=2 border=1>
<tr><td><b>filename</b></td><td><b>size</b></td>
<td><b>date modified</b></td></tr>
<tr><td>$file</td><td>$size</td><td>$time</td></tr></table>&quot;;
echo &quot;<form action=\&quot;ftp.php\&quot; method=\&quot;POST\&quot;>
where to download the file to (/home/me/file.txt or c:/file.txt):<br>
<INPUT NAME=\&quot;local\&quot; size=\&quot;40\&quot;>
<input type=\&quot;hidden\&quot; name=\&quot;dl\&quot; value=\&quot;1\&quot;>
<input type=\&quot;hidden\&quot; name=\&quot;file\&quot; value=\&quot;$file\&quot;><br>
<INPUT TYPE=\&quot;submit\&quot; name=\&quot;get\&quot; VALUE=\&quot;download file\&quot;>
</FORM>&quot;;
echo &quot;</body></html>&quot;;

}

} elseif($rename) {

if($do) {

if(!$val) {
die(&quot;please enter a value&quot;);
}

ftp_rename($ftp, $rename, $val);
echo funk_die(normal);

} else {

echo &quot;<html><body>
rename this file: $rename
<p>
<form action=\&quot;ftp.php\&quot; method=\&quot;POST\&quot;>
Rename to: <input size=\&quot;50\&quot; name=\&quot;val\&quot; value=\&quot;$rename\&quot;><br>
<input type=\&quot;hidden\&quot; name=\&quot;rename\&quot; value=\&quot;$rename\&quot;>
<input type=\&quot;submit\&quot; name=\&quot;do\&quot; value=\&quot;rename\&quot;>
</form>
</body></html>&quot;;

}

} elseif($chmod) {

if($do) {

if(!$val) {
die(&quot;please enter a value&quot;);
}

$cmd=&quot;CHMOD $val $file&quot;;
ftp_site($ftp, $cmd);
echo funk_die(normal);

} else {

echo &quot;<html><body>
chmod this file: $thingy
<p>
<form action=\&quot;ftp.php\&quot; method=\&quot;POST\&quot;>
<input size=\&quot;4\&quot; name=\&quot;val\&quot; maxlength=\&quot;4\&quot;> CHMOD value<br>
<input type=\&quot;hidden\&quot; name=\&quot;file\&quot; value=\&quot;$thingy\&quot;>
<input type=\&quot;hidden\&quot; name=\&quot;chmod\&quot; value=\&quot;1\&quot;>
<input type=\&quot;submit\&quot; name=\&quot;do\&quot; value=\&quot;chmod\&quot;>
</form>
</body></html>&quot;;

}

} elseif($delete) {

if($delete=='file') {
ftp_delete($ftp, $thingy);
} elseif($delete=='dir') {
ftp_rmdir($ftp, $thingy);
}
echo funk_die(normal);

} elseif($upload) {

if(!$localfile) { die(&quot;please enter a file&quot;); }

if(!$local_fp = fopen($localfile,&quot;r&quot;)) {
echo &quot;unable to open $localfile for reading<br>\n&quot;;
exit();
}

if(eregi(&quot;application|image&quot;,$localfile_type)) {
$ftp_type = FTP_BINARY;
}
else {
$ftp_type = FTP_ASCII;
}

ftp_fput($ftp, $current_dir.&quot;/&quot;.$localfile_name, $local_fp, $ftp_type);

header(&quot;Location: ftp.php?dir=&quot;.$current_dir);

echo funk_die(normal);

} elseif($newdir) {

if(!$name) { die(&quot;please enter a name&quot;); }

$newdir = $current_dir . &quot;/&quot; . $name;
ftp_mkdir($ftp,$newdir);
echo funk_die(normal);

} elseif($updir) {

$newdir = str_replace( substr( strrchr( $updir, &quot;/&quot; ), 1 ) , &quot;&quot;, $updir);

ftp_chdir($ftp,$newdir);
echo dir_funk();

} else {

echo dir_funk();

}
}

?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top