Page 1 of 2

Problems with File Upload

Posted: Wed May 02, 2007 9:20 am
by metal
JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am currently building a website as a project.  The web site needs to upload files to a database. I am using blobs to create my code.  The code enters all the information about the file eg, name, file type, etc but it does not physically upload the file.  The code I have entered is displayed below. I would assistance if anyone could shed a light on what I am doing wrong

Code: Select all

<?php
		include("connection.php");	
		$connection = connect();
	
	$strDesc = $_POST['strDesc'];
	$fileUpload = $_POST['fileUpload'];	
	$fileUpload_name = $_FILES['fileUpload'] ['name'];
	$fileUpload_size = $_FILES['fileUpload'] ['size'];
	$fileUpload_type = $_FILES['fileUpload'] ['type'];;
	
	// Make sure both a description and
	// file have been entered

if(empty($strDesc) || $fileUpload == "none")  
die("You must enter both a description and file");

fopen($fileUpload, "r"); 
//$file_contents = fread($fp, filesize($postfile)); 
$file_contents = file_get_contents($fileUpload); 
//fclose($fp); 



$dbQuery = "INSERT INTO myBlobs VALUES ";
$dbQuery .= "(0, '$strDesc', '$file_contents', '$fileUpload_type')";


	mysql_query($dbQuery) or die("Couldn't add file to database");
	
echo "<h1>File Uploaded</h1>";
echo "The details of the uploaded file are shown below:<br><br>";
echo "<b>File name:</b> $fileUpload_name <br>";
echo "<b>File type:</b> $fileUpload_type <br>";
echo "<b>File size:</b> $fileUpload_size <br>";
echo "<a href='getfiles.php'>Add Another File</a>";

?>
JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed May 02, 2007 9:41 am
by feyd
$_POST['fileUpload'] won't exist.

Also, is there a particular reason you're wishing to store binary file data in a database as opposed to the file system, where they were intended to be stored? Something to be aware of: the type information found in $_FILES can easily be falsified.

Posted: Wed May 02, 2007 11:05 am
by arturm
you can find your file after upload in $_FILES['fileUpload']['tmp'] variable not $_POST

Posted: Wed May 02, 2007 11:16 am
by feyd
$_FILES['fileUpload']['tmp_name'] actually.

Posted: Wed May 02, 2007 11:18 am
by arturm
You are right feyd. My mistake.

Posted: Wed May 02, 2007 1:25 pm
by staar2
One question more. Is it better to store pictures in database or in file system ?

Posted: Wed May 02, 2007 1:31 pm
by arturm
I always put them in file system but if you want to store just few small pictures database is fine.
With more than just a few it is better to store them in the file system.
You have much faster access to them and you don't have to use PHP and MySQL every time you display them.

File Upload problems

Posted: Thu May 03, 2007 2:53 am
by metal
I was adviced to use blobs as it is written reports that i want to store in my database.

I have got the coding

$strDesc = $_POST['strDesc'];
$fileUpload = $_POST['fileUpload'];

from the form that I get the information from the user from. That is why I have POST it through, is that incorrect should I use FILE instead? I am fairly new to PHP so I appreciate any advice.

Posted: Thu May 03, 2007 4:41 am
by arturm
You should use $_FILE in read file function to read uploaded file content

Code: Select all

//fopen($fileUpload, "r"); you don't need this line
$file_contents = file_get_contents($_FILE['fileUpload']['tmp_name']);

Upload Files Problem

Posted: Thu May 03, 2007 5:36 am
by metal
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have changed this section of the PHP to what is shown below and it comes up with a message saying "Couldn't add file to database"

Code: Select all

$file_contents = file_get_contents($_FILE['fileUpload']['tmp_name']); 
//fclose($fp); 

$dbQuery = "INSERT INTO myBlobs VALUES "; 
$dbQuery .= "(0, '$strDesc', '$file_contents', '$fileUpload_type')";

mysql_query($dbQuery) or die("Couldn't add file to database");

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu May 03, 2007 6:59 am
by feyd
$_FILES, not $_FILE.. big difference.

Posted: Thu May 03, 2007 7:22 am
by arturm
you are right feyd but he still has an error in the query.

metal can I see myBlobs table ?

My blobs table

Posted: Thu May 03, 2007 7:42 am
by metal
The contents of myblobs is shown below

myblobs
Table comments: InnoDB free: 4096 kB


Field Type Null Default
blobId int(11) No
blobTitle varchar(50) No
blobData longblob No
blobType varchar(50) No


Indexes:
Keyname Type Cardinality Field
PRIMARY PRIMARY 7 blobId

Cheers metal

Posted: Thu May 03, 2007 7:44 am
by arturm
is your blobId column auto_increment ???

my blobs file

Posted: Thu May 03, 2007 7:44 am
by metal
Would it be that it is myblobs in the database but myBlobs in the coding?? I have just noticed this as I sent that message

Cheers metal