2012-11-02 11 views
10

Ich verwende Malsup's jQuery File Upload Progress Bar zur Visualisierung der Fortschrittsbalken beim Hochladen von Dateien auf den Server. Ich benutze es jetzt zur Zeit. Ich habe folgende Zweifel in diesem Plugin bekam:Upload fehlschlägt in PHP mit Malsup jQuery File Upload Fortschrittsbalken

1) Datei-Uploads, auch wenn der Fortschrittsbalken 100% nicht erreicht hat

2) Ist die Dateigröße mehr als 10 MB ist, lädt es bis zu 100% in der Fortschrittsbalken und Echos Upload fehlgeschlagen! Botschaft. Hier

ist der Code:

<!doctype html> 
<head> 
<title>Uploads</title> 
<style> 
body { padding: 30px } 
form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px } 

.progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; } 
.bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; } 
.percent { position:absolute; display:inline-block; top:3px; left:48%; } 
</style> 
</head> 
<body> 
    <h1>File Upload</h1> 
    <form action="file.php" method="post" enctype="multipart/form-data"> 
     <input type="file" name="myfile[]" multiple><br> 
     <input type="submit" value="Upload File to Server"> 
    </form> 

    <div class="progress"> 
     <div class="bar"></div > 
     <div class="percent">0%</div > 
    </div> 

    <div id="status"></div> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
<script src="http://malsup.github.com/jquery.form.js"></script> 
<script> 
(function() { 

var bar = $('.bar'); 
var percent = $('.percent'); 
var status = $('#status'); 

$('form').ajaxForm({ 
    beforeSend: function() { 
     status.empty(); 
     var percentVal = '0%'; 
     bar.width(percentVal) 
     percent.html(percentVal); 
    }, 
    uploadProgress: function(event, position, total, percentComplete) { 
     var percentVal = percentComplete + '%'; 
     bar.width(percentVal) 
     percent.html(percentVal); 
     //console.log(percentVal, position, total); 
    }, 
    complete: function(xhr) { 
     status.html(xhr.responseText); 
    } 
}); 

})();  
</script> 
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script> 
<script type="text/javascript"> 
_uacct = "UA-850242-2"; 
urchinTracker(); 
</script> 

Die Form Aktion -> file.php Code:

<?php 
$upload_directory = "../users/Files/"; 
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_directory . $_FILES['userfile']['name'])) { 
    print "<br><br>"; 
    print "<font color='black'>File Uploaded Successfully</font>"; 
    print "<br><br>"; 
    print "<font color='black'>Uploaded File is {$_FILES['userfile']['name']} and its size is {$_FILES['userfile']['size']} bytes </font>"; 
} else { 
    print "Upload failed!"; 
} 
?> 

Meine Servereinstellungen für Datei-Uploads:

post_max_size   1050M 1050M 

upload_max_filesize 1050M 1050M 

upload_tmp_dir   /tmp /tmp 

Einstellungen in PHP .ini:

max_execution_time = 30 ; Maximum execution time of each script, in seconds 

max_input_time = -1 

memory_limit = 2000M ; Maximum amount of memory a script may consume (8MB) 

Antwort

1

Fügen Sie in Ihrer PHP-Seite diese Zeile nach dem PHP-Tag hinzu.

<?php 
set_time_limit(0); 
+0

Wenn es tatsächlich wird immer auf den Punkt zu beschweren sich über die Upload-Fehler, ich bin nicht sicher, dass es ein Timeout ... – Telgin

+0

Nach 30 Sekunden, Das PHP-Skript wird gestoppt, auch wenn es nicht die komplette Datei hochgeladen hat und die Antwort an die Ajax-Funktion zurückgibt, die fehlgeschlagen ist. –

+0

@AbhishekSaha, danke für die Antwort. Ich habe dieses Tag hinzugefügt, auch wenn das Dateigrößenlimit mehr als 10 MB beträgt, zeigt es Upload fehlgeschlagen! Nachricht .. – highlander141

1

In php.ini gibt es eine Grenze max Größe und ich ersetzt die file.php dafür:

<?php 
set_time_limit(0); 
$allowedExts = array("jpg", "jpeg", "gif", "png", "rar", "zip", "tgz", "iso", "gz",   "img", "exe",); 
$extension = end(explode(".", $_FILES["file"]["name"])); 
if ((($_FILES["file"]["type"] == "image/gif") 
|| ($_FILES["file"]["type"] == "image/jpeg") 
|| ($_FILES["file"]["type"] == "application/rar") 
|| ($_FILES["file"]["type"] == "application/octet-stream") 
|| ($_FILES["file"]["type"] == "image/pjpeg")) 
&& ($_FILES["file"]["size"] < 200000000000000) 
&& in_array($extension, $allowedExts)) 
    { 
    if ($_FILES["file"]["error"] > 0) 
    { 
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; 
    } 
    else 
    { 
    echo "Upload: " . $_FILES["file"]["name"] . "<br />"; 
    echo "Type: " . $_FILES["file"]["type"] . "<br />"; 
    echo "Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br />"; 
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; 

    if (file_exists("/var/www/file/upload/" . $_FILES["file"]["name"])) 
     { 
     echo $_FILES["file"]["name"] . " already exists. "; 
     } 
    else 
     { 
     move_uploaded_file($_FILES["file"]["tmp_name"], 
     "/var/www/file/upload/" . $_FILES["file"]["name"]); 
     echo "Stored in: " . "/file/upload/" . $_FILES["file"]["name"]; 

    } 
    } 
    } 
else 
    { 
    echo "Invalid file"; 
    } 
?> 
1

Sie haben einen Fehler im Namen der Datei ... das heißt:

<input type="file" name="myfile[]" multiple><br> 

Während Sie darauf zugreifen in PHP-Code als $_FILES["file"]["type"]. Ändern Sie den Namen in file statt myfile[]. Wechseln Sie zu:

<input type="file" name="file" multiple><br> 

Es gibt noch einige weitere Fehler obwohl