加载下一页之前jQuery / PHP文件上传超时

时间:2013-02-19 17:15:47

标签: php javascript jquery image-uploading

所以我基于现有的开源脚本开发了以下图像上传脚本。它目前可在此处观看:http://images.oneightynyc.com/

现在,如果您拍摄任何系列的常规尺寸图像(5mb以下)并继续上传它们,上传过程就可以了。上传文件,并将您带到显示这些文件的链接代码的页面。但是,假设您上传了一些大图像,如下所示:

http://imaging.nikon.com/lineup/dslr/d90/img/sample/pic_005b.jpg

http://imaging.nikon.com/lineup/dslr/d90/img/sample/pic_003b.jpg

上传发生在此过程中,但脚本永远不会将您带到上传的页面。我知道上传实际发生的唯一方法是浏览到Gallery页面并看到文件列在那里。

以下是处理上传的uploader.php文件:

<?
//ob_start();
session_start();
$auth_id=$_SESSION['userid'];
if (!$auth_id || empty($auth_id) || $auth_id==""){
$auth_id = 0;
}
require_once("config.php"); 
require_once("limits.php"); 
require_once("ftp.class.php");
require_once("func.php"); 

$link = mysql_connect($db_server, $db_user, $db_password) or die("Could not connect to the database.");

mysql_select_db($db_name) or die("Could not select the database.");

if ($config[Uploads] == 0) {

$msg= "<center><b><br><br><br>Uploads are temporarily disabled by the site admin</center></b>";
}

else if ($config[Uploads] == 1 && !$auth_id) {

$msg= "<center><b><br><br><br>You have to <a href=\"register.php\" title=\"Register\">Register</a> before you will be able to upload photos.</center></b>";
}


$query = "select count(*) as total from ftp where status=1";

$result = mysql_query($query) or die("Query failed.");


while ($row = mysql_fetch_array($result))

{

$total=$row[total];
}


if($total<=0)

    {

    $no_server="1";

    $ftpid=0;

    $url=$server_url."/images/";

    }

else

{

$query = "select * from ftp where status=1 ORDER BY RAND() limit 1";

$result = mysql_query($query) or die("Query failed.");

while ($row = mysql_fetch_array($result)) 

    {

            $no_server="0";

        $ftpid=$row['ftpid'];

        $path=$row['name'];        

        $url=$row['dir'];        

        $host=$row['host'];        

        $user=$row['user'];        

        $pass=$row['ftppass'];        

    }    

 }

// get variables for fields on upload screen                

        $tos = $_POST['tos'];        

        $prv = $_POST['prv'];        

                if($prv!="1")

                $prv=0;



        $uploaderip = $_SERVER['REMOTE_ADDR'];

        $messages="";

        $msg="";

        $newID="";

        $FileName="";

        $FileFile="";

        $FileUrl="";

        $FileUrlLink="";

        $FiletnUrl="";

        // check for blocked ip address

        if ($uploaderip != "") {

                $query = "select ip from blocked where ip = '$uploaderip'";

            $result = mysql_query($query) or die("Query failed.");

                $num_rows = mysql_num_rows($result);



                if ($num_rows > 0) {

                        $msg= "Your IP address (".$uploaderip.") has been blocked from using this service.";

                }



        }


if ($config[AcceptTerms]=="1"){

if ($tos=="") 

                {

                    $msg= "You must check the box stating you agree to our terms.";

                    echo "<script language='javascript'>parent.upload('".$msg."','".$newID."','".$messages."','".$FileName."','".$FileFile."','".$FileUrl."','".$FileUrlLink."','".$FiletnUrl."','".$page_url."','".$server_url."','".$site_name."','".$HotLink."');</script>";

                }

                }

