Retrieving text from a file field?

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

Post Reply
User avatar
skylavelle
Forum Newbie
Posts: 18
Joined: Sun May 04, 2003 11:33 pm
Location: Brisbane, Australia

Retrieving text from a file field?

Post by skylavelle »

Hey, does anyone know how to retrieve text from a file field?

Basically, what I am trying to do in (1) upload an image and (2) take the file name and path that it is uploaded to and insert it into my MySQL database.

Cheers
Sky :D
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

personally I'd advise reversing that logic and naming the file based on an auto_inc db field return - basically, if two people uploaded an image called blat.jpg what would happen with your version? would the old be overwritten?

$q = mysql_query("INSERT INTO `images` VALUES ('' , $title, $dimensions, $size)");
$n = mysql_insert_id();
move_uploaded_file($_FILES['formfield']['tmp_name'], '../images/' .$n. '.jpg');

if `images` was
id - auto_increment primary
title - varchar(100)
dims - varchar(100)
size - mediumint(6)

the auto increment value of id would be returned to $n and then the image named when it gets moved from /tmp/ - no images would ever be overwritten.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

yea, adding on to what he wrote:

the $_FILES['variable'] superglobal is made for this.

You can research it more here:

http://www.php.net/manual/en/security.r ... lobals.php

also the sticky on this forum:

http://www.devnetwork.net/forums/viewtopic.php?t=511
Post Reply