mysql insert image into database

tillabong

New Member
Messages
60
Reaction score
0
Points
0
hi i've created a script for users to upload images. but i keep getting this error.

fopen() [function.fopen]: Filename cannot be empty
this is the script.

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<?php
$thisfile="uploadpic.php";

$form = <<<FORM
<form action="$thisfile" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="25000"> Choose an image to upload: <input type="file" name="photo">
<br/>
<input type="submit" name="submit" value="Upload Image">
</form>
FORM;

if (isset($_POST['submit']))
{$fileName = $_FILES['photo']['name'];
$tmpName = $_FILES['photo']['tmp_name'];
$filesize = $_FILES['photo']['size'];
$filetype = $_FILES['photo']['type'];
if(!isset($_FILES['photo']))
{$messgae = "Please select an image to upload.";}

elseif($_FILES['photo']['size'] >= 500000)
{$message = "Your image is too large. Please keep the image size under 500kb.";}

else
{$fp = fopen($tmpname, 'r');
$photo = fread($fp, $filesize);
$photo = mysql_real_escape_string($photo);
fclose($fp);
$query= "UPDATE databse
SET photo = '$photo'
WHERE email = '$email'";
$result = mysql_query($query);
if (mysql_affected_rows() == 1)
{$message = "Your photo has been uploaded.";}
else
{$message = 'There was an error. Your photo has not been uploaded.';}
}
}
else
{$message = $form;}


?>
</head>
<body>
<h1>Profile picture</h1>
<?php echo $message; ?>
</body>
</html>
 
Last edited:

xgreenberetx

New Member
Messages
57
Reaction score
1
Points
0
For one you have to many < in front of the form tag
Code:
$form = <<<FORM
should be
Code:
$form = <FORM

Then change fopen to this
Code:
$fp = fopen($tmpname, "r");  //<-----use double quotes instead of single
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
]

fopen() [function.fopen]: Filename cannot be empty
this is the script.

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<?php
$thisfile="uploadpic.php";

$form = <<<FORM
<form action="$thisfile" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="25000"> Choose an image to upload: <input type="file" name="photo">
<br/>
<input type="submit" name="submit" value="Upload Image">
</form>
FORM;

if (isset($_POST['submit']))
{$fileName = $_FILES['photo']['name'];
$tmpName = $_FILES['photo']['tmp_name'];
$filesize = $_FILES['photo']['size'];
$filetype = $_FILES['photo']['type'];
if(!isset($_FILES['photo']))
{$messgae = "Please select an image to upload.";}

elseif($_FILES['photo']['size'] >= 500000)
{$message = "Your image is too large. Please keep the image size under 500kb.";}

else
{$fp = fopen($tmpname, 'r') ;
$photo = fread($fp, $filesize);
$photo = mysql_real_escape_string($photo);
fclose($fp);
$query= "UPDATE databse
SET photo = '$photo'
WHERE email = '$email'";
$result = mysql_query($query);
if (mysql_affected_rows() == 1)
{$message = "Your photo has been uploaded.";}
else
{$message = 'There was an error. Your photo has not been uploaded.';}
}
}
else
{$message = $form;}


?>
</head>
<body>
<h1>Profile picture</h1>
<?php echo $message; ?>
</body>
</html>

Check where you use fopen. It is saying that $tmpname is empty.

But how can that be? You have assigned a value to it.

$tmpName = $_FILES['photo']['tmp_name'];

Ooops. $tmpname != $tmpName
Edit:
For one you have to many < in front of the form tag
Code:
$form = <<<FORM
should be
Code:
$form = <FORM

The
Code:
<<<FORM
Stuff
FORM

construct is not a form tag. It is a method of quoting a string called a heredoc
 
Last edited:

tillabong

New Member
Messages
60
Reaction score
0
Points
0
Yeah its heredoc.

I've change the $tmpname thing and the double quotation and i get this error.
fopen() expects parameter 1 to be string, array
fread(): supplied argument is not a valid stream resource
fclose(): supplied argument is not a valid stream resource

im having another problem. i tried uploading a file that is 209kb but it still comes out as my file being too large. am i missing out any detail on the file size part?
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
im having another problem. i tried uploading a file that is 209kb but it still comes out as my file being too large. am i missing out any detail on the file size part?

Try adjusting this line

$form = <<<FORM
<form action="$thisfile" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="25000"> Choose an image to upload: <input type="file" name="photo">
<br/>
<input type="submit" name="submit" value="Upload Image">
</form>
FORM;
 
Top