Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help on script (image blob/file dir)

Status
Not open for further replies.

wolfandcrow

IS-IT--Management
Jan 14, 2007
3
GB
i have been at this for 4 hours every thing i read on google searchers does not work what am trying to do is add an image as a blob to mysql and a file to my dir and add the path to mysql but no matter what i try i can only get 1 of them to work at any 1 time?

html form

<form method="post" enctype="multipart/form-data" action="add.php">
<input type="file" name="userfile[]">
<input type="file" name="userfile[]">
<input type="submit" name="Submit">

and then my add.php





$postdate=date("d/m/y h:i:s"); //create date time
$uploadDir = '/file/';

if ($_FILES)
{
$image_types = Array ("image/bmp",
"image/jpeg",
"image/pjpeg",
"image/gif",
"image/x-png");
if (is_uploaded_file ($_FILES['userfile']['tmp_name'][1])) {
$imagefile = addslashes (fread
(fopen ($_FILES["userfile"]["tmp_name"][1], "r"),
filesize ($_FILES["userfile"]["tmp_name"][1])));
$image_name = $_FILES["userfile"]["name"][1];
$image_size = $_FILES["userfile"]["size"][1];
$image_type = $_FILES["userfile"]["type"][1];

$fileName = $_FILES['userfile']['name'][0];
$tmpName = $_FILES['userfile']['tmp_name'][0];
$fileSize = $_FILES['userfile']['size'][0];
$fileType = $_FILES['userfile']['type'][0];
$ext = substr(strrchr($fileName, "."), 1);
$randName = md5(rand() * time());
$filePath = $uploadDir . $randName . '.' . $ext;




if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}



if (in_array (strtolower ($image_type), $image_types)) {
$sql = "INSERT INTO add (title, addresses, video_dis, subby, displaysub, file_name, file_size, file_type, file_path image_type, image, image_size, image_name, image_date) ";
$sql.= "VALUES ('{$title}', '{$addresses}', '{$video_dis}', '{$subby}', '{$displaysub}', '{$fileName}', '{$fileSize}', '{$fileType}', '{$filePath}', '{$image_type}', '{$imagefile}', '{$image_size}', '{$image_name}', NOW())";


if ($result != true)
{


print "
<META HTTP-EQUIV='Refresh' CONTENT='3; URL=/index.php'>
worked


";
}
}
}
}


 
can i check what you're after? the user uploads a file and with that file you (i) store the file itself in the db (ii) store the file in the filesystem; and (iii) store in the database a pointer the file in the filesystem?

 
the script above is not letting me submit my image into my database and a file into my dir. It's not letting me do both from 1 form? How ever if i split the code up and only do 1 thing it works. But i need both uploads working? i have submitted on loads of coding forums and have not got any help as of yet.

Thanks
 
reducing the complexity of your code a bit, try the following:

Code:
<?
define ("PATH", "./"); 	// relative or absolute pathname for upload directory with trailing slash.
if (!empty($_FILES)):
	//file is uploaded (or at least attempted
	foreach ($_FILES["userfile"]["error"] as $key => $error):
	//taken in part from the php manual
		if ($error == UPLOAD_ERR_OK):
			$fname = fileSave(	$_FILES['userfile']["tmp_name"][$key], 
								$_FILES['userfile']['name'][$key], 
								PATH	);
			if ($fname != FALSE):
				if(databaseSave($fname) === FALSE):
					die ("problem uploading into database<br/>". mysql_error());
				endif;
			else:
				die ("problem saving file.  Check permissions for ". PATH);
			endif;
		else:
			die("Problems with upload");
		endif;
	endforeach;
endif;

function fileSave($tmpfilename, $realfilename, $path){
	if(is_uploaded_file($tmpfilename)):
		move_uploaded_file($tmpfilename, $path.$realfilename);
	else:
		return false;
	endif;
	return $path.$realfilename;
}

function databaseSave($realfilename) {
	$hostname = "";
	$username ="";
	$password = "";
	$dbname = "";
	$filestoragetable = "";
	mysql_connect($hostname, $username, $password) or die (mysql_error());
	mysql_select_db($dbname) or die (mysql_error());
	$fileContents = file_get_contents($realfilename);
	$query = "	Insert 
				into 
					$filestoragetable
				fileID = null,
				filePointer ='".mysql_real_escape_string($realfilename)."',
				fileBlob = '".mysql_real_escape_string($fileContents)."'";
	return mysql_query($query);				
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>File Upload Test</title>
</head>

<body>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>" enctype="multipart/form-data" >
<input type="file" name="userfile[]" /><br/>
<input type="file" name="userfile[]" /><br/>
<input type="submit" name="submit" value="Upload"/>
</form>
</body>
</html>
i've not tested the code but it should work ok
 
i was typing out the code to submit and i found the problem myself it's amazing how some thing so small can effect things

Code:
, missing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top