OK, first of… I’m completely new to MySql and PHP!!
This is my “problem”… I need to let user upload files to a server, and when the file is uploaded onto the server i need to make an entry in the MySql DB with a link to the file that was just uploaded.
I’ve been looking around the internet for a couple of days now, and i’ve tried alot, but i can’t get it to work propperly. I’ve been able to split it up in two (2) parts, so first you upload the file, then you make the DB entry. But i would like to combine these two into ONE function.
This is the PHP i’ve got so far… (the one that doesn’t work. this is were i’ve tried to combine the to steps.)
STEP #1
<?php include(“dbconnect.php”)?>
<?php
$upload_dir = "../files/";
$size_bytes = 1024000;
$limit_file_type = "yes";
$allowed_file_type = array('application/pdf');
if (!is_dir("$upload_dir")) {
die ("Mappen ($upload_dir) finns inte!");
}
if (!is_writeable("$upload_dir")){
die ("Mappen ($upload_dir) går inte att skriva till, ändra Chmod till (777)");
}
if (is_uploaded_file($_FILES['filetoupload']['tmp_name']))
{
$size = $_FILES['filetoupload']['size'];
if ($size > $size_bytes)
{
echo "Filen är för stor, den får inte vara större än $size_bytes bytes.";
exit();
}
if (($limit_file_type == "yes") && (!in_array($_FILES['filetoupload']['type'],$allowed_file_type)))
{
echo"Du kan bara ladda upp PDF filer";
exit();
}
$filename = $_FILES['filetoupload']['name'];
if(file_exists($upload_dir.$filename)){
echo "Filnamnet $filename finns redan. Filens namn måste vara UNIKT.";
exit();
}
if (move_uploaded_file($_FILES['filetoupload']['tmp_name'],$upload_dir.$filename)) {
echo "Filen <a href=$upload_dir$filename class=r_link>$filename</a> laddades upp utan problem!
<p><a href=ars_sub_stp2.php class=r_link>Klicka här för att gå vidare</a>";
exit();
}
else
{
echo "Ett oväntat fel uppstod! Vänligen försök igen.";
exit();
}
}
?>
STEP #2
<?php
if(isset($_POST[‘submit’])) {
$file=$_POST[‘file’];
$year=$_POST[‘year’];
$sql = mysql_query(“INSERT INTO ars
(file
, year
) VALUES (’<a href=…/…/files/$filename target=_blank><img src=…/…/images/pdfICO.jpg width=95 height=48 border=0> get file from $year</a>’);”);
echo ‘File was enterd into DB’;
}
?>
I know, it’s probably all wrong… like i said… just started this MySql PHP stuff :azn:
Thx to anoyone who has/and want’s to take the time to look at/try to help with this problem.