Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Creating FTP upload page

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Creating FTP upload page

Postby Ryyo » Sat Mar 15, 2003 7:08 pm

Howdy all,

I am very new to php, and I'm trying to create a simple page where files can be uploaded to an ftp server. My biggest problem right now is trying to figure out how to have the script name the destination file the same as the source file. The code I am working with is as follows:

HTML FORM:

<html>

<body marginwidth=4 marginheight=4 topmargin=4 leftmargin=4 bgcolor=white vlink="#0000ff" link="#0000ff">

<form name="Attachments" method=POST action="sendfile.php" enctype="multipart/form-data">

<input type=hidden name=box value="">

<tr>
<td nowrap width="1%">&&<b>Image:</b></td>
<td colspan=2>
<input type=file name=source_file size=20>


&&&&&

</td>
</tr>
<input type=submit name=btnSubmit value=Submit size=20 style="border: 1px solid #0000FF"></form>
</body>
</html>


PHP SCRIPT:

<?php

$ftp_server='xxx.xxx.xxx.xxx';//serverip
$conn_id = ftp_connect($ftp_server);


// login with username and password
$user="username";
$passwd="*****";
$login_result = ftp_login($conn_id, $user, $passwd);

// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
die;
} else {
echo "
Connected to $ftp_server, for user $user
";
}
//directorylike /www.something.com/images
ftp_chdir($conn_id, "documents");

//$destination_file=ftp_pwd($conn_id);

//below is where my problem lies, I want it to have the same name as the source file

$destination_file="misc.doc";
echo ("
");
print $destination_file;

echo ("
");

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

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

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

Can I just ditch the destination file entries, and have it upload the source file?

Any input you could provide would be much obliged!

-
Ryyo
New php-forum User
New php-forum User
 
Posts: 4
Joined: Sat Mar 15, 2003 6:55 pm

Postby sleepingdanny » Mon Mar 17, 2003 6:44 am

Why don't you just use a regular copy process ? :?

$source_file (Its the the uploading input)
Code: Select all
<?
$path="path to directory";
$max_size=1000000;
$source_file_size_kb=(int)($source_file_size/1000);
if(is_uploaded_file($source_file)){
if($source_file_size>$max_size){
print "<font color=red><b>ERROR</b>:</font> The File's Size Is More Then <b>1Mb</b>!<br>"; exit;}
if($source_file_type=="text/html" || $source_file_type=="application/octet-stream"){
print "<font color=red><b>ERROR</b>:</font> You Are <b>Not</b> Allowed To Upload This Type Of File!<br><br>"; exit;}
$do=copy($source_file, $path . $source_file_name);
if($do){
print "<font color=red><b>Name</b>:</font> $source_file_name<br>";
print "<font color=red><b>URL</b>:</font> $http://.../$source_file_name<br>";
print "<font color=red><b>Size</b>:</font> $source_file_size_kb KB <br>";
print "<font color=red><b>Type</b>:</font> $source_file_type<br><br>";
if($source_file_type=="image/gif" || $source_file_type=="image/jpg" || $source_file_type=="image/jpeg" || $source_file_type=="image/bmp"){
print "<u><b>Picture Preview</b></u><br><br>";
print "<img border=0 src=http://.../$source_file_name><br><br><br>";
}
}
elseif(!$do){
print "<font color=red><b>ERROR</b>:</font> Uploading Failed! Please Try Again Or Email The <b>Webmaster</b>.<br><br>";
}
}
?>
sleepingdanny
New php-forum User
New php-forum User
 
Posts: 13
Joined: Mon Mar 17, 2003 6:38 am

Postby Ryyo » Mon Mar 17, 2003 10:14 am

Whoa, I'm not sure I understand this, but I appreciate your help none the less!

I assume I substitute this for what I have as the upload process in my code?

Ryyo
Ryyo
New php-forum User
New php-forum User
 
Posts: 4
Joined: Sat Mar 15, 2003 6:55 pm

Postby sleepingdanny » Tue Mar 18, 2003 1:25 am

Explanation:
:arrow: You can set max size for the uploaded files - 100000 = 1MB.
:arrow: If the file that you want to upload is bigger then 1MB it will show an error.
:arrow: You can set type of files that are not allowed to be uploaded - html,asp...
:arrow: You tell the code what to do after the file is uploaded - here it shows it's name... url... size and type and if its an image file, then it will preview the image to the user...

Very Easy To Understand :P

:idea: You need to set a form...

<form method="POST" action="">
<input type="file" name="source_file" value="<?=$source_file?>">
<br>
<input type="submit" name="upload" value="Upload">
</form>
Last edited by sleepingdanny on Wed Mar 19, 2003 3:29 am, edited 3 times in total.
sleepingdanny
New php-forum User
New php-forum User
 
Posts: 13
Joined: Mon Mar 17, 2003 6:38 am

Postby Ryyo » Tue Mar 18, 2003 11:09 am

I'll try this right away, thanks sleepingdanny!

Ryyo
Ryyo
New php-forum User
New php-forum User
 
Posts: 4
Joined: Sat Mar 15, 2003 6:55 pm

Postby sleepingdanny » Wed Mar 19, 2003 3:30 am

I made a small mistake in the form code... its fixed now! :P
sleepingdanny
New php-forum User
New php-forum User
 
Posts: 13
Joined: Mon Mar 17, 2003 6:38 am

Postby ph » Wed Mar 19, 2003 3:34 am

For $59 you can buy the dreamweaver extension 'pure php upload'. It does exactly what you want (I think), but you need to use Dreamweaver (MX). See this site: http://www.dmxzone.com/ShowDetail.asp?NewsId=4509.
ph
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Mar 13, 2003 2:14 am

Postby Xerpher » Wed Mar 19, 2003 8:48 am

Or you could use the freely posted code above.
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby ph » Wed Mar 19, 2003 9:40 am

That indeed is the $59 saving option :)
ph
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Mar 13, 2003 2:14 am

Postby Xerpher » Wed Mar 19, 2003 9:59 am

Lol... I have Dreamweaver MX, what is it exactly you were talking about anyway?
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby ph » Wed Mar 19, 2003 10:03 am

It's an extension, meaning that you can download and install it with ease, and then it will integrate in Dreamweaver as if it was pre-installed. I've been looking for an option to do php uploading, and found this extension. I haven't bought it (yet), but it seems to be a very easy and usefull one. You can find more information at the website I gave.
ph
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Mar 13, 2003 2:14 am

Postby Ryyo » Wed Mar 19, 2003 1:41 pm

Ooh, that is tempting... but I sort of want to understand what is going on in the php for the future. I'm going to try sleepingdanny's code right now...

Still doesnt seem to have helped... the form seems to work okay, but when I go to upload it just ends up on a blank page.

Perplexing...

Ryyo
Ryyo
New php-forum User
New php-forum User
 
Posts: 4
Joined: Sat Mar 15, 2003 6:55 pm

Postby sleepingdanny » Thu Mar 20, 2003 2:59 am

Are you talking about the code that I'v writed :?:
By the way the form and the code has to be in the same page...
Do this :arrow:

<form>
.....
</form>
<?
....
?>
sleepingdanny
New php-forum User
New php-forum User
 
Posts: 13
Joined: Mon Mar 17, 2003 6:38 am


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron