Jump to content

uploading a file to database


jeff5656

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/178483-uploading-a-file-to-database/
Share on other sites

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.

$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.

 

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!

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.