if($msg=="")
{



// check for a file

for($i=0;$i<=14;$i++)

{

        $err="0";

        $thefile = $_FILES['thefile'.$i];        




        if ($thefile['name']!="") 

                {


        // check for valid file extension

$path_parts = pathinfo($thefile['name']);

$file_ext = strtolower($path_parts['extension']);


        if ($err == "0") 

                {

                        // check for valid file type

                        if (!in_array_nocase($file_ext, $valid_file_ext)) 

                                {

                                        $messages.= "|<em>".$thefile['name']."</em> is not in a valid format (".$valid_mime_types_display.")";



                                        $err="1";

                                }                        

                }


 if ($err == "0") {        
   // check for valid image file
$imageinfo = getimagesize($_FILES['thefile0']['tmp_name']);

if(!eregi('image',$imageinfo['mime'])) {

$messages.="|". "Sorry, This is not a valid image file!";
  $err="1";  } }



                if ($err == "0") 

                {        



                     // check for valid file size

                        if ($thefile['size'] > ($max_file_size_b)) 

                                {
                $filesizemb =($thefile['size']/1048576);
                $filesizemb = number_format($filesizemb, 3);

$messages.="Sorry but this image size is ".$filesizemb." MB which is bigger than the max allowed file size of ".$max_file_size_mb." MB.";

                                        $err="1";        

                                }

                }










       // save the file, if no error messages

        if ($err == "0") 

                {


    // replace special chars with  spaces
    $thefile['name'] = eregi_replace("[^a-z0-9.]", " ", $thefile['name']);

    // Replace multiple spaces with one space
    $thefile['name'] = ereg_replace(' +', ' ', $thefile['name']);
    // Replace spaces with underscore
    $thefile['name'] = str_replace(' ', '_', $thefile['name']);
    // Replace hyphens with underscore
    $thefile['name'] = str_replace('-', '_', $thefile['name']);  

    // Replace multiple underscores with one underscore
    $thefile['name'] = ereg_replace('_+', '_', $thefile['name']);


$path_parts = pathinfo($thefile['name']);

// if php < 5.2
              if(!isset($path_parts['filename'])){
                $path_parts['filename'] = substr($path_parts['basename'], 0,strpos($path_parts['basename'],'.'));
              }


$thefile['name'] = strpos($path_parts['filename'], '.');


$thefile['name'] = substr($path_parts['filename'], 0, 22); // limit file name length to 22 chars from the beginning

$thefile['name'] = $thefile['name'] . "." . strtolower($path_parts['extension']);    


    // Generate prefix to add to file name

     $prefix = rand(99,999);

    // Add prefix to file name

    $newFileName = $prefix . $thefile['name'];    



                        // SAVE THE PICTURE


                        $FileName.="|". newImageName($thefile['name']);

                        $FileFile.="|". $server_dir . $newFileName;

                        $newFile = $server_dir . $newFileName;                        

                        $newFileUrl = $url . $newFileName;

                        $FileUrl.="|". $url . $newFileName;

                        $newFileUrlLink = $server_save_directory . $newFileName;

                        $FileUrlLink.="|". $newFileName;


                         if (in_array_nocase($file_ext, $valid_file_ext)) 


                                {

$lx = 3;
if ($file_ext == "jpeg") {
$lx = 4; }

$tnFileName = substr($newFileName, 0, strlen($newFileName) - $lx) . "jpg";
$tnFileName = str_replace('.', '_tn.', $tnFileName);


                                        $tnFile = $server_dir . $tnFileName;

                                        $FiletnUrl.="|". $url . $tnFileName;

                                        $tnFileUrl = $url . $tnFileName;

                                } 

                        else 

                                {

                                        $tnFileName = "";

                                        $tnFile = "";

                                        $tnFileUrl = "";

                                }

                        $filesize = $thefile['size'];

                        $newID = "";

                        if (!@copy($thefile['tmp_name'], $newFile)) 

                                {

                                        $messages.="|". "Please check site settings in admin panel and set proper value for server local path.<br><br>Also please make sure the images folder is chmodded to 0777";

                                } 

                        else 

                                {

                                        // add to database

                                        if($auth_id)        

                                        $uid=$auth_id;

                                        else $uid=0;



                                        //ftpupload($host,$user,$pass,$path."/".$dir."/".$newFileName,$newFileUrl);                                        

                                        //ftpupload

                                        if($no_server=="0")

                                         {

                                        $ftp =& new FTP();

                                        if ($ftp->connect($host)) {

                                            if ($ftp->login($user,$pass)) {

                                                $ftp->chdir($path);

                                                $ftp->put($newFileName,$newFile);

                                            }

                                        }

                        //  unlink($newFile);


                                          }

                                        //ftpupload

                                        $date_add=time();


                                        $query = "INSERT INTO images (prv,ftpid,userid,filename, tn_filename, filepath, ip, filesize,added) VALUES ($prv,$ftpid,$uid,'$newFileName', '$tnFileName', '$url', '$uploaderip', $filesize,$date_add)";



                                        mysql_query($query) or die("Database entry failed.");

                                        $newID.="|". mysql_insert_id();

                                }


                                 if ($file_ext == "jpeg" ||$file_ext == "jpg" || $file_ext == "png" || $file_ext == "gif" || $file_ext == "bmp") 


                                {

                                        if ($file_ext == "jpg") 

                                                {

                                                        $source_id = imagecreatefromjpeg($newFile);

                                                }

                                        if ($file_ext == "jpeg") 

                                                {

                                                        $source_id = imagecreatefromjpeg($newFile);

                                                }                                               

                                        elseif ($file_ext == "png") 

                                                {

                                                        $source_id = imagecreatefrompng($newFile);

                                                } 

                                        elseif ($file_ext == "gif") 

                                                {

                                                        $source_id = imagecreatefromgif($newFile);

                                                }

                                         elseif ($file_ext == "bmp") 

                                                {

                                                        $source_id = ImageCreateFromBMP($newFile);

                                                }

                                        $true_width = imagesx($source_id);

                                        $true_height = imagesy($source_id);





                                }

                }



} 


} 

mysql_close($link);

        // create URL links to display to user



        $showURL1 = false;  // image on hosted page - image only



        $showURL2 = false;  // direct link to file - all



        $showURL3 = false;  // HTML for img - image only



        $showURL4 = false;  // [img][/img] tags - image only



        $showURL5 = false;  // thumbnail pic - image only







        // determine flags



        $showURL2 = true;



        if ($file_ext == "jpg" || $file_ext == "jpeg"|| $file_ext == "gif" || $file_ext == "png" || $file_ext == "bmp") {



                $showURL1 = true;



                $showURL3 = true;



                $showURL4 = true;



        }



        if ($file_ext == "jpg" || $file_ext == "gif" || $file_ext == "png"|| $file_ext == "jpeg" || $file_ext == "bmp") {

                        $showURL5 = true;                        

        }

                    echo "<script language='javascript'>parent.upload('".$msg."','".$newID."','".$messages."','".$FileName."','".$FileFile."','".$FileUrl."','".$FileUrlLink."','".$FiletnUrl."','".$page_url."','".$server_url."','".$site_name."','".$HotLink."');</script>";


}
else
{
    echo "<script language='javascript'>parent.uploaderror('".$msg."');</script>";
    exit;
}



        function newImageName($fname) {



                $timestamp = time();



                $new_image_file_ext = substr($fname, strlen($fname) - 3, strlen($fname));



                if ($new_image_file_ext == "peg") {



                        $ext = ".jpg";



                } else {



                        $ext = "." . $new_image_file_ext;



                }



                $newfilename = randString() . substr($timestamp, strlen(timestamp) - 4, strlen(timestamp)) . $ext;



                return $newfilename;



        }







        function randString() {



                $newstring="";



                while(strlen($newstring) < 3) {



                        $randnum = mt_rand(0,61);



                        if ($randnum < 10) {



                                $newstring .= chr($randnum + 48);



                        } elseif ($randnum < 36) {



                                $newstring .= chr($randnum + 55);



                        } else {



                                $newstring .= chr($randnum + 61);



                        }



                }



                return $newstring;



        }







        function in_array_nocase($item, $array) {



                $item = &strtoupper($item);



                foreach($array as $element) {



                        if ($item == strtoupper($element)) {



                                return true; 



                        }



                }



                return false;



        } 



