Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.