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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ftp upload php/html form

Status
Not open for further replies.

partymong

Programmer
Nov 5, 2003
39
GB
Hi All,
I am new to php so any help will be appreciated.

I want multiple users to be able to upload a file using ftp via a html form. The file that is chosen(with the form), will then get ftped to the remote server using a php script

(I want to hard code the ftp server, user and password in the php script)

Thankyou in advance for your help!

Regards,

P
 
Code:
function uploadFile() {
        // import form data into local function
        global $ulFile, $ulFile_name, $ulFile_type;
        
        // check for form data
        if (isset($ulFile) && $ulFile_name) {
                // check uploaded file is one of supported types
                if ($ulFile_type == "image/png" ||
                                $ulFile_type == "image/jpeg" ||
                                $ulFile_type == "image/pjpeg") { 
                        
                        $fn = getUniqueName($ulFile_name); // generate a unique file name
        
                        /* move file to the photo directory. Notice we now use move_uploaded_file
                         * instead of copy. It includes a check to make sure the file came from
                         * the right source, making it more secure
                        */
                        if (move_uploaded_file($ulFile, PHOT_DIR."/$fn")) {
                                print &quot;<p>$ulFile_name successfully uploaded</p>\n&quot;;
                                saveThumbnail($fn); // generate tumbnail image
                        } else {
                                print &quot;<p>Oops! Couldn't upload $ulFile_name</p>\n&quot;;
                                unlink($ulFile); // remove temporary file
                        }
                } else {
                        print &quot;<p>Sorry, file type $ulFile_type not supported. Try a JPEG or PNG image.</p>\n&quot;;
                        unlink($ulFile); // remove temporary file
                }
        }
}
--------------------------------------------------------------------------------

and the form code 

code:--------------------------------------------------------------------------------
<?php
        /* album.php - version 3
         * .net magazine ([URL unfurl="true"]www.netmag.co.uk),[/URL] issue 83
         * Matt Kynaston, 2001
         * Distributed under the GNU Public License - [URL unfurl="true"]www.gnu.org/copyleft/gpl.html[/URL]
         *
         * The PHP Photo Album displays the contents of a thumbnail directory,
         * linked to full size images in the photo directory. It gives the user
         * the opportunity to upload their own photos to the album, automatically
         * creating tumbnails.
         *
         * Requires PHP4 with the GD and ZLIB extensions installed (php_gd.dll and php_zlib.dll
         * on Windows). These are available from the full download of PHP at [URL unfurl="true"]www.php.net[/URL]
         * Modify your php.ini file (C:\WINDOWS\PHP.INI in Windows systems) to point at them
        */
         
        /* constant declaration section
         * change these to modify the album directories or image sizes
        */
        define(&quot;NUM_COLS&quot;, 4); // number of columns in table
        define(&quot;PHOT_DIR&quot;, &quot;photos&quot;); // photo directory, relative to this page
        define(&quot;THUMB_DIR&quot;, &quot;thumbs&quot;); // thumbnail directory, relative to this page
        define(&quot;MAX_XY&quot;, 150); // maximum width or height of thumbnail image

        /* all the functions used on this page have been split off into a 
         * seperate file. The include statement below makes them available to this
         * page.
        */
        include(&quot;album_functions.php&quot;); 

?>
<html>
<head>
<title>Photo Album</title>
<link href=&quot;album.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<h1>PHP Photo Album</h1>
<h3>(final version)</h3>

<?php uploadFile() ?>

<form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;<?php print $PHP_SELF?>&quot; enctype=&quot;multipart/form-data&quot;>
  <p>Add your own image to the album:<br>
    <input type=&quot;file&quot; name=&quot;ulFile&quot;>
        <input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;512000&quot;>
  </p>
  <p>
    <input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Upload&quot;>
  </p>
</form>

<p> </p>

<?php makeAlbumTable(); ?>

</body>
</html>

Bastien

Any one have a techie job in Toronto, I need to work...being laid off sucks!
 
Hi Bastien,
Thanks for your help!...
I have got some code working, I have posted for anyone who enconters the same problem.



THE HTML FORM CODE:
===================

<HTML>
<HEAD>
<TITLE> PHP Upload </TITLE>
</HEAD>
<h1>Upload a file</h1>
<BODY>

<form method=&quot;post&quot; action=&quot;do_uploadFTP.php&quot; enctype=&quot;multipart/form-data&quot;>
<p><strong>File to Upload:</strong><br>
<input type=&quot;file&quot; name=&quot;file1&quot; size=40></p>
<p><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Upload File&quot;></p>

</BODY>
</HTML>


THE PHP SCRIPT CODE:
====================


<?
if ($_FILES[file1] != &quot;&quot;)
{
$ftpServer = &quot;myftpserver;
$ftpUser = &quot;myuser&quot;;
$ftpPass = &quot;mypassword&quot;;

$conn = @ftp_connect($ftpServer) or die(&quot;Couldn't connect to FTP server&quot;);
$login = @ftp_login($conn, $ftpUser, $ftpPass) or die (&quot;Login details failed !&quot;);
$putFile = @ftp_put($conn, $_FILES[file1][name],$_FILES[file1][tmp_name], FTP_BINARY);

ftp_quit($conn);
}
else
{
die (&quot;No input file specified.&quot;);
}
?>
<html>
<head>
<title>File Upload</title>
<body>
<?
if ($putFile)
{
echo &quot;File upload OK.&quot;;
}
else
{
echo &quot;File upload failed.&quot;;
}
?>

<form action=&quot;upload_form.html&quot;>
<p><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Back to upload form&quot;></p>

</body>
</head>
</html>

Regards,
P
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top