$val) { $val = preg_replace("/[^_A-Za-z0-9-\.&= ]/i",'', $val); $_REQUEST[$key] = $val; } ?> BigDump ver. <?php echo (VERSION); ?>
'); } function skin_close() { echo (''); } skin_open(); echo ('

BigDump: Staggered MySQL Dump Importer v'.VERSION.'

'); skin_close(); $error = false; $file = false; // Check PHP version if (!$error && !function_exists('version_compare')) { echo ("

PHP version 4.1.0 is required for BigDump to proceed. You have PHP ".phpversion()." installed. Sorry!

\n"); $error=true; } // Calculate PHP max upload size (handle settings like 10M or 100K) if (!$error) { $upload_max_filesize=ini_get("upload_max_filesize"); if (eregi("([0-9]+)K",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024; if (eregi("([0-9]+)M",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024*1024; if (eregi("([0-9]+)G",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024*1024*1024; } // Get the current directory if (isset($_SERVER["CGIA"])) $upload_dir=dirname($_SERVER["CGIA"]); else if (isset($_SERVER["ORIG_PATH_TRANSLATED"])) $upload_dir=dirname($_SERVER["ORIG_PATH_TRANSLATED"]); else if (isset($_SERVER["ORIG_SCRIPT_FILENAME"])) $upload_dir=dirname($_SERVER["ORIG_SCRIPT_FILENAME"]); else if (isset($_SERVER["PATH_TRANSLATED"])) $upload_dir=dirname($_SERVER["PATH_TRANSLATED"]); else $upload_dir=dirname($_SERVER["SCRIPT_FILENAME"]); // Handle file upload if (!$error && isset($_REQUEST["uploadbutton"])) { if (is_uploaded_file($_FILES["dumpfile"]["tmp_name"]) && ($_FILES["dumpfile"]["error"])==0) { $uploaded_filename=str_replace(" ","_",$_FILES["dumpfile"]["name"]); $uploaded_filename=preg_replace("/[^_A-Za-z0-9-\.]/i",'',$uploaded_filename); $uploaded_filepath=str_replace("\\","/",$upload_dir."/".$uploaded_filename); if (file_exists($uploaded_filename)) { echo ("

File $uploaded_filename already exist! Delete and upload again!

\n"); } else if (!eregi("(\.(sql|gz|csv))$",$uploaded_filename)) { echo ("

You may only upload .sql .gz or .csv files.

\n"); } else if (!@move_uploaded_file($_FILES["dumpfile"]["tmp_name"],$uploaded_filepath)) { echo ("

Error moving uploaded file ".$_FILES["dumpfile"]["tmp_name"]." to the $uploaded_filepath

\n"); echo ("

Check the directory permissions for $upload_dir (must be 777)!

\n"); } else { echo ("

Uploaded file saved as $uploaded_filename

\n"); } } else { echo ("

Error uploading file ".$_FILES["dumpfile"]["name"]."

\n"); } } // Handle file deletion (delete only in the current directory for security reasons) if (!$error && isset($_REQUEST["delete"]) && $_REQUEST["delete"]!=basename($_SERVER["SCRIPT_FILENAME"])) { if (eregi("(\.(sql|gz|csv))$",$_REQUEST["delete"]) && @unlink(basename($_REQUEST["delete"]))) echo ("

".$_REQUEST["delete"]." was removed successfully

\n"); else echo ("

Can't remove ".$_REQUEST["delete"]."

\n"); } // Connect to the database if (!$error && !TESTMODE) { $dbconnection = @mysql_connect($db_server,$db_username,$db_password); if ($dbconnection) $db = mysql_select_db($db_name); if (!$dbconnection || !$db) { echo ("

Database connection failed due to ".mysql_error()."

\n"); echo ("

Edit the database settings in ".$_SERVER["SCRIPT_FILENAME"]." or contact your database provider

\n"); $error=true; } if (!$error && $db_connection_charset!=='') @mysql_query("SET NAMES $db_connection_charset", $dbconnection); } else { $dbconnection = false; } // List uploaded files in multifile mode if (!$error && !isset($_REQUEST["fn"]) && $filename=="") { if ($dirhandle = opendir($upload_dir)) { $dirhead=false; while (false !== ($dirfile = readdir($dirhandle))) { if ($dirfile != "." && $dirfile != ".." && $dirfile!=basename($_SERVER["SCRIPT_FILENAME"])) { if (!$dirhead) { echo ("\n"); echo ("\n"); $dirhead=true; } echo (""); if (eregi("\.sql$",$dirfile)) echo (""); elseif (eregi("\.gz$",$dirfile)) echo (""); elseif (eregi("\.csv$",$dirfile)) echo (""); else echo (""); if ((eregi("\.gz$",$dirfile) && function_exists("gzopen")) || eregi("\.sql$",$dirfile) || eregi("\.csv$",$dirfile)) echo ("\n \n"); else echo ("\n \n"); } } if ($dirhead) echo ("
FilenameSizeDate&TimeType  
$dirfile".filesize($dirfile)."".date ("Y-m-d H:i:s", filemtime($dirfile))."SQLGZipCSVMiscStart Import into $db_name at $db_serverDelete file
  
