--File Upload via a Form--
I don't know whether this is going to assist your particular problem. I have been working on a file upload problem for the past couple of weeks with no success - until now. I found I had several problems rolled into one so I thought I'd itemise my problems
Hope you find this useful:
Ensure you have the latest Apache version and PHP version.
Ensure you have copied the php.ini-dist (from the source directory) to the configuration file location. Default is /usr/local/lib/php.ini, however if you configured php with '--with-config-file-path=/usr/local/apache/conf', then copy the php.ini file to this location. Config location can be found by running '<?php phpinfo(); ?>' within HTML code.
Within the above php.ini, you may want to specify the temporary directory (where the file is initially uploaded to). By default, it should be set to 'upload_tmp_dir = /tmp' or remarked out with ';'.
Ensure that the user running apache is the owner of the destination directory. e.g 'chown nobody.nobody /home/httpd/html/image_upload'
where 'image_upload' is the directory I wish the file to reside.
Upload Form:
a) give the form a name (extremely important because of form upload problems with Microsoft Internet Explorer (MSIE))
b) encoding type (again extremely important)
c) method is post
e.g
<form name="formname" enctype="multipart/form-data" action="$PHP_SELF" method="post">
<input type="hidden" name="actionflag" value="done">
<input type="file" name="image" size="30" enctype="multipart/form-data">
<input type="submit" name="submit" value="Upload">
Process Upload [copy files]:
Either follow Step 6 or Step 7 - not both.
$imagefile = "test.jpg"
$copy ($image, "/home/httpd/html/images/$imagefile") or die ("Didn't work");
print "Image: $image, Image File: $imagefile";
The above code will upload the file you submitted into '/home/httpd/html/images/test.jpg'. Variable '$image' from form submitted.
NOTE: use fully distinguished path ie root through to file. This will fix IE problems with uploading. Netscape works IE doesn't with relative pathing.
- Process Upload [into database]:
You may either copy your submitted file into the file system or into a database. Either step 6 or step 7 of these instructions.
$imagedata = addslashes(fread(fopen($image,"r"), filesize($image)));
if (isset($image_type) && $image_type <> "") { $imagetype = $image_type; }
if (!isset($imagetype)) $imagetype = "";
$result = mysql_query("INSERT INTO tbl_images (imagedata, imagetype)
VALUES('$imagedata','$imagetype')");
In the above, $image is from the submit (step 5). The above line beginning with '$imagedata =' copies the uploaded file from the php temporary directory ($image) to a variable ($imagedata). $image_type and $image_name come from the form submission. $image_type contains the MIME type of the submission. $image_name contains the actual name of the submitted file.
NOTE: $image_name - the 'image' part of the variable is infact from the <input name="image" ... part of the submission form.
- Display File from Database:
If you chose to copy the uploaded file into the database (following step 7 instead of step 6), see below.
--------------------
imageload.php:
<?php
include('open_db.x'); // Opens database.
$result = mysql_query("SELECT * FROM tbl_images WHERE imageid = '$imageid'");
$item_row = mysql_fetch_object ($result);
Header( "Content-type: $item_row->imagetype");
echo $item_row->imagedata;
mysql_close ($link); // Close database.
?>
displayimages.php:
<img src="imageload.php?imageid=22">
The top file is used to extract the image from the database. The 'displayimages.php' is the code you put in your normal code.
I don't claim to be an expert on the subject, however, I hope it gives you an idea of where you're having your problems.
Brett Scott
http://www.westaussie.com
Perth's Restaurant and Nightclub Search Engine - Perth, Western Australia