Page 1 of 1

Splitting $variables for transfer into MySQL

Posted: Sat Mar 15, 2003 11:42 pm
by belaraka
I've been having problems transferring large files into my MySQL db. I know most people don't find this to be a good idea but since I have a 20MB quota on my disk space and my MySQL database information doesn't count towards this quota I find it very useful to place images and large files into a mysql BLOG field.

You can see the upload page here: http://www.clpoetics.com/test/index.php.

Just for notice this is the code I am currently using to insert the data into mysql and it works perfectly for files under 1MB.

Code: Select all

<?
if ($action == "upload") &#123;
  // ok, let's get the uploaded data and insert it into the db now
  include "open_db.inc";

  if (isset($binFile) && $binFile != "none") &#123;
    $data = addslashes(fread(fopen($binFile, "rb"), 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);
    if(!$result == 0) &#123;
echo "Thank you. The new file $binFile_name<br>Size: $binFile_size<br>Type:$binFile_type<br>Was successfully added to our database with the following description:<br>$txtDescription<br><br>";
    echo "<a href="/test/index.php">Add another file.</a>";
&#125; else &#123;
    echo "The file $binFile_name<br>Size: $binFile_size<br>Type: $binFile_type<br>was not added properly for an unknown reason.<br>";
    echo "<a href="/test/index.php">Please try another file.</a>";
  &#125;
&#125;
  mysql_close();

&#125; else &#123;
//all that follows is the HTML which shows the upload form
&#125;
?>
Files over 1MB this will not work since the max_allowed_packet in the mysql my.cnf file is set to 1MB and since I am on a shared server I can not change this. Now here is the big question:

Can the variable $binFile be split into multiple variables of 1MB a each.
If so then I could transfer them each into a temp table in my database and then append them together.

Forgive me if this post seems somewhat confusing as I am not sure how to exactly explain this. Either way it should be somewhat like the following:

//a variable holding 3.5MB of data exists and is to be transferred to a
//mysql database
$largeuserfile
//this file needs to be split into 1MB sections
//----HOW DO I DO THAT!!???----
function to_split_variable($largeuserfile) {
????
????
????
return $splitfile1, $splitfile2, $splitfile3, $splitfile4;
//would probably be best if it could be split into an array
}

I would then transfer each array seperately into the database.

Once inside the database I would append, to the $splitfile1, the data in the database starting from $splitfile2 and so on.

Any ideas on how I could do this?

Posted: Sat Mar 15, 2003 11:47 pm
by Stoker
...if your host know you where doing that they would probably boot you off pretty quick... so much overhead you are creating with both php and mysql handling megabytes of data... tsk tsk

Posted: Sun Mar 16, 2003 12:02 am
by belaraka
Stoker wrote:...if your host know you where doing that they would probably boot you off pretty quick... so much overhead you are creating with both php and mysql handling megabytes of data... tsk tsk
Well handling megs of data isn't what I mean to do but I do have about two files which is what I need this for, the rest of the blog fields I use are going to be from my users and they will be limited to the amount of space each file is as well as the number of files posted.