Need help with upload Form!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrekko
Forum Newbie
Posts: 6
Joined: Fri Sep 24, 2010 6:37 am

Need help with upload Form!

Post by mrekko »

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:

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>";

}
?>
Thank you guys!
mrekko
Forum Newbie
Posts: 6
Joined: Fri Sep 24, 2010 6:37 am

Re: Need help with upload Form!

Post by mrekko »

I already created the directory 'upload' and has the 777 permission, in case you ask!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Need help with upload Form!

Post by John Cartwright »

1) Have you checked if an error is present in the $_FILES superglobal? See Handling File Upload Errors

2) Try echo'ing out $filePath prior to attempting to move the file there to see if it's what you expect.

3) Try performing a chmod() within the script itself, as different permissions settings on servers can cause that to fail (although I believe it should have worked if you set it to 777 which makes it globally writable --- and is highly discouraged).
Post Reply