Page 1 of 1

Adding Files for ftp upload to a php form

Posted: Fri Jun 17, 2005 3:39 pm
by thx967
Does anyone know of a way to add the option for a "image" or "file" upload to ftp server using PHP? In this case it would be for images that are on my site that users could change. Not looking to import them into a database though. I'm familiar with form postings - just giving a user the option to add a file.

Posted: Fri Jun 17, 2005 4:36 pm
by dnathe4th
you could have the PHP script check the filename extention, and therefor only accept certain file types

Posted: Fri Jun 17, 2005 4:48 pm
by Chris Corbyn
Use...

Code: Select all

<input type=&quote;file&quote; name=&quote;field_name&quote; />
and read it on the server

Code: Select all

print_r($_FILES);
The $_FILES array is multi-dimensional and you need to access the actual file via $_FILES['field_name']['tmp_name'] ;)

For validation, checking extensions is not a good method... it's too easy to forge. If you're checking for images then use getimagesize() ;)

Have a form now that uploads - But . . . . .

Posted: Fri Jun 17, 2005 7:38 pm
by thx967
I've constructed a form that seems to be working - no validation though.

However - I still have one hurdle.

I'm trying to upload the file to one of my other websites on a completely different server. I have ftp access - just can't figure out how to make the connection via php script so the upload goes there.

Any ideas??

Posted: Fri Jun 17, 2005 7:44 pm
by Chris Corbyn
Have you read the manual at php.net on the FTP functions?

If you have then I'll help you ;)

If not...

http://www.php.net/ftp

Thanks d11wtq

Posted: Fri Jun 17, 2005 10:27 pm
by thx967
I'll take a read then . . .

Posted: Fri Jun 17, 2005 11:21 pm
by thx967
OK

I've got my ftp server connection working

still not sure about the upload() and close() aspect

I currently have this as my form page

PHP

Code: Select all

<?
$ftp_server = &quote;ftp.anysite.com&quote;;
$ftp_user = &quote;user&quote;;
$ftp_pass = &quote;pass&quote;;

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die(&quote;Couldn't connect to $ftp_server&quote;);

// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
   echo &quote;Connected as $ftp_user@$ftp_server\n&quote;;
} else {
   echo &quote;Couldn't connect as $ftp_user\n&quote;;
}

// close the connection
ftp_close($conn_id);  

/*== upload directory where the file will be stored relative to where script is run ==*/
    
    $uploaddir = &quote;/UPLOAD_TEST&quote;;
    

    /*== get file extension (fn at bottom of script) ==*/
    /*== checks to see if image file, if not do not allow upload ==*/
    $pext = getFileExtension($imgfile_name);
    $pext = strtolower($pext);
    if (($pext != &quote;jpg&quote;)  && ($pext != &quote;jpeg&quote;))
    {
        print &quote;<h1>ERROR</h1>Image Extension Unknown.<br>&quote;;
        print &quote;<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>&quote;;
        print &quote;The file you uploaded had the following extension: $pext</p>\n&quote;;

        /*== delete uploaded file ==*/
        unlink($imgfile);
        exit();
    }

    
    if (is_uploaded_file($imgfile))
    {

       /*== move file to proper directory ==*/
       if (!copy($imgfile,&quote;$newfile&quote;))
       {
          /*== if an error occurs the file could not
               be written, read or possibly does not exist ==*/
          print &quote;Error Uploading File.&quote;;
          exit();
       }
     }

    /*== delete the temporary uploaded file ==*/
    unlink($imgfile);

    
    print(&quote;<img src=\&quote;$final_filename\&quote;>&quote;);


    /*== FUNCTIONS ==*/

    function getFileExtension($str) {

        $i = strrpos($str,&quote;.&quote;);
        if (!$i) { return &quote;&quote;; }

        $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);

        return $ext;

    }
?>
the form

Code: Select all

<form action=&quote;<?=$SCRIPT_NAME; ?>&quote; method=&quote;POST&quote; enctype=&quote;multipart/form-data&quote;>
    <input type=&quote;hidden&quote; name=&quote;MAX_FILE_SIZE&quote; value=&quote;50000&quote;>

    <p>Upload Image: <input type=&quote;file&quote; name=&quote;imgfile&quote;><br>
    <font size=&quote;1&quote;>Click browse to upload a local file</font><br>
    <br>
    <input type=&quote;submit&quote; value=&quote;Upload Image&quote;>
    </form>