?>

upload.js脚本负责生成上传的页面:

var cp = new cpaint();
cp.set_transfer_mode('get');
cp.set_response_type('xml');
cp.set_debug(1);

function uploaderror(msg)
{
    alert(msg);
}
function showfile()
{
    var countfld=1;
    countfld=document.getElementById("countfld").value+countfld;
    fld=countfld.length;
    if(fld>14)
    {
    alert("Sorry, i can upload  max 15 files at once.");    
    return false;
    }
    else
    {
        document.getElementById("f"+fld).style.display="block";
        document.getElementById("countfld").value=countfld;
    }

   var file=document.getElementById("f"+fld).value;
   if(file=="")
   {
        msg="Please fill this field.";
        alert(msg);        
        document.getElementById("f"+fld).focus();
        return false;
    }    
}
function showfileux()
{
    var countfld=1;
    countfld=document.getElementById("countfldu").value+countfld;
    fld=countfld.length;
    if(fld>14)
    {
    alert("Sorry, i can upload  max 15 files at once.");    
    return false;
    }
    else
    {
        document.getElementById("u"+fld).style.display="block";
        document.getElementById("countfldu").value=countfld;
    }    
}

function showfileu()
{
    var countfld=1;
    countfld=document.getElementById("countfldu").value+countfld;
    fld=countfld.length;
    fldx=fld-1;
    fldxx=fld.value;
    if(fldxx=="")
{
msg="Email Address cannot be left empty.";
alert(msg);
document.getElementById("u"+fldxx).select();
  document.getElementById("u"+fldxx).focus();
   return false;
}
    if(fld>14)
    {
    alert("Sorry, i can upload  max 15 files at once.");    
    return false;
    }
    else
    {
        document.getElementById("u"+fld).style.display="block";
        document.getElementById("countfldu").value=countfld;
    }    
}

