Jump to content

[SOLVED] PHP MySQL fread() Error


kneifelspy

Recommended Posts

Okay, I am trying to set up a form where you can upload a CD review with a picture of the CD.  Here is the form on reveiw.post.php

 

	<form action="review.publish.php" method="post">
<input type="text" name="cf_band" value="BAND NAME" /><br /><br />
<input type="text" name="cf_title" value="ALBUM TITLE" /><br /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<input type="file" name="cf_photo" /><br /><br />
<textarea cols="30" rows="5" wrap="soft" name="cf_body">BODY TEXT</textarea><br /><br />
<input type="text" name="cf_web" value="BAND WEBSITE" /><br /><br />
<input type="text" name="cf_author" value="REVIEW AUTHOR" /><br /><br />
<input type="text" name="cf_timestamp" value="<? echo $timestamp; ?>" /><br />
<input type="submit" value="Post" />
</form>

 

Here is the code to upload the data to the MySQL table, review.publish.php:

 

// ASSUME MY LOGIN STUFF IS CORRECT//

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);

$filetag = $_POST['cf_photo'];

$filename = $_FILES['filetag']['name'];
$filetype = $_FILES['filetag']['name'];
$filesize = $_FILES['filetag']['size'];
$tmp_filename = $_FILES['filetag']['tmp_name'];

$fp = fopen($tmp_filename, 'r');
$file_content = fread($fp, filesize($tmp_filename));
fclose($fp);

$cf_band = mysql_real_escape_string($_POST['cf_band']);
$cf_title = mysql_real_escape_string($_POST['cf_title']);
$cf_body = mysql_real_escape_string($_POST['cf_body']);
$cf_web = mysql_real_escape_string($_POST['cf_web']);
$cf_author = mysql_real_escape_string($_POST['cf_author']);
$cf_timestamp = mysql_real_escape_string($_POST['cf_timestamp']);

$query="INSERT INTO reviews VALUES ('','$cf_band', '$cf_title', '$file_content' '$cf_body', '$cf_web', '$cf_author', '$cf_timestamp')";
mysql_query($query);

echo '<b>Post Successfully Added!</b><br /><br />';

mysql_close();

 

Here are the errors I am getting:

fread(): supplied argument is not a valid stream resource

fclose(): supplied argument is not a valid stream resource

 

Can anyone help me get this running correctly!?

 

Link to comment
https://forums.phpfreaks.com/topic/140741-solved-php-mysql-fread-error/
Share on other sites

try this

 

instead of

$filetag = $_POST['cf_photo'];

$filename = $_FILES['filetag']['name'];
$filetype = $_FILES['filetag']['name'];
$filesize = $_FILES['filetag']['size'];
$tmp_filename = $_FILES['filetag']['tmp_name'];

 

use

 


$filename = $_FILES['cf_photo']['name'];
$filetype = $_FILES['cf_photo']['name'];
$filesize = $_FILES['cf_photo']['size'];
$tmp_filename = $_FILES['cf_photo']['tmp_name'];

You form tag must include enctype="multipart/form-data" for the $_FILES array to be populated.

 

Try:

<form enctype="multipart/form-data" action="review.publish.php" method="post">
<input type="text" name="cf_band" value="BAND NAME" /><br /><br />
<input type="text" name="cf_title" value="ALBUM TITLE" /><br /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<input type="file" name="cf_photo" /><br /><br />
<textarea cols="30" rows="5" wrap="soft" name="cf_body">BODY TEXT</textarea><br /><br />
<input type="text" name="cf_web" value="BAND WEBSITE" /><br /><br />
<input type="text" name="cf_author" value="REVIEW AUTHOR" /><br /><br />
<input type="text" name="cf_timestamp" value="<? echo $timestamp; ?>" /><br />
<input type="submit" value="Post" />
</form>

 

Ken

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.