Page 1 of 1

Multiple file uploading problem

Posted: Thu Jan 23, 2003 7:20 pm
by Shinmeiryu
I'm a newbie when it comes to file uploading. I understand the concept of uploading with one file via form to processing page, or at least I think I do.

I'm trying to work with multiple file uploads. I have it set on my form where you can change the amount you want to upload from 1-40. When I submit, the results come out as the variable posts out the word 'array' in the imagefile field according to what I checked in the database. But nothing is uploaded.

I was hoping it would upload the gathered array of files to a directory then post out all the filenames onto database, but I'm wondering if this is possible? Or will I have to use a field per upload (imagefile1, imagefile2, etc.) into my database, rather than the shortcut I'm trying of just one?

CREATE TABLE screenshots (
ID INT(10) UNSIGNED DEFAULT '1' NOT NULL AUTO_INCREMENT,
description TEXT NOT NULL,
imagefile varchar(255),
PRIMARY KEY (ID)
);

Code: Select all

<?php

for($i = 0; $_FILES['imagefile']['name'][$i] != ""; $i++)
		{
		// set directory where you will be uploading (CHMOD to 777) 
		$dir = "../screenshots";

			// if no file was uploaded
			if (($_FILES['imagefile']['name'][$i] == "")) {
			   echo "No file Uploaded. Press BACK button on your browser.";
			   exit;
			}
		
			// if a file with that name already exists on server
			if (file_exists("$dir/$imagefile_name")) {
			   echo "A file with that name already exists on server. Press BACK button on your browser.";
			} 
			else 
			{
				// upload file
		   		if (is_uploaded_file($imagefile)) {
		   	   	$imagefile_name = $_FILES['imagefile']['name'][$i];
				$size = $_FILES['imagefile']['size'][$i];
				$type = $_FILES['imagefile']['type'][$i];
				$ii = $i + 1;
		
				$size = $size / 1000;
				$size = round($size, 1);
			  	copy($_FILES['imagefile']['tmp_name'][$i], "$dir/$imagefile_name");
			  	echo "File Uploaded Successfully";
				}
		   	}
		
		}
		echo ("<table cellspacing='1' cellpadding='1' align='center'>\n");
        echo "<tr><td colspan='3' bgcolor='#868283'>File $ii</td></tr>";
        echo "<tr><td></td><td>File name:</td><td>$imagefile_name</td></tr>";
        echo "<tr><td></td><td>File size:</td><td>$size Kbytes</td></tr>";
        echo "<tr><td></td><td>File type:</td><td>$type</td></tr>";
	
	$query = "INSERT INTO screenshots VALUES (NULL,'$description','$imagefile')";
	$result = mysql_query($query) or die('error making query'); 


?>