gracias por responder
... y bueno, ahora que lo mencionas, en realidad cuento con ese archivo jajaja, supongo que estoy haciendolo mal xD, a ver ¿tendría primero que mover el archivo que se ha subido para poder utilizarlo (ya que su formato es .tmp) y luego validar que fuese de extension sql, utilizarlo y posteriormente borrarlo?
estoy usando esta clase :
<?php
/**
* @author yas500('mohamedigm@gmail.com','mohamedigm@yahoo.com')
* @copyright 2009
* @Date 24 April
* @Phpversion 5.2.8
*/
/** // // // // // // // // // // // // // // ||
// this class is used for backup and ||
// restore mysql server databases or entire ||
// server , can compress backup ,send ||
// to ftp server ,restore from compress again ||
*/
abstract class BackupRestore
{
protected $link;
protected $table=array(); protected $text="";
private $debug='0';
/**
function construct for logging to database server
*/
final public function __construct($host='localhost',$user='root',$pass='')
{
try{
$this->connect($host,$user,$pass);
}
catch (exception $e){
}
}
final private function __destruct()
{
if ($this->msg){
foreach ($era as $rr){
echo "<b>Note</b>".$rr."<br>";
}
}
else
{
echo "The mission successfully complete.";
}
//SHOW ERROR NOTES TO USER AFTER END
}
final public function handleError($errno,$errmsg,$errfile,$errline)
{
if ($this->debug == '0'){
switch ($errno){
case E_USER_ERROR:
case E_WARNING:
echo $errmsg;
break;
case E_USER_WARNING:
case E_USER_NOTICE:
$this->msg[]=$errmsg;
return true;
break;
case E_NOTICE:
case E_STRICT:
return true;
break;
default:
echo "UNKNOWN ERROR OCCURED".$errmsg;
}
}
if ($this->debug == '1'){
echo "<b>".$errno."</b>: ".$errmsg." <b>LINE: ".$errline."</b>"."<br><b><i>In file</i></b> ".$errfile."<hr>";
}
}
final private function connect($host,$user,$pass)
{
throw new exception ("Mysql:couldn't connect to database server or invalid inforamtion");
}
// if you have multiple dbs enter them in that sequence
// $db1,$db2,..
// or leave it and it will backup the entire server databases
final public function setDbs($db='*')
{
if (empty($db) || $db == '*'){ if($rows == 0){
trigger_error("Mysql:THERE IS NO DATABASES ON THE SERVER!!",E_USER_ERROR); }
for($i=0;$i<$rows;$i++){
}
}
else{
$this->db=$db;
}
}
/**
// this method will be for selecting tables or ignore it and
// it will backup all tables
*/
final public function selectTable($table='*')
{
if($table == '')
$table='*';
if (! $table=="*" && count($this->db) > '1'){ trigger_error("you can't specify tables if you want more than one db",E_USER_ERROR); }
if ($table == "*"){
foreach ($this->db as $name){
$sql = "SHOW TABLES FROM $name";
//$list=mysql_list_tables($name);
for($i=0;$i<$rows;$i++){
}
}
}
else{
foreach($table as $tb){
$this->table[$this->db['0']][]=$tb;
}
}
}
/**
// method for selecting the query required
// for backup
// this is ahabit for me to store all queries required in amethod and call it
*/
final protected function selectQuery($type)
{
$query=array(1=>"SHOW CREATE DATABASE ","SHOW CREATE TABLE ","INSERT INTO ","DROP DATABASE IF EXISTS ","DROP TABLE IF EXISTS ","SELECT * FROM "); return $query[$type];
}
/**
method for validate file before restore database
*/
final protected function getFile($file)
{
$this->text="";
switch ($file){
return false;
break;
return false;
break;
default:
}
}
else
{
if(!$fp=fopen($file,'rb')) }
}
}
return true;
}
}
/**
this method is for prepare file that be backuped
//
*/
final protected function setFile($txt,$cmp,$ftp,$fhost,$fuser,$fpass,$fport)
{
$recognize="";
foreach ($this->db as $rec){
$recognize.=$rec."_";
}
//aca
$recognize=preg_replace("/_$/","",$recognize); //for naming file backuped
// this the preferred for me format for naming files
$file='backup@'.$recognize."@".date('Y-M-d',time()).'.sql'; if(!$fp=fopen($file,'wb')) {
trigger_error("You may have no enough rights on server",E_USER_ERROR); }
if ($cmp == '1'){
$file=$file.".gz";
trigger_error("Script failed to compress backuped file.",E_USER_NOTICE); }
if($ftp == '1'){
trigger_error("this is not avalid ftp server or make sure you type it well",E_USER_ERROR); if(!$log)trigger_error("Username or Password is not correct",E_USER_ERROR); $put=ftp_put($conn,$file,$file,FTP_BINARY
); if(!$put){
trigger_error("Couldn't upload file to remote server",E_USER_WARNING); }
}
else{
$this->downBackup($file);
}
return true;
}
// download backuped file
final private function downBackup($file)
{
header("Content-Description:File Transfer");; header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Content-Type: application/octet-stream'); }
abstract protected function backupData($cmp='0',$ftp='0',$fhost='',$fpass='',$fport='21');
abstract protected function restoreSql($file);
}
final class Backuprestoresql extends BackupRestore
{
const HEADERS='***********************
Y A S 5 0 0
***********************'; // mohamed ghareeb mohamed saeed signature
const FIELDSEP=';'; // seperate between queries
/* this method for backup
// you can specify more options like compression (0,1) , send through ftp to server (0,1)
//
*/
public function backupData($cmp='0',$ftp='0',$fhost='',$fuser='anonymous',$fpass='mohamedigm@gmail.com',$fport='21')
{
if($ftp == '1'){
trigger_error("You must specify ftp host name as you select ftp option",E_USER_ERROR); }
}
}
$this->text="";
$this->text.=self::HEADERS;
/**
this will begin save database to file
*/
foreach($this->db as $key){
$result=mysql_query($this->selectQuery('1')."$key",$this->link); $this->text.="\r\n".$this->selectQuery('4')."$key".self::FIELDSEP.$row['1'];
}
// this will save tables related to that database
foreach($this->table[$key] as $select){
if(! count($this->table[$key]) == '0'){ $result=mysql_query($this->selectQuery('2').$select,$this->link); $this->text.=self::FIELDSEP.$this->selectQuery('5').$select.self::FIELDSEP.$row['1'];
// fetch fields values in the tables
$result2=mysql_query($this->selectQuery('6').$select,$this->link); $txt="";
foreach ($row2 as $val){
$txt.="'".$val."'".",";
//aca
}
// save field values as insertion query
}
$this->text.=self::FIELDSEP.$this->selectQuery('3').$select." VALUES(".$txt.")";
}
}
}
}
//* finish database Dump *//
$this->text.="\r\n### ".$key." DATABASE DUMP COMPLETED ###";
}
if(!$this->setFile($this->text,$cmp,$ftp,$fhost,$fuser,$fpass,$fport))
trigger_error("Something goes wrong with file creation",E_USER_ERROR);
return true; // all things is done correctly
}
/**
this method for restore server database
//
*/
public function restoreSql($file)
{
if($this->getFile($file)){
$this->text=str_replace(self::HEADERS,"",$this->text); // ignore header $this->text=preg_replace("/###.* DATABASE DUMP COMPLETED ###/",self::FIELDSEP,$this->text); // ignore database dump complete message
foreach ($tt=explode(self::FIELDSEP,$this->text) as $query){ continue;
if(!$rs){
}
if (strstr($query,"CREATE DATABASE")){ //**
// could use query with "use database " and the name of database
//**
}
}
}
else
return true;
}
}
//***** EXAMPLES *****//
/** FOR BACKUP FILE **/
/**
|| $obj=new Backuprestoresql() // you can specify information of your server or make it
// default as localhost and user root with no pass
|| $obj->setDbs() // leave it blank or * and it will backup entire server
// or specify dbs "more one db1,db2,..."
// caution :: make sure you have rights to write on dbs you choose
|| $obj->selectTable() // leave it blank or * and it will backup all tables
// or specify tables "more one table1,table2,..."
|| $obj->backupData() // here will be the backup you can choose compression (0,1)
// ftp (0,1) if you specify ftp you must specify at least host
*/
/** FOR RESTORE FROM FILE **/
/**
|| $obj=new Backuprestoresql() // you can specify information of your server or make it
// default as localhost and user root with no pass
|| $obj->restoreSql('file path') // specify location of your file accepted extension sql,gz,txt
*/
?>