Problems with File Upload

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

metal
Forum Newbie
Posts: 10
Joined: Wed May 02, 2007 9:07 am
Location: UK

Problems with File Upload

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

you can find your file after upload in $_FILES['fileUpload']['tmp'] variable not $_POST
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_FILES['fileUpload']['tmp_name'] actually.
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

You are right feyd. My mistake.
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Post by staar2 »

One question more. Is it better to store pictures in database or in file system ?
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post 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.
metal
Forum Newbie
Posts: 10
Joined: Wed May 02, 2007 9:07 am
Location: UK

File Upload problems

Post 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.
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post 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']);
metal
Forum Newbie
Posts: 10
Joined: Wed May 02, 2007 9:07 am
Location: UK

Upload Files Problem

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_FILES, not $_FILE.. big difference.
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

you are right feyd but he still has an error in the query.

metal can I see myBlobs table ?
metal
Forum Newbie
Posts: 10
Joined: Wed May 02, 2007 9:07 am
Location: UK

My blobs table

Post 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
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

is your blobId column auto_increment ???
metal
Forum Newbie
Posts: 10
Joined: Wed May 02, 2007 9:07 am
Location: UK

my blobs file

Post 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
Post Reply