Jump to content

[SOLVED] select file via form and insert into database


roonnyy

Recommended Posts

Hi guys!

I have got small problem. First of all what I want to do: select file then click upload button and save this file to database. I have got script that works (almost  ;D ).

My problem is that i can't make this script working with upload form. If someone could help me I have be glad

 

he is scirpt:

<? 
if (isset($_POST['submit'])) 
{ 
include("pg.php"); //script with database connection and others commands for file insert
$filetosave=($_FILES['userfile']); // file to save in the database 
$loId = addImage($conn,$filetosave); 
$id_Desc = $filetosave; 
$fsize=filesize("$filetosave"); 
$sql = "INSERT INTO file(name,image,filesize) VALUES('$id_Desc','$loId','$fsize')"; 
pg_query($sql); 
}
?>

<form enctype="multipart/form-data" action="<?php echo $PHP_SELF; ?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>

the problem is here: $filetosave=($_FILES['userfile']);

$_FILES['userfile'] is a reference to aspects of the file uploaded

 

if you need just the file name then use

$filetosave=$_FILES['userfile']['name']

 

 

If you eed to bring in the contents of the file

 


//read image from tmp file
$ffile = fopen($_FILES['userfile']['tmp_name'],"rb");	
$contents = fread($ffile,$_FILES['userfile']['size']);
fclose($ffile);

 

$contents variable no holds the contents of the file

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.