Jump to content

Recommended Posts

I spent the weekend googling this one and found a hundred scripts to do this, but none will work because they all require globals to be turned on, and I'm not about to do that :P

 

I want to store some mp3's in a mysql table.  I want to do it purely for the education of storing and retrieving binary data.  I will use this later on other binary formats. 

 

Can someone help?  Again, I do not want to turn on globals, so I don't know how to get stuff like the file type, file size, etc, on the upload.

 

Here is a script that uses globals:

 

<?php
if ($action == "upload") {
  // ok, let's get the uploaded data and insert it into the db now
	$db = mysql_connect("localhost", "", "");
mysql_select_db("binary_files", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");
  if (isset($binFile) && $binFile != "none") {
    $data = addslashes(fread(fopen($binFile, "r"), filesize($binFile)));
    $strDescription = addslashes(nl2br($txtDescription));
    $sql = "INSERT INTO tbl_Files ";
    $sql .= "(description, bin_data, filename, filesize, filetype) ";
    $sql .= "VALUES ('$strDescription', '$data', ";
    $sql .= "'$binFile_name', '$binFile_size', '$binFile_type')";
    $result = mysql_query($sql, $db);
    mysql_free_result($result); // it's always nice to clean up!
    echo "Thank you. The new file was successfully added to our database.<br><br>";
    echo "<a href='main.php'>Continue</a>";
  }
  mysql_close();

} else {
?>
<HTML>
<BODY>
<FORM METHOD="post" ACTION="add.php" ENCTYPE="multipart/form-data">
<INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="1000000">
<INPUT TYPE="hidden" NAME="action" VALUE="upload">
<TABLE BORDER="1">
  <TR>
   <TD>Description: </TD>
   <TD><TEXTAREA NAME="txtDescription" ROWS="10" COLS="50"></TEXTAREA></TD>
  </TR>
  <TR>
   <TD>File: </TD>
   <TD><INPUT TYPE="file" NAME="binFile"></TD>
  </TR>
  <TR>
   <TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload"></TD>
  </TR>
</TABLE>
</FORM>
</BODY>
</HTML>
<?php
}
?>

I'm sure he means register_globals.

 

All the data you need is available in $_REQUEST and $_FILES.  Try the following:

 

print "<pre>";
var_dump($_REQUEST);
var_dump($_FILES);

 

Put that at the top of your script and see what's inside those variables.

thats what i thought until

 

 

Can someone help?  Again, I do not want to turn on globals, so I don't know how to get stuff like the file type, file size, etc, on the upload.

 

then a script with no globals except a super global $_FILES !!

 

??? maybe a misunderstanding of what register_globals is!

Yea, I guess I meant register globals .in the old days $whatever existed without doing

 

$whatever=$_GET['whatever'];

 

That was bad for a lot of reasons, and I it looks to me like that is what the script(s) are doing.  I can use this suggestion:

 

print "<pre>";

var_dump($_REQUEST);

var_dump($_FILES);

 

and pull the values out of the arrays.

 

Q:

 

The above gives this:

 

array(3) {

  ["MAX_FILE_SIZE"]=>

  string(7) "1000000"

  ["action"]=>

  string(6) "upload"

  ["txtDescription"]=>

  string(4) "test"

}

array(1) {

  ["binFile"]=>

  array(5) {

    ["name"]=>

    string(14) "irishhymn1.mp3"

    ["type"]=>

    string(0) ""

    ["tmp_name"]=>

    string(0) ""

    ["error"]=>

    int(2)

    ["size"]=>

    int(0)

  }

}

 

I still have to do this,

 

$binFile_size=$_FILES['size'];

 

right?

 

The script as is says $binFile_size doesn't exist, for example.

This

 

if (isset($_REQUEST['action'])&& $_REQUEST['action'] == "upload") {
  // ok, let's get the uploaded data and insert it into the db now
$txtDescription=$_REQUEST['txtDescription'];
$binFile_name=$_FILES['binFile']['name'];
$binFile_size=$_FILES['binFile']['size'];
$binFile_type=$_FILES['binFile']['type'];

$db = mysql_connect("localhost", "", "");
mysql_select_db("binary_files", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");

    $data = addslashes(fread(fopen($binFile_name, "r"), filesize($binFile_size)));
    $strDescription = addslashes(nl2br($txtDescription));
    $sql = "INSERT INTO tbl_Files ";
    $sql .= "(description, bin_data, filename, filesize, filetype) ";
    $sql .= "VALUES ('$strDescription', '$data', ";
    $sql .= "'$binFile_name', '$binFile_size', '$binFile_type')";
    $result = mysql_query($sql, $db);
    mysql_free_result($result); // it's always nice to clean up!
    echo "Thank you. The new file was successfully added to our database.<br><br>";
    echo "<a href='main.php'>Continue</a>";
  mysql_close();
} else {
?>
<HTML>
<BODY>
<FORM METHOD="post" ACTION="add.php" ENCTYPE="multipart/form-data">
<INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="1000000">
<INPUT TYPE="hidden" NAME="action" VALUE="upload">
<TABLE BORDER="1">
  <TR>
   <TD>Description: </TD>
   <TD><TEXTAREA NAME="txtDescription" ROWS="10" COLS="50"></TEXTAREA></TD>
  </TR>
  <TR>
   <TD>File: </TD>
   <TD><INPUT TYPE="file" NAME="binFile"></TD>
  </TR>
  <TR>
   <TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload"></TD>
  </TR>
</TABLE>
</FORM>
</BODY>
</HTML>
<?php
}

 

Gives me this:

 

Warning: filesize() [function.filesize]: stat failed for 0 in /home/osprey/public_html/add.php on line 18

Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/osprey/public_html/add.php on line 18

 

When I select an mp3 from the current directory...???

 

Here is the var_dump for the file.  It shows that the file size is 0??? (I'm on LAMP, is it a permissions thing?):

 

array(1) {
  ["binFile"]=>
  array(5) {
    ["name"]=>
    string(14) "irishhymn1.mp3"
    ["type"]=>
    string(0) ""
    ["tmp_name"]=>
    string(0) ""
    ["error"]=>
    int(2)
    ["size"]=>
    int(0)
  }
}

Go to the section in the manual entitled "Handling File Uploads" (no surprises so far ;)  )

 

http://www.php.net/manual/en/features.file-upload.php

 

Scroll down and click on the link "error codes" (just above example 38.3)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.