Jump to content

How to save filenames into a database


Jzzt

Recommended Posts

Hi,

first of all I'm dutch so if my english isn't perfect I apologize.
Second of all I haven't got much experience with php so my question is probably (and hopefully) easy to answer.

Here's the case:

in my website I want the webmaster to easily upload pictures and add a discription to them.
I have a script that uploads pictures to my webserver and it works. You can see the code below:

<form name="form1" method="post" action="" enctype="multipart/form-data">
<input type="file" name="imagefile">
<br>
<input type="submit" name="Submit" value="Submit">

<?
if(isset($_POST['Submit'])) {
//If the Submitbutton was pressed do:

if ($_FILES['imagefile']['type']=="image/pjpeg" || $_FILES['imagefile']['type']=="image/gif"){

// MAKE SURE THERE EXIST A FOLDER NAMED 'files' IN THIS PAGE'S CURRENT DIRECTORY
// Actually you don't have to supply a folder, it will upload the files to current directory
// but for easier and safer file handling, it will be wise to set a separate directory for uploaded files
// and this is just an example, you can set '../File/' so that it will jump to another directory altogether.
// For Windows users, there will be no problem, but for unix-based users, CHMOD 777 to the upload folder!

$imglink = "files/".$_FILES['imagefile']['name'];

copy ($_FILES['imagefile']['tmp_name'], $imglink)
or die ("Could not copy");

echo "<br>Name: ".$_FILES['imagefile']['name'].""; // the file name+extension
echo "<br>Size: ".$_FILES['imagefile']['size']."bytes"; // the size in bytes
echo "<br>Type: ".$_FILES['imagefile']['type'].""; // the file type
echo "<br><img src=".$imglink." height=50 width=50><br>"; // view the uploaded file/image with fixed height and width
}


else {
echo "<br><br>";
echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")<br>";
}
}


?></form>

Now all I need to do is write the filename to a database. I would like to use a ms access database.
Can anyone help me please??

Thnx a lot!

Jzzt
Link to comment
Share on other sites

I am unfamilliar with PHP->MS Access database solutions, but I am able to show you how to access, store, and read from MySQL, PostgreSQL, MSSQL databases using php.

First thing you need to do is create a connection to the database:

MYSQL:
$link = @mysql_connect( DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD ) ;

MSSQL:
$link = @mssql_connect( DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD ) ;


POSTGRESQL:
$link = pg_connect( "host=".DB_SERVER." port=".DB_SERVER_PORT." dbname=".DB_NAME." user=".DB_SERVER_USERNAME." password=".DB_SERVER_PASSWORD ) ;
Once you have a successful connection to the database, you can start giving SQL queries to it. For example:

$sql = "SELECT * FROM uploaded_files WHERE filename='%abc%' ;
$resource = mysql_query( $sql, $link ) ;
$result_array = @mysql_fetch_array( $resource ) ;

Now, to add a record to a database table you would use a query statement like:
$sql = "INSERT INTO uploaded_files VALUES( NULL, '".$file_name."', '".file_size."', '".file_description."'" ;
$resource = mysql_query( $sql, $link ) ;

If you would like to update a record you would use:
$sql = "UPDATE uploaded_files SET file_name='".$file_name."', file_size='".$file_size."', file_description='".$file_description."' WHERE file_id=5" ;
$resource = mysql_query( $sql, $link ) ;

There is a lot more to PHP/Database routines, so I suggest you read www.php.net and any other PHP/MySQL, PHP/MSSQL, or PHP/PostgreSQL resource you can find on the Internet (a google search does wonders).

If you would rather go the MS Access route I apologize for be unable to assist you.

Best Regards,

ProdyneTech

[!--quoteo(post=371391:date=May 4 2006, 06:41 PM:name=Jzzt)--][div class=\'quotetop\']QUOTE(Jzzt @ May 4 2006, 06:41 PM) [snapback]371391[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi,

first of all I'm dutch so if my english isn't perfect I apologize.
Second of all I haven't got much experience with php so my question is probably (and hopefully) easy to answer.
Now all I need to do is write the filename to a database. I would like to use a ms access database.
Can anyone help me please??

Thnx a lot!

Jzzt
[/quote]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.