Hi everybody,
would you please tell me what's wrong with this code
that generates the following error:

Warning: fopen(C:\Documents and Settings\Sando\Desktop\boys.gif) [function.fopen]: failed to open stream: No such file or directory in /home/domain/public_html/webs/divisions/chek.php on line 108
data inserted without any picture

although it works fine and great @ localhost
MySQL Server 5.1
Appache 2.2
PHP 5.2.11

and as I said just great
but on the server 'BLUEHOST' I get that error

help please:

<?php session_start();
// if session is not set redirect the user
if(empty($_SESSION['u_name'])){
 header("Location:table.php");
}
include ("connect.php");
include ("function.php");

$agent=$_SESSION['u_name'];
$sql="SELECT * FROM tbl_user WHERE user_name='$agent'    ";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$pri= $row['pri'];
If($pri ==1 || $pri ==3) {
GoTO("../log/cp.php");	
}

$host="localhost";
$username="name";
$password="pass";
$db_name="db";
$tbl_name="teachers2";
//////////////////////////////////////////////////////////////
mysql_connect("$host", "$username", "$password")or die("cannot connect server "); 
mysql_select_db("$db_name")or die("cannot select DB");
//////////////////////////////////////////////////////////////

$N_Teacher = $_POST['N_Teacher'];
$N_Email = $_POST['N_Email'];
$N_Teacher = addslashes($N_Teacher);
$N_Email = addslashes($N_Email);
$N_date=date("d-m-y H:i:s");
$agent=$_SESSION['u_name'];
$division=$_POST['division'];
$divisionID=$_POST['divisionID'];
$position=$_POST['position'];
$positionID=$_POST['positionID'];
$employee=$_POST['employee'];
$wordcomm=$_POST['wordcomm'];
$N_Telefon=$_POST['N_Telefon'];

if (empty($N_Teacher) || empty($N_Email))
{
bad();
}
else {
good();
}
////////////////////////////////////
function bad(){
echo "<center><font color='ff0000' font face='Tahoma' size='2'><b>Please fill in both name and email</B></font></center>";
$htmlheader=InCludePageHtml("template/header.html");
eval("echo stripslashes(\"$htmlheader\");");
GoTO("add.php");
}
//////////////////////////////////////////////////////////////
function good(){

$N_Teacher = $_POST['N_Teacher'];
$N_Email = $_POST['N_Email'];
$N_Teacher = addslashes($N_Teacher);
$N_Email = addslashes($N_Email);
$N_date=date("d-m-y H:i:s");
$agent=$_SESSION['u_name'];
$division=$_POST['division'];
$divisionID=$_POST['divisionID'];
$position=$_POST['position'];
$positionID=$_POST['positionID'];
$employee=$_POST['employee'];
$wordcomm=$_POST['wordcomm'];
$N_Telefon=$_POST['N_Telefon'];

$hndl=fopen($_REQUEST["imgfile"],"r"); 
$isize=sizeof($_REQUEST["imgfile"]); 

if (empty($hndl) ){
                        $hndl=fopen("temp.gif","r"); 
                         $imgdata=""; 
                        while(!feof($hndl)){   $imgdata.=fread($hndl,$isize); }
                         $imgdata=addslashes($imgdata); 

$sql="INSERT INTO teachers2(N_Teacher,N_Email,position,N_date,agent,division,divisionID,positionID,wordcomm,employee, N_Telefon,imgtype,pic)VALUES                                          ('$N_Teacher','$N_Email','$position','$N_date','$agent','$division','$divisionID','$positionID','$wordcomm','$employee','$N_Telefon','". $_REQUEST["imgtype"] ."','$imgdata')";

$result=mysql_query($sql);

if($result){
ECHO "<center><font color='088000' font face='Tahoma' size='2'><b> data inserted without any pictures </font></center></b>";
echo "<BR>";
$htmlfother=InCludePageHtml("template/fother.html");
eval("echo stripslashes(\"$htmlfother\");");
GoTO("Del_Edit.php");
}

else{
ECHO "<center><font color='ff0000' font face='Tahoma' size='2'><b>db error</font></center></b>";
echo "<BR>";
$htmlfother=InCludePageHtml("template/fother.html");
eval("echo stripslashes(\"$htmlfother\");");
GoTO("add.php");
}

}




else {
                 $hndl=fopen($_REQUEST["imgfile"],"r"); 
                 $isize=sizeof($_REQUEST["imgfile"]); 
                 $imgdata=""; 
                 while(!feof($hndl)){   $imgdata.=fread($hndl,$isize); }
                 $imgdata=addslashes($imgdata); 

$sql="INSERT INTO teachers2(N_Teacher,N_Email,position,N_date,agent,division,divisionID,positionID,wordcomm,employee, N_Telefon,imgtype,pic)VALUES                                          ('$N_Teacher','$N_Email','$position','$N_date','$agent','$division','$divisionID','$positionID','$wordcomm','$employee','$N_Telefon','". $_REQUEST["imgtype"] ."','$imgdata')";
$result=mysql_query($sql);

if($result){
ECHO "<center><font color='088000' font face='Tahoma' size='2'><b> all data  inserted </font></center></b>";
echo "<BR>";
$htmlfother=InCludePageHtml("template/fother.html");
eval("echo stripslashes(\"$htmlfother\");");
GoTO("Del_Edit.php");
}

else{
ECHO "<center><font color='ff0000' font face='Tahoma' size='2'><b>db issue -error </font></center></b>";
echo "<BR>";
$htmlfother=InCludePageHtml("template/fother.html");
eval("echo stripslashes(\"$htmlfother\");");
GoTO("add.php");
}

}


}
?>

