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="e;file"e; name="e;field_name"e; />
and read it on the server
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 = "e;ftp.anysite.com"e;;
$ftp_user = "e;user"e;;
$ftp_pass = "e;pass"e;;
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("e;Couldn't connect to $ftp_server"e;);
// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
echo "e;Connected as $ftp_user@$ftp_server\n"e;;
} else {
echo "e;Couldn't connect as $ftp_user\n"e;;
}
// close the connection
ftp_close($conn_id);
/*== upload directory where the file will be stored relative to where script is run ==*/
$uploaddir = "e;/UPLOAD_TEST"e;;
/*== 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 != "e;jpg"e;) && ($pext != "e;jpeg"e;))
{
print "e;<h1>ERROR</h1>Image Extension Unknown.<br>"e;;
print "e;<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>"e;;
print "e;The file you uploaded had the following extension: $pext</p>\n"e;;
/*== delete uploaded file ==*/
unlink($imgfile);
exit();
}
if (is_uploaded_file($imgfile))
{
/*== move file to proper directory ==*/
if (!copy($imgfile,"e;$newfile"e;))
{
/*== if an error occurs the file could not
be written, read or possibly does not exist ==*/
print "e;Error Uploading File."e;;
exit();
}
}
/*== delete the temporary uploaded file ==*/
unlink($imgfile);
print("e;<img src=\"e;$final_filename\"e;>"e;);
/*== FUNCTIONS ==*/
function getFileExtension($str) {
$i = strrpos($str,"e;."e;);
if (!$i) { return "e;"e;; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
?>
the form
Code: Select all
<form action="e;<?=$SCRIPT_NAME; ?>"e; method="e;POST"e; enctype="e;multipart/form-data"e;>
<input type="e;hidden"e; name="e;MAX_FILE_SIZE"e; value="e;50000"e;>
<p>Upload Image: <input type="e;file"e; name="e;imgfile"e;><br>
<font size="e;1"e;>Click browse to upload a local file</font><br>
<br>
<input type="e;submit"e; value="e;Upload Image"e;>
</form>