\n"); else echo ("

No uploaded files found in the working directory

\n"); closedir($dirhandle); } else { echo ("

Error listing directory $upload_dir

\n"); $error=true; } } // Single file mode if (!$error && !isset ($_REQUEST["fn"]) && $filename!="") { echo ("

Start Import from $filename into $db_name at $db_server

\n"); } // File Upload Form if (!$error && !isset($_REQUEST["fn"]) && $filename=="") { // Test permissions on working directory do { $tempfilename=time().".tmp"; } while (file_exists($tempfilename)); if (!($tempfile=@fopen($tempfilename,"w"))) { echo ("

Upload form disabled. Permissions for the working directory $upload_dir must be set to 777 in order "); echo ("to upload files from here. Alternatively you can upload your dump files via FTP.

\n"); } else { fclose($tempfile); unlink ($tempfilename); echo ("

You can now upload your dump file up to $upload_max_filesize bytes (".round ($upload_max_filesize/1024/1024)." Mbytes) "); echo ("directly from your browser to the server. Alternatively you can upload your dump files of any size via FTP.

\n"); ?>
" enctype="multipart/form-data">

Dump file:

Note: The current mySQL connection charset is $charset. Your dump file must be encoded in $charset in order to avoid problems with non-latin characters. You can change the connection charset using the \$db_connection_charset variable in bigdump.php

\n"); } } // Open the file if (!$error && isset($_REQUEST["start"])) { // Set current filename ($filename overrides $_REQUEST["fn"] if set) if ($filename!="") $curfilename=$filename; else if (isset($_REQUEST["fn"])) $curfilename=urldecode($_REQUEST["fn"]); else $curfilename=""; // Recognize GZip filename if (eregi("\.gz$",$curfilename)) $gzipmode=true; else $gzipmode=false; if ((!$gzipmode && !$file=@fopen($curfilename,"rt")) || ($gzipmode && !$file=@gzopen($curfilename,"rt"))) { echo ("

Can't open ".$curfilename." for import

\n"); echo ("

Please, check that your dump file name contains only alphanumerical characters, and rename it accordingly, for example: $curfilename.". "
Or, specify \$filename in bigdump.php with the full filename. ". "
Or, you have to upload the $curfilename to the server first.

\n"); $error=true; } // Get the file size (can't do it fast on gzipped files, no idea how) else if ((!$gzipmode && @fseek($file, 0, SEEK_END)==0) || ($gzipmode && @gzseek($file, 0)==0)) { if (!$gzipmode) $filesize = ftell($file); else $filesize = gztell($file); // Always zero, ignore } else { echo ("

I can't seek into $curfilename

\n"); $error=true; } } // ******************************************************************************************* // START IMPORT SESSION HERE // ******************************************************************************************* if (!$error && isset($_REQUEST["start"]) && isset($_REQUEST["foffset"]) && eregi("(\.(sql|gz|csv))$",$curfilename)) { // Check start and foffset are numeric values if (!is_numeric($_REQUEST["start"]) || !is_numeric($_REQUEST["foffset"])) { echo ("

UNEXPECTED: Non-numeric values for start and foffset

\n"); $error=true; } if (!$error) { $_REQUEST["start"] = floor($_REQUEST["start"]); $_REQUEST["foffset"] = floor($_REQUEST["foffset"]); skin_open(); if (TESTMODE) echo ("

TEST MODE ENABLED

\n"); echo ("

Processing file: ".$curfilename."

\n"); echo ("

Starting from line: ".$_REQUEST["start"]."

\n"); skin_close(); } // Check $_REQUEST["foffset"] upon $filesize (can't do it on gzipped files) if (!$error && !$gzipmode && $_REQUEST["foffset"]>$filesize) { echo ("

UNEXPECTED: Can't set file pointer behind the end of file

\n"); $error=true; } // Set file pointer to $_REQUEST["foffset"] if (!$error && ((!$gzipmode && fseek($file, $_REQUEST["foffset"])!=0) || ($gzipmode && gzseek($file, $_REQUEST["foffset"])!=0))) { echo ("

