We help IT Professionals succeed at work.

PHP Why wont this work- It works on htdocs locally

LeighJor
LeighJor asked
on
415 Views
Last Modified: 2013-12-13
Hi
Why wont this work- It works on htdocs loaclly -but not on server. The image file is supposed to go the image folder and the file name should be going into db table images.
It just wont work on server . $_SERVER['DOCUMENT_ROOT']  is supported.
Please Help
Thanks Leighjor
<?php
   if (array_key_exists('insert', $_POST )){
       include('buildFileList5.php');
       include('corefuncs.php');
//include('conn_mysqli.inc.php');
       if($_FILES['filename']['name']) {
           $target_path = $_SERVER['DOCUMENT_ROOT']."/images";
           $target_path = $target_path."/".$_FILES['filename'];
               //['name'];
          //$target_path = basename($target_path);        
      //($target_path);
               move_uploaded_file($_FILES['filename']['tmp_name'], $target_path);
          // $OK = false;
               
                  $filename = $_FILES['filename']['name'];
          //die($filename);
                  $caption = trim($_POST['caption']);
           // $sql = "insert into images ( filename, caption ) values ( '$filename', '$caption' )";
//     mysqli_query($conn,$sql);
 }}
 $conn=new mysqli('');
//$conn=new mysqli('' ) or die ( 'cannot opendb');

                  $sql = 'INSERT INTO images (filename, caption)VALUES(?, ?)';
  // initialize prepared statement
                          $stmt = $conn->stmt_init();
                         if ($stmt->prepare($sql)) {
    // bind parameters and execute statment
                            $stmt->bind_param('ss', $filename, $caption);
                              $OK = $stmt->execute();
                                     if ($OK) {
                                    echo 'Image Uplaoded';
                                    }
                                else {
                            echo $stmt->error;
                             
                              }
         
}      
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Insert image</title>
<link href="../assets/admin.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
#Layer1 {
      position:absolute;
      left:178px;
      top:269px;
      width:205px;
      height:42px;
      z-index:1;
      background-color: #000033;
}
#Layer2 {
      position:absolute;
      left:81px;
      top:41px;
      width:605px;
      height:259px;
      z-index:2;
      background-color: #000033;
}
#Layer3 {
      position:absolute;
      left:19px;
      top:47px;
      width:327px;
      height:102px;
      z-index:3;
}
#Layer4 {
      position:absolute;
      left:19px;
      top:112px;
      width:503px;
      height:61px;
      z-index:4;
}
#Layer5 {
      position:absolute;
      left:253px;
      top:205px;
      width:53px;
      height:24px;
      z-index:2;
}
body {
      background-color: #F6C38E;
}
body,td,th {
      color: #F6C38E;
}
a:link {
      color: #F6C38E;
}
a:visited {
      color: #F6C38E;
}
a:hover {
      color: #FFFFFF;
}
a:active {
      color: #F6C38E;
}
-->
</style>
</head>
<body>
<div id="Layer2">
<h1>Uplaod an  image </h1>
<?php /*?><?php if (isset($error)) { ?>
<p class="warning"><?php echo $error; ?></p>
<?php } ?><?php */?>
<!--<form id="form1" name="form1" method="post" action="">-->
 <form enctype="multipart/form-data" action="" method="POST">

    <div id="Layer3">
    <p>Choose an Image to Upload:
      <input name="filename" type="file" size="80" maxlength="150" />
      </p>
    </div>
    <div id="Layer4">
      <p>
        <label for="textfield">Give Image a name:</label>
       </p>
    <input name="caption" type="text" class="widebox" id="caption" size="80" maxlength="150" />
</p></div>
  <div id="Layer5"> </p>
    <p> <input name="insert" type="submit" id="insert" value="insert" />
  </p></div>
</form><div id="Layer1"><a href="admin.php">Return to ADMIN Homepage </a></div>
</div>
</div>
</body>
Comment
Watch Question

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
Some questions

1) Can you be more explicit? In what way does it "not work"?
2) Does the remote server have the image in the image folder?
3) I notice you are using the mysqli extension. Is this enabled on the remote server?