Dani AI

Generated

Short answer: the script is trying to open a client-side/desktop path that does not exist on BlueHost. As and said, a path like a local Desktop file only works on your machine. Uploaded files live on the server in the PHP tmp folder and must be read from the $_FILES array (or put under your web root).

Quick checklist and fixes

  • Make sure the form uses method="post" enctype="multipart/form-data" and the file input is named imgfile.
  • Inspect var_dump($_FILES) to confirm the upload reached PHP.
  • Use $_FILES['imgfile']['tmp_name'] (and $_FILES['imgfile']['size']) instead of $_REQUEST['imgfile'] or sizeof(...). sizeof on a string is wrong here.
  • Read the uploaded file with file_get_contents() (or fopen with mode 'rb') and escape it before inserting into the DB. Test with is_uploaded_file() and is_readable() first.
  • If you want to keep a fallback, read a server-side file such as dirname(__FILE__).'/temp.gif' — not a client desktop path.

Practical example (reading uploaded file or fallback)

if (!empty($_FILES['imgfile']) && $_FILES['imgfile']['error'] === UPLOAD_ERR_OK) {
  $tmp = $_FILES['imgfile']['tmp_name'];
  if (is_uploaded_file($tmp) && is_readable($tmp)) {
    $imgdata = mysql_real_escape_string(file_get_contents($tmp)); // mysql connection required
    $imgtype = mysql_real_escape_string($_FILES['imgfile']['type']);
  }
} else {
  $fallback = dirname(__FILE__) . '/temp.gif';
  $imgdata = mysql_real_escape_string(file_get_contents($fallback));
  $imgtype = 'image/gif';
}

Extra notes

  • For production validate file type/size (getimagesize() or finfo) and reject unexpected uploads.
  • Consider storing files on disk with move_uploaded_file() and saving the path in the DB (more scalable) instead of large BLOBs.
  • Prefer parameterized statements (PDO or mysqli) over mysql_* functions to avoid injection and for forward compatibility.

Recommended Answers

All 6 Replies

Member Avatar for Member #120589

Yuk! Use code tags - can't read that.

The problem is that C:\Documents and Settings\Sando\Desktop\boys.gif is a file on your desktop. It works on localhost, because that file exists on your desktop. You need to upload the boys.gif file to Bluehost and change the line that opens that file.

EDIT (AFTER EXAMINING YOUR CODE FURTHER)
Just wondering, are you trying to upload a file. It seems to me like you are, and sadly, fopen will only work on the server. To understand how to upload files to a PHP script, read this article by Tizag.

Well, Thank you FlashCreations for answering!!!

I am not trying to upload to the server rather to insert into a db
but after reading the file name and size from the tmp folder on the server.

otherwise, I could have used move_uploaded_file($_FILES["file"]["tmp_name"], to store on the server ...

please help

So what exactly do you want? Insert the file name into a db?

try moving that directory somewhere below or in public_html (doc root).
Then access it there.

If you are trying to create an uploader, look into PHP file handling and uploading on W3Schools to learn more. If not, simply follow my and JRM's advice: upload the image to your web root (or a folder inside of it) and then change your fopen so that it reflects the new location of the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.