PHP version problem...please help!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
StayClasy
Forum Newbie
Posts: 2
Joined: Thu Apr 06, 2006 4:26 pm

PHP version problem...please help!!

Post by StayClasy »

Hi i am a newbie so go easy on me :wink:

I am running

Apache/1.3.11
PHP/5.0.5

I have a script that uploads a zip file and stores it as binary data into MySQL. I thought the code wasnt working until two other people i asked ran it on their own personal servers and it works fine. The problem is, it wont work on mine and it is driving me crazy to the point i think im gonna go insane.

I am using a university computers so im not sure if its the way these are configured or the versions not be aligned with the code.

I suspect its the way i am passing the file from the html page into the php.
Here is the code:

binary_form.html:

Code: Select all

<HTML>
<TITLE>Upload your files</TITLE>
<BODY>
<font face=verdana size=2>
<FORM METHOD="post" ACTION="add_binary_data.php" ENCTYPE="multipart/form-data">
 <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="500000">
 <!--
 Set the size of MAX_FILE_SIZE I have set it to approx 500KB means when we add 
 binary data add.php will check if the binaryfile size is > MAX_FILE_SIZE it wont allow
 this is just a precaution so that users do not upload very large files. 
 -->
 <INPUT TYPE="hidden" NAME="action" VALUE="upload">
 <TABLE BORDER="1" cellspacing=0 cellpadding=0>
  <TR>
   <TD>File Description: </TD>

   <TD><TEXTAREA NAME="file_description" ROWS="3" COLS="50"></TEXTAREA></TD>
  </TR>
  <TR>
   <TD>File: </TD>
   <TD><INPUT TYPE="file" NAME="binary_File"></TD>
  </TR>
  <TR>
   <TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload French"></TD>

  </TR>
 </TABLE>
</FORM>
</font>
</BODY>
</HTML>
add_binary_data.php:

Code: Select all

<?php
  
$db = mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("test",$db) or die(mysql_error()); //connects to our mybuddy database 

//$binary_File=$_POST['binary_File'];


$data = ('hello');

if (isset($binary_File) && $binary_File != "none") 
{
  $data = addslashes(fread(fopen($binary_File, "r"), filesize($binary_File)));
  $strDescription = addslashes($file_description);
    
  $sql = "INSERT INTO binary_data_files ";
  $sql .= "(description, bin_data, filename, filesize, filetype) ";
  $sql .= "VALUES ('$strDescription', '$data', ";
  $sql .= "'$binary_File_name', '$binary_File_size', '$binary_File_type')";
    
  $result = mysql_query($sql, $db);
    if (!$result) {
   $message  = 'Invalid query: ' . mysql_error() . "\n";
   $message .= 'Whole query: ' .$result;
   die($message);
}
 echo "<font face=verdana size=2>The file was successfully added to our database.<P>";
 
 echo "<P><B>File Name: </B>". $binary_File_name;
 echo "<P><B>File Size: </B>". $binary_File_size ." bytes  (approx ". $binary_File_size/1024 ." KB)";
 echo "<P><B>File Type: </B>". $binary_File_type;
}else{
echo $data;
}
mysql_close();
?>
</font>
The problem is this, it is simply not going into the IF statement and is therefore not setting $binary_File. I wonder with my version should i be posting or getting it differently?? i.e.

Code: Select all

if (isset($_GET['binary_File']) && ($_GET['binary_File']) != "none"){
Those that it worked on are running versions
Apache 1.3.34
PHP 4.4.2

Any help would be appreciated, i am going out my mind as to why this wont work.

Thanks alot
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

My guess is that the others hosts are running with global variables on (which is a bad thing, and a simply websearch will learn you why).

Anyway, since your html is using POST (<FORM METHOD="post") you should look in $_POST for regular variables.
Since you are posting binary data you should look in $_FILES.

And there is a complete section dedicated in the manual: http://be.php.net/features.file-upload.
StayClasy
Forum Newbie
Posts: 2
Joined: Thu Apr 06, 2006 4:26 pm

Post by StayClasy »

Hey thanks for the reply,

i just realised about the $_FILES from a tutorial on here about something entirely different.

I am now doing this:

Code: Select all

<?php
  
$db = mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("test",$db) or die(mysql_error()); //connects to our mybuddy database 


$binary_File = $_FILES['binary_File'];
$file_description = $_FILES['file_description'];

$data = ('hello');
if (isset($binary_File) && $binary_File != "none")
{
echo $data;
  $data = addslashes(fread(fopen($binary_File, "r"), filesize($binary_File)));
 
  $strDescription = addslashes($file_description);
    
  $sql = "INSERT INTO binary_data_files ";
  $sql .= "(description, bin_data, filename, filesize, filetype) ";
  $sql .= "VALUES ('$strDescription', '$data', ";
  $sql .= "'$binary_File_name', '$binary_File_size', '$binary_File_type')";
    
  $result = mysql_query($sql, $db);
    if (!$result) {
   $message  = 'Invalid query: ' . mysql_error() . "\n";
   $message .= 'Whole query: ' .$result;
   die($message);
}
 echo "<font face=verdana size=2>The file was successfully added to our database.<P>";
 
 echo "<P><B>File Name: </B>". $binary_File_name;
 echo "<P><B>File Size: </B>". $binary_File_size ." bytes  (approx ". $binary_File_size/1024 ." KB)";
 echo "<P><B>File Type: </B>". $binary_File_type;
}else{
echo $data;
}
mysql_close();
?>
</font>
It is actually putting an entry into MySQL however it is just blank (except for the autoincrement id) and in apache error log it is giving these errors:

[error] PHP Notice: Undefined index: file_description in h:\\Project\\htdocs\\backup\\add_binary_data.php on line 9
[error] PHP Warning: fopen() expects parameter 1 to be string, array given in h:\\Project\\htdocs\\backup\\add_binary_data.php on line 15
[error] PHP Notice: Array to string conversion in h:\\Project\\htdocs\\backup\\add_binary_data.php on line 15
[error] PHP Warning: filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for Array in h:\\Project\\htdocs\\backup\\add_binary_data.php on line 15
[error] PHP Warning: fread(): supplied argument is not a valid stream resource in h:\\Project\\htdocs\\backup\\add_binary_data.php on line 15
[error] PHP Notice: Undefined variable: binary_File_name in h:\\Project\\htdocs\\backup\\add_binary_data.php on line 22
[error] PHP Notice: Undefined variable: binary_File_size in h:\\Project\\htdocs\\backup\\add_binary_data.php on line 22
[error] PHP Notice: Undefined variable: binary_File_type in h:\\Project\\htdocs\\backup\\add_binary_data.php on line 22
[error] PHP Notice: Undefined variable: binary_File_name in h:\\Project\\htdocs\\backup\\add_binary_data.php on line 32
[error] PHP Notice: Undefined variable: binary_File_size in h:\\Project\\htdocs\\backup\\add_binary_data.php on line 33
[error] PHP Notice: Undefined variable: binary_File_size in h:\\Project\\htdocs\\backup\\add_binary_data.php on line 33
[error] PHP Notice: Undefined variable: binary_File_type in h:\\Project\\htdocs\\backup\\add_binary_data.php on line 34


Can you shed any light on this??

I am reading and trying to learn it myself but ive been on this for about 12 hours and it is killing me

Thanks
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

This should get you started:

If you want the name: $_FILES['binary_File']['name'].
If you want the size: $_FILES['binary_File']['size'].
If you want the path to the file: $_FILES['binary_File']['tmp_name']
...
Post Reply