Need help with upload Form!
Posted: Fri Sep 24, 2010 6:46 am
Hi guys,
I want to send to MySQL database a web form with 4 text fields and the path of a uploaded file.
I can“t get it done because no matter what i do it keep sending me the same error, "Error uploading file"
the code is the follow:
Thank you guys!
I want to send to MySQL database a web form with 4 text fields and the path of a uploaded file.
I can“t get it done because no matter what i do it keep sending me the same error, "Error uploading file"
the code is the follow:
Code: Select all
<?php
require_once 'auth.php';
$uploadDir = 'upload/';
if(isset($_POST['upload']))
{
//form details variables
$membro = $_SESSION['SESS_FIRST_NAME'];
$numeroarq = $_POST['numeroarq'];
$descricao = $_POST['descricao'];
$tipo = $_POST['tipo'];
$data = $_POST['data'];
//upload file variables
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$filePath = $uploadDir . $fileName;
$ext = substr(strrchr($fileName, "."), 1);
// make the random file name
$randName = md5(rand() * time());
// and now we have the unique file name for the upload file
$filePath = $uploadDir . $randName . '.' . $ext;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO documentos (id_membro, nr_arquivo, descricao, tipo, data, tamanho, path ) ".
"VALUES ( '$membro', '$numeroarq', '$descricao', '$tipo', '$data', '$fileSize', '$filePath')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
print "<p>File Name: <b>$fileName</b><br>";
print "<p>File Size: <b>$fileSize</b><br>";
print "<p>File Type: <b>$fileType </b><p>";
print "Para transferir outro ficheiro: <a href=http://www.andregarcia.comuf.com> Click Here</a>";
echo "<br>Files uploaded<br>";
}
?>