Forums

PHP

This topic is locked

"Warning" when submitting blank field.

Posted 06 Feb 2002 17:34:49
1
has voted
06 Feb 2002 17:34:49 Peter R posted:
Hello,

I'm using the following code to allow me to insert images into BLOB fields, as well as text into other fields. Everything works fine except if I leave 1 or both File Fields empty when submitting the form, I get the following errors:

Warning: fopen("none","r" - No such file or directory in /usr/local/www/vhosts/mydomain.com/htdocs/mysubdir/myrecordinsertpage.php on line 70

Warning: Supplied argument is not a valid File-Handle resource in /usr/local/www/vhosts/mydomain.com/htdocs/mysubdir/myrecordinsertpage.php on line 70

Warning: fopen("none","r" - No such file or directory in /usr/local/www/vhosts/mydomain.com/htdocs/mysubdir/myrecordinsertpage.php on line 71

Warning: Supplied argument is not a valid File-Handle resource in /usr/local/www/vhosts/mydomain.com/htdocs/mysubdir/myrecordinsertpage.php on line 71


Lines 70 and 71, are the lines that get the images to be uploaded. The lines start with $logo_upload and $image_upload. One is for uploading a company's logo, the other is for an image of a business, ie the company's building, or lobby.

But in some cases I may not have an image for one or the other, or possibly both. This forces me to leave the file upload fields blank, which causes the "Warnings".(leaving text fields blank causes no problems of course)

Is there something I can do to the following script so that it won't produce the warnings? Make the script somehow ignore a file field if it's blank?

Thanks as always for any help!

Peter R.


<?php
// code that will be executed if the form has been submitted:

if ($submit) {

// connect to the database
// (you may have to adjust the hostname,username or password)

mysql_select_db("mydatabase";

$logo_upload = addslashes(fread(fopen($file_cli_logo, "r", filesize($file_cli_logo)));
$image_upload = addslashes(fread(fopen($file_cli_image, "r", filesize($file_cli_image)));

$result=MYSQL_QUERY("INSERT INTO mydirectory.mytable (textfield_a,textfield_b,cli_logo,cli_image) ".
"VALUES ('$txt_textfield_a','$txt_textfield_b','$logo_upload','$image_upload')";

$id= mysql_insert_id();
print "<p>This file has the following Database ID: <b>$id</b>";

MYSQL_CLOSE();

} else {

// else show the form to submit new data:
?>

Replies

Replied 06 Feb 2002 23:58:20
06 Feb 2002 23:58:20 Tim Green replied:
All you have to do is to detect for blank values. Something like this, should work:-

if ($file_cli_logo != "" {
$logo_upload = addslashes(fread(fopen($file_cli_logo, "r", filesize($file_cli_logo)));
} else {
$logo_upload = "";
}
if ($file_cli_image != "" {
$image_upload = addslashes(fread(fopen($file_cli_image, "r", filesize($file_cli_image)));
} else {
$image_upload = "";
}


Hope it helps

Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>

Reply to this topic