function uploadfile(id)
{
    if(document.getElementById(id).value==1)
    {
    document.getElementById("showurl").style.display="none";
    document.getElementById("showfl").style.display="block";        
    return true;
    }
    if(document.getElementById(id).value==2)
    {
    document.getElementById("showfl").style.display="none";
    document.getElementById("showurl").style.display="block";    
    return true;
    }
    document.getElementById("countfldu").value="0";
    document.getElementById("countfld").value="0";


}

function show_loading()
{
        document.getElementById('loading').style.display = "block";
        document.getElementById('newupload').submit;
        document.getElementById('submit').disabled = true;
       // return true;
}
function show_loading1()
{
        document.getElementById('loading1').style.display = "block";
        document.getElementById('newupload1').submit;
        document.getElementById('submit').disabled = true;
}

function upload(msg,newID,messages,FileName,FileFile,FileUrl,FileUrlLink,FiletnUrl,page_url,server_url,site_name,HotLink)
{
var html='<div id="wrapper"><div style="width:760px;"><center><FONT SIZE="4" COLOR="#00A4B7">Photo Links</FONT></h4><br></center><span class="body"><form name="uploadresults" action="uploademail.php" method="post">';
if(newID)
{    
    html=html+'<input type="hidden" name="idx[]" value="'+newID+'">';
}

if(msg)
    {
        var getmsg = msg.split("|");
        for(i=0;i<getmsg.length;i++)
            {
                if(getmsg[i] && getmsg[i]!="on")
                html=html+'<span style="font-weight: bold; color: red;">'+getmsg[i]+'</span><br>';
            }

    }    
html=html+'<br><center>';    
if(messages)
    {

        var getmessages = messages.split("|");
        for(i=0;i<getmessages.length;i++)
            {
                if(getmessages[i] && getmessages[i]!="on")                
                html=html+'<span style="font-weight: bold; color: red;">'+getmessages[i]+'</span>';
            }
            html=html+'</center>';    
    }    


if(FileName)
    {
        var getFileName = FileName.split("|");
        var getFileFile = FileFile.split("|");
        var getFileUrl = FileUrl.split("|");
        var getFileUrlLink = FileUrlLink.split("|");
        var getFiletnUrl = FiletnUrl.split("|");
        var getHotLink = HotLink.split("|");
                for(i=0;i<getFileName.length;i++)
                        {
                                if(getFileName[i] && getFileName[i]!="on")        {

html=html+'<center><br><a href="view2.php?filename='+getFileUrlLink[i]+'"><img src="'+getFileUrl[i]+'" style="max-width: 550px;"" /></a><br><br>';

html=html+'<strong>Link to add tags and delete the photo <br><div align="center"><textarea name="url1[]" cols="80" rows="1" READONLY onfocus="javascript: this.select()">'+server_url+'/view2.php?filename='+getFileUrlLink[i]+'

让我知道您认为导致此错误的原因,因为这是我需要解决的最后一步。

1 个答案:

答案 0 :(得分:1)

我在从大型数据库创建excel文件方面遇到了类似的问题。归结为PHP脚本超出了服务器设置的时间限制。有多种方法可以从内置的PHP函数中延迟/扩展它,可以使用部分或全部。我个人使用了AJAX的功能组合,允许它在背景中运行,然后重定向该页面。

以下是有关如何延迟/扩展它的文档:

http://php.net/manual/en/function.set-time-limit.php

以下是关于如何检查超时的文档:

http://php.net/manual/en/function.connection-timeout.php

如果你像我一样最终走上AJAX路线,我强烈建议你去jQuery路线而不是香草JS。