To check the mysqli extension either inspect the php.ini and check the [Extensions] section has an entry for mysqli or do a phpinfo() at the start of your script and see if it has a mysqli section

more at http://www.php.net/mysqli

Author

Commented:
It is not working as in 1. The image is not uploading .
                                   2  The file name is not going into the database
                                   3  It works fine locally
                                   4 Mysqli is enabled.
Frustrating
CERTIFIED EXPERT

Commented:
If, just after your opening <?php tag you add phpinfo(32) like this

<?php
phpinfo(32);

What does it say about the parameters passed? Try on both the live and local machines and compare the results. What differences exist?

Are both PHP versions the same?

Also, have you verified the upload directories permissions and that the mysqli connection to the database is correctly established?

Author

Commented:
Im using other forms to put other records into same database. These forms use the same $conn and mysqli . No problems doing that. I did change my image folder to 777 was 766 to no avail though.

Author

Commented:
when debugging in Zend I get this message
 Expression result is never used (line 24)
Line 24 is my db ---$conn=new mysqli(''); ah it is filled in correctly.

Author

Commented:
Again in hope
The following will  allow me to upload the image. Still cannot give filename into databse though? He...  l  ..p

<?php //phpinfo(32);
   if (array_key_exists('insert', $_POST )){
       include('buildFileList5.php');
       include('corefuncs.php');

$conn=new mysqli() or die ( 'cannot opendb');
       if($_FILES['filename']['name']) {
           $target_path = $_SERVER['DOCUMENT_ROOT']."/images";
           $target_path = $target_path."/".$_FILES['filename']['name'];
            move_uploaded_file($_FILES['filename']['tmp_name'], $target_path);
                 $filename = $_FILES['filename'];
                 $caption = trim($_POST['caption']);
              
      $sql = 'INSERT INTO images ( filename, caption)VALUES( ?, ? )';
                   $stmt = $conn->stmt_init();
                   if ($stmt->prepare($sql)) {                                                         $stmt->bind_param('ss', $filename, $caption);
                          $OK = $stmt->execute();
                  }
          }
}      
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Insert image</title>
<link href="../assets/admin.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="Layer2">
<h1>Uplaod an  image </h1>
<?php /*?><?php if (isset($error)) { ?>
<p class="warning"><?php echo $error; ?></p>
<?php } ?><?php */?>
    <div id="Layer3">
    <p>Choose an Image to Upload:
      <input name="filename" type="file" size="80" maxlength="150" />
      </p>
    </div>
    <div id="Layer4">
      <p>
        <label for="textfield">Give Image a name:</label>
       </p>
    <input name="caption" type="text" class="widebox" id="caption" size="80" maxlength="150" />
</p></div>
  <div id="Layer5"> </p>
    <p> <input name="insert" type="submit" id="insert" value="insert" />
  </p></div>
</form>
</div>
</div>
</body>

Author

Commented:
Humbly I come . The database type was mysql not mysqli --though all my sqli code works in other forms on the same database but different table.
Anyway I tried this with with mysql database conncetion
What I get is a file called array in the image folder
and the name 'array' in my database.
I am missing something simple Please Help

<?php
 $host = "";
   $dbname = "";
    $username = "";
  $password = "";
       $conn = mysql_connect($host, $username, $password);
    mysql_select_db($dbname,$conn);
     
   if (array_key_exists('insert', $_POST )){
       include('buildFileList5.php');
       include('corefuncs.php');
       if($_FILES['filename']['name']) {
           $target_path = $_SERVER['DOCUMENT_ROOT']."/bulbs/images";
           $target_path = $target_path."/".$_FILES['filename']['name'];
          //$target_path = basename($target_path);        
      //($target_path);
         move_uploaded_file($_FILES['filename']['tmp_name'], $target_path);
         // $OK = false;['name'];
                  $filename = $_FILES['filename'];
          //die($filename);['name']
                  $caption = trim($_POST['caption']);
                 //$filename = basename($filename);
                  $sql = "INSERT INTO images ( filename, caption ) VALUES( '$filename', '$caption' )";
             $result = mysql_query($sql) or die ( mysql_error());
                }
}      
?>
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.