UNEXPECTED: Can't set file pointer to offset: ".$_REQUEST["foffset"]."

\n"); $error=true; } // Start processing queries from $file if (!$error) { $query=""; $queries=0; $totalqueries=$_REQUEST["totalqueries"]; $linenumber=$_REQUEST["start"]; $querylines=0; $inparents=false; // Stay processing as long as the $linespersession is not reached or the query is still incomplete while ($linenumber<$_REQUEST["start"]+$linespersession || $query!="") { // Read the whole next line $dumpline = ""; while (!feof($file) && substr ($dumpline, -1) != "\n") { if (!$gzipmode) $dumpline .= fgets($file, DATA_CHUNK_LENGTH); else $dumpline .= gzgets($file, DATA_CHUNK_LENGTH); } if ($dumpline==="") break; // Stop if csv file is used, but $csv_insert_table is not set if (($csv_insert_table == "") && (eregi("(\.csv)$",$curfilename))) { echo ("

Stopped at the line $linenumber.

"); echo ('

At this place the current query is from csv file, but $csv_insert_table was not set.'); echo ("You have to tell where you want to send your data.

\n"); $error=true; break; } // Create an SQL query from CSV line if (($csv_insert_table != "") && (eregi("(\.csv)$",$curfilename))) $dumpline = 'INSERT INTO '.$csv_insert_table.' VALUES ('.$dumpline.');'; // Handle DOS and Mac encoded linebreaks (I don't know if it will work on Win32 or Mac Servers) $dumpline=str_replace("\r\n", "\n", $dumpline); $dumpline=str_replace("\r", "\n", $dumpline); // DIAGNOSTIC // echo ("

Line $linenumber: $dumpline

\n"); // Skip comments and blank lines only if NOT in parents if (!$inparents) { $skipline=false; reset($comment); foreach ($comment as $comment_value) { if (!$inparents && (trim($dumpline)=="" || strpos ($dumpline, $comment_value) === 0)) { $skipline=true; break; } } if ($skipline) { $linenumber++; continue; } } // Remove double back-slashes from the dumpline prior to count the quotes ('\\' can only be within strings) $dumpline_deslashed = str_replace ("\\\\","",$dumpline); // Count ' and \' in the dumpline to avoid query break within a text field ending by ; // Please don't use double quotes ('"')to surround strings, it wont work $parents=substr_count ($dumpline_deslashed, "'")-substr_count ($dumpline_deslashed, "\\'"); if ($parents % 2 != 0) $inparents=!$inparents; // Add the line to query $query .= $dumpline; // Don't count the line if in parents (text fields may include unlimited linebreaks) if (!$inparents) $querylines++; // Stop if query contains more lines as defined by MAX_QUERY_LINES if ($querylines>MAX_QUERY_LINES) { echo ("

Stopped at the line $linenumber.

"); echo ("

At this place the current query includes more than ".MAX_QUERY_LINES." dump lines. That can happen if your dump file was "); echo ("created by some tool which doesn't place a semicolon followed by a linebreak at the end of each query, or if your dump contains "); echo ("extended inserts. Please read the BigDump FAQs for more infos.

\n"); $error=true; break; } // Execute query if end of query detected (; as last character) AND NOT in parents if (ereg(";$",trim($dumpline)) && !$inparents) { if (!TESTMODE && !mysql_query(trim($query), $dbconnection)) { echo ("

Error at the line $linenumber: ". trim($dumpline)."

\n"); echo ("

Query: ".trim(nl2br(htmlentities($query)))."

\n"); echo ("

MySQL: ".mysql_error()."

\n"); $error=true; break; } $totalqueries++; $queries++; $query=""; $querylines=0; } $linenumber++; } } // Get the current file position if (!$error) { if (!$gzipmode) $foffset = ftell($file); else $foffset = gztell($file); if (!$foffset) { echo ("

UNEXPECTED: Can't read the file pointer offset

\n"); $error=true; } } // Print statistics skin_open(); // echo ("

Statistics

