基本上,我使用的是<form action=MyPhp.php>
將客戶端上的文件上傳到我的服務器。當上傳成功(或不成功)時,我會向客戶端發送消息以顯示結果(成功或失敗)。在由form =「action」調用的警報框中顯示PHP函數的輸出
這裏有個訣竅:我不知道如何捕捉那條消息。如果只是javascript/php我會使用XMLHttpRequest(),但我認爲它可以更容易。我已經檢查過參數「onsubmit =」和「target =」,但我不知道如何訪問消息。我的目標是將該消息顯示爲javascript:alert(「來自php的消息」)。任何建議,將不勝感激。這裏是我的代碼:
HTML
<form action="Upload.php" method="post" enctype="multipart/form-data">
<div class="custom-button-style browse-button-style" onclick="Browse()">Browse part</div>
<input class="toHide" type="file" id="fileToUpload" name="fileToUpload" data-role="none"/>
<input class="toHide" type="submit" value="Upload Part" id="submit" name="submit" data-role="none"/>
</form>
PHP
<?php
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($imageFileType != "zip")
{
echo "The file selected doesn't have the right extension. Please select a .zip file.";
$uploadOk = 0;
}
if($uploadOk == 1)
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
$zip = new ZipArchive();
$res = $zip->open($target_file);
if($res == TRUE)
{
$zip->extractTo($target_dir);
$zip->close();
unlink($target_file);
echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded.";
}
}
else
{
echo "Sorry, there was an error uploading your file.";
}
}
else
{
echo "Sorry, your file was not uploaded.";
}
?>
其實你可以'回聲「「;'如果你不想創建一個Ajax請求。 –