show me your codes for upload
Uploading files into a MySQL database using PHP
Collapse
X
-
Oki here's my code.
Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <FORM METHOD="post" ACTION="add_file.php" ENCTYPE="multipart/form-data"> <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="1000000"> <INPUT TYPE="hidden" NAME="action" VALUE="Upload"> <TABLE BORDER="1" cellspacing="1" cellpadding="3"> <TR> <TD>Subject: </TD> <TD><input name="subjects" size = "66%" /></TD> </TR> <TR> <TD>Client Name: </TD> <TD><input name="clientname" size = "35%" id="clientname" /></TD> </TR> <TR> <TD>File: </TD> <TD><INPUT type="file" NAME="uploaded_file"></TD> </TR> <TR> <TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload"></TD> </TR> </TABLE> </FORM> <?php # Check if a file has been uploaded if(isset($_FILES['uploaded_file'])) { # Make sure the file was sent without errors if($_FILES['uploaded_file']['error'] == 0) { # Connect to the database $dbLink = mysql_connect("localhost", "root", "777") or die (mysql_error()); mysql_select_db("filestorage", $dbLink) or die(mysql_error()); /*if(mysql_connect()) { die("MySQL connection failed: ". mysql_error()); }*/ # Gather all required data $name = mysql_real_escape_string($_FILES['uploaded_file']['name']); $mime = mysql_real_escape_string($_FILES['uploaded_file']['type']); $size = $_FILES['uploaded_file']['size']; $data = mysql_real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name'])); # Create the SQL query $query = " INSERT INTO file_upload ( FileName, FileMime, FileSize, FileData, subjects, clientname, Created ) VALUES ( '{$name}', '{$mime}', {$size}, '{$data}', '{$subjects}','{$clientname}', NOW() )"; # Execute the query $result = mysql_query($query, $dbLink); # Check if it was successfull if($result) { echo "Success! Your file was successfully added!"; } else { echo "Error! Failed to insert the file"; echo "<pre>". mysql_error($dbLink) ."</pre>"; } } else { echo "Error! An error accured while the file was being uploaded. Error code: ". $_FILES['uploaded_file']['error']; } # Close the mysql connection mysql_close($dbLink); } # Echo a link back to the mail page echo "<p><a href='list.php'>Click here to see all Files.</a></p>"; ?> </body> </html>
Comment
-
Error! An error accured while the file was being uploaded. Error code: 2
Notice: Undefined variable: dbLink in c:\Inetpub\wwwr oot\uploadingfi les\add_file.ph p on line 86
Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in c:\Inetpub\wwwr oot\uploadingfi les\add_file.ph p on line 86Comment
-
The problem there is explained here. (Error code #2).
Your form has a MAX_FILESIZE field, who's value is to low to upload your files:
Code:<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Also, the error on line #86 has nothing to do with the fact you are using the old mysql extension.
It's because you open your database connection inside the if statement, so when the upload fails it skips it and goes right to the else clause without ever opening it, where it tries to close it, obviously failing.
Edit,
No, on second glance, the mysql_close call is outside the if/else block altogether.
Move it inside the if statement and that error is fixed. (The one on line #86, anyways)Comment
-
Atli was right, I move the connection of database outside the if statement and the error regrading mysql_close(); was gone.
In max_file_size.. I put 1 000 000. And pdf file is already working, but some pdf file didn't work. Maybe because they are a too large file. Is 1000000, the maximum file size I can put?Comment
Comment