\n"); if (!$error) { $lines_this = $linenumber-$_REQUEST["start"]; $lines_done = $linenumber-1; $lines_togo = ' ? '; $lines_tota = ' ? '; $queries_this = $queries; $queries_done = $totalqueries; $queries_togo = ' ? '; $queries_tota = ' ? '; $bytes_this = $foffset-$_REQUEST["foffset"]; $bytes_done = $foffset; $kbytes_this = round($bytes_this/1024,2); $kbytes_done = round($bytes_done/1024,2); $mbytes_this = round($kbytes_this/1024,2); $mbytes_done = round($kbytes_done/1024,2); if (!$gzipmode) { $bytes_togo = $filesize-$foffset; $bytes_tota = $filesize; $kbytes_togo = round($bytes_togo/1024,2); $kbytes_tota = round($bytes_tota/1024,2); $mbytes_togo = round($kbytes_togo/1024,2); $mbytes_tota = round($kbytes_tota/1024,2); $pct_this = ceil($bytes_this/$filesize*100); $pct_done = ceil($foffset/$filesize*100); $pct_togo = 100 - $pct_done; $pct_tota = 100; if ($bytes_togo==0) { $lines_togo = '0'; $lines_tota = $linenumber-1; $queries_togo = '0'; $queries_tota = $totalqueries; } $pct_bar = "
"; } else { $bytes_togo = ' ? '; $bytes_tota = ' ? '; $kbytes_togo = ' ? '; $kbytes_tota = ' ? '; $mbytes_togo = ' ? '; $mbytes_tota = ' ? '; $pct_this = ' ? '; $pct_done = ' ? '; $pct_togo = ' ? '; $pct_tota = 100; $pct_bar = str_replace(' ',' ','[ Not available for gzipped files ]'); } echo ("
SessionDoneTo goTotal
Lines$lines_this$lines_done$lines_togo$lines_tota
Queries$queries_this$queries_done$queries_togo$queries_tota
Bytes$bytes_this$bytes_done$bytes_togo$bytes_tota
KB$kbytes_this$kbytes_done$kbytes_togo$kbytes_tota
MB$mbytes_this$mbytes_done$mbytes_togo$mbytes_tota
%$pct_this$pct_done$pct_togo$pct_tota
% bar$pct_bar
\n"); // Finish message and restart the script if ($linenumber<$_REQUEST["start"]+$linespersession) { echo ("

Congratulations: End of file reached, assuming OK

\n"); echo ("

Thank you for using this tool! Please rate Bigdump at Hotscripts.com

\n"); echo ("

You can send me some bucks or euros as appreciation via PayPal. Thank you!

\n"); ?>
Now I'm waiting $delaypersession milliseconds before starting next session...

\n"); if (!$ajax) echo ("\n"); echo ("\n"); echo ("

Press STOP to abort the import OR WAIT!

\n"); } } else echo ("

Stopped on error

\n"); skin_close(); } if ($error) echo ("

Start from the beginning (DROP the old tables before restarting)

\n"); if ($dbconnection) mysql_close(); if ($file && !$gzipmode) fclose($file); else if ($file && $gzipmode) gzclose($file); ?>

© 2003-2008 Alexey Ozerov - BigDump Home

'."\n"; echo ''."\n"; echo 'cos'."\n"; echo ''."\n"; */ echo ''; echo ""; // data - for calculations echo ""; echo "$linenumber"; echo ""; echo ""; echo "$foffset"; echo ""; echo ""; echo '"'.$curfilename.'"'; echo ""; echo ""; echo "$totalqueries"; echo ""; // results - for form update echo ""; echo "$lines_this"; echo ""; echo ""; echo "$lines_done"; echo ""; echo ""; echo "$lines_togo"; echo ""; echo ""; echo "$lines_tota"; echo ""; echo ""; echo "$queries_this"; echo ""; echo ""; echo "$queries_done"; echo ""; echo ""; echo "$queries_togo"; echo ""; echo ""; echo "$queries_tota"; echo ""; echo ""; echo "$bytes_this"; echo ""; echo ""; echo "$bytes_done"; echo ""; echo ""; echo "$bytes_togo"; echo ""; echo ""; echo "$bytes_tota"; echo ""; echo ""; echo "$kbytes_this"; echo ""; echo ""; echo "$kbytes_done"; echo ""; echo ""; echo "$kbytes_togo"; echo ""; echo ""; echo "$kbytes_tota"; echo ""; echo ""; echo "$mbytes_this"; echo ""; echo ""; echo "$mbytes_done"; echo ""; echo ""; echo "$mbytes_togo"; echo ""; echo ""; echo "$mbytes_tota"; echo ""; echo ""; echo "$pct_this"; echo ""; echo ""; echo "$pct_done"; echo ""; echo ""; echo "$pct_togo"; echo ""; echo ""; echo "$pct_tota"; echo ""; // converting html to normal text $pct_bar = htmlentities($pct_bar); echo ""; echo "$pct_bar"; echo ""; echo ""; } function create_ajax_script() { global $linenumber, $foffset, $totalqueries, $delaypersession, $curfilename; ?>