I've tried to find on the internet a simple script wich upload images on the server-hard and to mysql the link to that picture, to be used later. Well, i've just finish this script and it works very well. you just have to read carefully were your connexion is made you will have all files including the mysql create db instructions.
Lets start!!!
- First at all we must to create a mysql database with name test
Go in your phpmyadmin account and just paste this in SQL window:
CREATE TABLE test (
user varchar(16) NOT NULL default '',
poza varchar(128) NOT NULL default ''
) TYPE=MyISAM;
This will create a table named TEST!!!
- Now you will have to create the form from were you will upload pictures to the web server. It works with POST method so...here's the code:
<form enctype="multipart/form-data" method="post" action="trimite.php">
user<br>
<input type="Text" name="user" size="25">
<br>
poza<br>
<input type="File" name="superdat" size="25">
<br><br>
<input type="submit" name="submit" value="Upload">
</form>
he will send values of user(text description) and superdat(picture file) to trimite.php (this file will upload and will do the rest of the job).
<?php
//here we will declare variables
$timestamp = date("Y.m.d-H.i.s");
$rest = substr ($superdat_name, -4);
// here we will verify if file uploaded is jpg or not
// if file is jpg
if ($rest == ".jpg" or $rest==".JPG")
{
// the will be copied on a folder named poze
copy ("$superdat", "poze/$superdat_name.$timestamp.jpg");}
else
{
//if file is not jpg user will receive an error
echo "Error! file is not jpg. for more help contact me at webmaster@marmota.ro";
exit;
}
// if file is jpg and on server does exist a folder with name poze
//here we will connect to the mysql server
mysql_connect("localhost", "user", "password");
// we will select the corect database
mysql_select_db('test');
//here will set up values to be inserted in mysql table
$sql="INSERT INTO test (user, poza) VALUES ('$user','poze/$superdat_name.$timestamp.jpg')";
$rezultat=mysql_query ($sql);
//we close the conection to mysql
mysql_close();
//we close the if statement
exit;
//we close the php
?>
- So now is settid up, you can stop here or you can create a new page from wich your pictures will be displayed...also you can use include 'conectare.php' command to eliminate command mysql_connect from file trimite.php and use your connexion file. here is an example of conectare.php source
<?php
//connection to the database
mysql_connect("localhost","user","password");
mysql_select_db("test");
?>
- now lets see how you can view files from your server...
you can copy this script in a page called random.php, this will show you random pictures from your server with height and wight = 300
<?
//use this if you didnt include a connexion file
$cnx = mysql_connect("localhost", "user", "password");
mysql_select_db("test", $cnx);
$sql = mysql_query("SELECT poza FROM test order by rand() limit 1")
or die (mysql_error());
while($row = mysql_fetch_row($sql)){
$poza=$row[0];
//show the picture and on click open the original picture file
echo '<a href="http://mail.curierulnational.ro/cnr/1/proba/'.$poza.'">';
echo '<img width=300 height=300 src="'.$poza.'">';
echo '</a>';
}
mysql_close($cnx);
?>
Im sorry that i cant upload rar or ace on this server to give you all script setted up.
SO THAT's all, i hope somebody will find this usefully and sorry for my english, if i can help you just contact me. cya.....
bye the way. be sure that folder "poze" has chmod setted up to 777 else you will cant upload pictures in that folder!!!!!!!!
:glare: