note 43830 added to function.move-uploaded-file
| From: | nchamberlain at improvetechnology dot com | Date: | Mon, 05 Jul 2004 17:39:05 +0000 |
| Subject: | note 43830 added to function.move-uploaded-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-72621@lists.php.net to get a copy of this message | ||
I use web hosting services (virtual hosting) that does not allow me to use move_uploaded_file
because the UIDs the different processes run under do not match the UIDs of the target directory I
want to move the file to. However, I can upload files using HTTP POST, and I have discovered a
workaround using the PHP ftp functions to get the file into the directory I want. Here is the full
source code I am using:
<?php
// ---------------------------------------------------------
function UploadFile( $srcFile, $destFile, $ftpHost, $ftpUser, $ftpPassword ) {
$conn_id = ftp_connect( $ftpHost );
if ( !$conn_id ) return( false );
$login_result = ftp_login($conn_id, $ftpUser, $ftpPassword );
if ( !$login_result ) return( false );
$upload = ftp_put( $conn_id, $destFile, $srcFile, FTP_BINARY );
if ( $upload ) $retVal = true;
else $retVal = false;
ftp_close($conn_id);
return( $retVal );
}
// ---------------------------------------------------------
if ( isset( $_POST['Submit'] )) {
$srcFile = $_FILES['thefile']['tmp_name'];
$destFile = '/xxx/' . $_FILES['thefile']['name'];
if ( $_FILES['thefile']['size'] > 0 ) {
if ( UploadFile( $srcFile, $destFile, 'hostname', 'username',
'password' ))
echo '<P>Upload and transfer successful</P>';
else
echo '<P>Upload and transfer failed</P>';
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</HEAD>
<BODY>
<FORM name="form1" method="post" action="f2.php"
enctype="multipart/form-data">
<P>
<INPUT type="file" name="thefile">
</P>
<P>
<INPUT type="submit" name="Submit" value="Submit">
</P>
</FORM>
</BODY>
</HTML>
The code above can use some additional error checking, but is a working solution for when
move_uploaded_file does not work.
----
Manual Page -- http://www.php.net/manual/en/function.move-uploaded-file.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43830
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43830&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43830&report=yes
Search -- http://master.php.net/manage/user-notes.php