RE: [PHP] file uploads
| From: | Patrick Dunford | Date: | Wed, 06 Dec 2000 09:05:20 +0000 |
| Subject: | RE: [PHP] file uploads | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-28861@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Cesar Peregrina [mailto:cepe@telnor.com]
> Sent: Wednesday, 6 December 2000 09:44
> To: Oplinger, Shawn
> Cc: 'php-general@lists.php.net'
> Subject: Re: [PHP] file uploads
>
>
> What you need is to copy, move , etc. the uploaded file
> in the target page
> the code in http://www.php.net/manual/features.file-upload.php
> was very usefull to me.
>
> But i have another problem, How can i manage multiple uploads
By having multiple fields of type FILE in your form, with different names,
then you pick them up as they come in.
To answer the other one, when you click Submit, the file is uploaded onto
your server, then all you need do is invoke the Copy command passing it the
name of your File field and the new name, and it should all work.
I use this script that I downloaded from somewhere. Note it is customised
for my system. It is all in the one file, not separate files as other
scripts sometimes use.
<HTML>
<HEAD>
<TITLE>Upload Planner File</TITLE>
</HEAD>
<BODY BGCOLOR=FFFFFF>
<?php
$uploadDir = '/home/pdunford/planner/'; //Put your own destination
directory here
if
set($submit)){
if ($upload != 'none'){
$dest = $uploadDir . $upload_name;
if (@copy($upload, $dest)){
echo "Successfully uploaded $dest<BR>\n";
}
else{
echo "<FONT COLOR=FF0000><B>File Upload
Failed</B></FONT><BR>\n";
$perms = @fileperms($uploadDir);
$owner = @fileowner($uploadDir);
if (!$perms){
echo "Directory does not exist: $uploadDir<BR>\n";
}
else{
$myuid = getmyuid();
if (!($perms & 2) && !(($owner == $myuid) && ($perms
& 128))){
echo get_current_user(), " doesn't have permission to write in
$uploadDir<BR>\n";
}
}
}
}
else{
echo "<FONT COLOR=FF0000><B>File Upload
Failed</B></FONT><BR>\n";
echo "Filesize exceeds limit in FORM or php.ini<BR>\n";
}
}
?>
<FORM ENCTYPE=multipart/form-data ACTION=fileupload.php METHOD=POST>
<INPUT TYPE=HIDDEN NAME=MAX_FILE_SIZE VALUE=1000000>
Upload File: <INPUT NAME=upload size=50 TYPE=FILE><BR>
<INPUT TYPE=SUBMIT NAME=submit VALUE="Upload">
</FORM>
</BODY></HTML>
You can also use FTP to do uploads, the main advantage being security. Here is an FTP upload script
and form. This one changes the extension of the uploaded file to .shtml
<HTML>
<HEAD>
<TITLE>Upload Planner File</TITLE>
</HEAD>
<BODY BGCOLOR=FFFFFF>
<?php
// set up basic connection
if (isset($submit)){
if ($upload != 'none'){
$conn_id = ftp_connect("ftp.godzone.net.nz"); //Customise for your server
// login with username and password
$login_result = ftp_login($conn_id, $username, $password);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "<BR>Ftp connection has failed!";
echo "<BR>Attempted to connect to ftp.godzone.net.nz for user $username";
//Customise for your server
die;
}
else {
echo "<BR>Connected to ftp.godzone.net.nz for user $username"; //Customise
for your server
}
// upload the file
ftp_chdir($conn_id,'/planner/'); //Customise for your destination directory
$filenamesplit = explode(".", $upload_name);
$newfilename = $filenamesplit[0] . ".shtml"; //Change file extension to .shtml
$doupload = ftp_put($conn_id, $newfilename, $upload, FTP_ASCII);
// check upload status
if (!$doupload) {
echo "<BR>Ftp upload has failed!";
}
else {
echo "<BR>Uploaded $upload_name to ftp.godzone.net.nz as $newfilename";
//Customise for your server
}
// close the FTP stream
ftp_quit($conn_id);
}
}
?>
<FORM ENCTYPE=multipart/form-data ACTION=ftpupload.php METHOD=POST>
<INPUT TYPE=HIDDEN NAME=MAX_FILE_SIZE VALUE=1000000>
Upload File: <INPUT NAME=upload size=50 TYPE=FILE><BR>
Username: <INPUT NAME=username size=10 TYPE=Text><BR>
Password: <INPUT NAME=password size=10 TYPE=Password><BR>
<INPUT TYPE=SUBMIT NAME=submit VALUE="Upload">
</FORM>
</BODY></HTML>