Jump to content

File Upload & PHP


This topic is 7133 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Here is an example of how to do file uploads and access an FM database with PHP.

First the form:

<body>

<form action="uploadtest.php" method="post" enctype="multipart/form-data">

<input type="file" name="myfile">

<input type="submit" value="Upload">

</form>

</body>

</html>


Second the PHP file:
<body>

<?php

$file_name = $_FILES['myfile']['name'];

move_uploaded_file($_FILES['myfile']['tmp_name'], "/Volumes/Data/" . $_FILES['myfile']['name']);

readfile ("http://10.0.1.2:1154/FMPro?-db=combotest.fp5&-lay=web&-format=hworld.html&name=" . $file_name . "&-new");

?>

File Uploaded

</body>

</html>


The 'hworld.html' format file can contain whatever you like.

Mac OS X comes with Apache and PHP.

Hope this is of interest.

Garry

Link to comment
Share on other sites

OK, this one got my attention. I am running a filemaker database on a iMAX running OSX. I am also serving up the pages to the web. I have been wanting to add the ability to have cliients upload files via a browser interface.

You said OSX comes with apache and php. I was not aware of this. I did some searches and understood that you had to install/activate php on a osx machine and that it was a bit of a sticky process? Same with Apache?

LR

Link to comment
Share on other sites

The 'Web Sharing' of OS X is Apache. For PHP you have to edit the httpd.conf file to un-comment the lines that refer to PHP.

You place the *.php files in the 'Documents' folder of the 'Web Server' folder. Hey presto that's it.

Notes on how to edit the httpd.conf file, to enable PHP, can be found in the Apple Tech Article ID 106485 Also, I can write some notes on how to edit this file if the need arises.

Hope this helps.

Garry

Link to comment
Share on other sites

This can also be done eaily in perl. The form gary uses would be the same except that the action would be the perl script instead.

The script uses the perl module CGI.pm. Here is sample code

#! /usr/bin/perl

use CGI qw/:standard/;

####################

#Global Variables

####################

$officeUsername ='test';

$officePassword = 'upload';

####################

print header,

start_html(-title=>'File Upload',

-bgcolor=>'#ffffff'),

h2('File Upload');

print_form() unless param; #print form if not filled out

print_results() if param; #process if form filled in

print end_html;

exit;

sub print_form {

print start_multipart_form,

"

Username:

",

textfield(-name=>'username', -size=>10),br,

"

",

"

Password:",

textfield(-name=>'password', -size=>10),

"

",

"

File to upload:",

filefield(-name=>'upload',-size=>20),

"

",

submit(-label=>'Upload File'),

end_form;

}

sub print_results {

my $length;

my $username = param('username');

my $password = param('password');

my $file = param('upload');

if(!$file && $username != $officeUsername && $password != $officePassword) {

print "No file uploaded. Username/password combination incorrect or no file chosen. Please try again.";

return;

}

#upload and save file

open (SAVE,">./saved_file.txt") || die $!;

while (<$file>) {

print SAVE $_;

}

close SAVE;

print h2('File: ',$file,' --Uploaded Successfully');

}

Link to comment
Share on other sites

  • 3 weeks later...

Hi spcalia

I've been trying to use your CGI script, but it doesn't seem to be completing...

Any tips? All I'm getting is a white page that says "File Upload" from the first print statements.

Do you think my CGI handler is having trouble?

My Web Server is a OS X Server v1.2 (the old one) running Tenon's iTools 5

*These are the specifics:

Apache/1.3.19 (iTools/MacOSXS 1.2) mod_ssl/2.7.1 OpenSSL/0.9.5a mod_perl/1.24 mod_fastcgi/2.2.8

-Arin

Link to comment
Share on other sites

  • 2 years later...

I was going over your php code for uploading a file into a container field. I was wondering if you could give me some help on your php script...

I was interested mostly in the readfile line... what is each parameter for, exactly? You mention the hworld.html file; what is it for, exactly? I can make sense of most of the rest... first is the location of the database, then the name of the database; the name is gotten from the form... but what about the rest? The lay and format? I'm not familiar with readfile, so i'm not sure how it works exactly, or how to make a format file. I would appreciate any help, thanks.

Also, where do you declare the variable names for the $_FILE array, and the move_uploaded_file function call?

Feel free to e-mail me. Thanks,

-Clinton

Link to comment
Share on other sites

The parameters of the readfile() are CDML tags (Custom Web Publishing). For example, "-lay" is the FM Layout (not really needed), "-format" is the FM Format file that is returned and would usually contain the CDML replacement tags.

The "$_FILE" is a built-in php array that is returned after the Form is uploaded.

The "move_uploaded_file()" is a built-in php function.

Note that this php script does not insert a file into a Container field. It places the file external to the FM database.

Good Luck.

Garry

Link to comment
Share on other sites

This topic is 7133 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.