jeff5656 Posted October 21, 2009 Share Posted October 21, 2009 I am trying to make a page to upload a binary file to a database. Here's the error I get: Warning: fread(): supplied argument is not a valid stream resource in /home/content/j/e/f/html/education/add.php on line 5 Here's the add.php <?php include ("../connectdb.php"); $binFile= $_POST['binFile']; $data = addslashes(fread(fopen($binFile, "r"), filesize($binFile))); $txtDescription = mysql_real_escape_string($_POST['txtDescription']); $strDescription = addslashes(nl2br($txtDescription)); if (isset($binFile) && $binFile != "none") { $sql = "INSERT INTO tbl_Files "; $sql .= "(description, bin_data, filename, filesize, filetype) "; $sql .= "VALUES ('$strDescription', '$data', '$binFile_name', '$binFile_size', '$binFile_type')"; $result = mysql_query($sql); } ?> My form is in a separate file:: <FORM METHOD="post" ACTION="add.php" ENCTYPE="multipart/form-data"> <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="10000000"> <INPUT TYPE="hidden" NAME="action" VALUE="upload"> <TABLE BORDER="1"> <TR> <TD>Description: </TD> <TD><TEXTAREA NAME="txtDescription" ROWS="10" COLS="50"></TEXTAREA></TD> </TR> <TR> <TD>File: </TD> <TD><INPUT TYPE="file" NAME="binFile"></TD> </TR> <TR> <TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload"></TD> </TR> </TABLE> </FORM> Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/178483-uploading-a-file-to-database/ Share on other sites More sharing options...
MadTechie Posted October 21, 2009 Share Posted October 21, 2009 You sure you mean $binFile= $_POST['binFile']; and not $binFile= $_FILES['binFile']['tmp_name']; Quote Link to comment https://forums.phpfreaks.com/topic/178483-uploading-a-file-to-database/#findComment-941200 Share on other sites More sharing options...
Bricktop Posted October 21, 2009 Share Posted October 21, 2009 Hi jeff5656, You can't use $_POST for a file upload, you must use the $_FILES associative array. Have a look at the PHP manual for examples but something along the lines of: $binFile= basename($_FILES['binFile']['tmp_name']; Is what you need. You could then remove the next line down, the one that tries to get the filesize and use the $_FILES associative array for this also, i.e.: $data = $_FILES['binFile']['size'] Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/178483-uploading-a-file-to-database/#findComment-941201 Share on other sites More sharing options...
MadTechie Posted October 21, 2009 Share Posted October 21, 2009 $binFile= basename($_FILES['userfile']['name']; Is what you need. ~Coughs~ I don't think getting the original name of the file is going to help $_FILES['binFile']['tmp_name'] is for the temporary filename of the file in which the uploaded file was stored on the server. Quote Link to comment https://forums.phpfreaks.com/topic/178483-uploading-a-file-to-database/#findComment-941205 Share on other sites More sharing options...
MadTechie Posted October 21, 2009 Share Posted October 21, 2009 @Bricktop why copy my post (shame I missed the _name part) but at least i corrected it Quote Link to comment https://forums.phpfreaks.com/topic/178483-uploading-a-file-to-database/#findComment-941208 Share on other sites More sharing options...
Bricktop Posted October 21, 2009 Share Posted October 21, 2009 I'd typed quite a sizable response and when I pressed "Submit" it said you had posted also. After reading your response I thought my post had some additional information that may be pertinant so decided to go ahead and post. I didn't "copy" your post at all. Anyway, what's wrong with trying to help? Because that's all I was trying to do! Quote Link to comment https://forums.phpfreaks.com/topic/178483-uploading-a-file-to-database/#findComment-941213 Share on other sites More sharing options...
MadTechie Posted October 21, 2009 Share Posted October 21, 2009 I didn't "copy" your post at all. Well considing you said use $binFile= basename($_FILES['userfile']['name']; then changed it to $binFile= basename($_FILES['binFile']['tmp']; which was exactly the same as mine (note that userfile changed to binFile and use of tmp ) at which time i was updating mine to from tmp to tmp_name it seams strange you now have updated again to tmp_name.. « Last Edit: Today at 15:38:47 by Bricktop » this looks like a copy and paste to me Quote Link to comment https://forums.phpfreaks.com/topic/178483-uploading-a-file-to-database/#findComment-941230 Share on other sites More sharing options...
Bricktop Posted October 21, 2009 Share Posted October 21, 2009 Oh yes, that bit I copied, but I meant to only grab the binFile text, too lazy to type it! Oh well, at least the OP has an answer. Quote Link to comment https://forums.phpfreaks.com/topic/178483-uploading-a-file-to-database/#findComment-941288 Share on other sites More sharing options...
MadTechie Posted October 21, 2009 Share Posted October 21, 2009 Well if we got it wrong we're going to look like idiots.. but i think we're fine Quote Link to comment https://forums.phpfreaks.com/topic/178483-uploading-a-file-to-database/#findComment-941310 Share on other sites More sharing options...
Bricktop Posted October 21, 2009 Share Posted October 21, 2009 lol, true. Quote Link to comment https://forums.phpfreaks.com/topic/178483-uploading-a-file-to-database/#findComment-941347 Share on other sites More sharing options...
jeff5656 Posted October 21, 2009 Author Share Posted October 21, 2009 Thanks everyone. Based on this feedback I went and re-did the whole thing. Because it is totally different, and I have a new problem, I will post a new thread. (It works but file uploaded is always the same) Quote Link to comment https://forums.phpfreaks.com/topic/178483-uploading-a-file-to-database/#findComment-941537 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.