2011-12-31 10 views
0

以前のプロジェクトで使用した古いスクリプトを使用して画像をアップロードしています。プロジェクトは、HTMLとPHPをアップロードするだけでを扱いやすくなりましたそのように! :)PHP&Jquery Image Upload - ファイルの問題なし

私はJqueryハンドラとAjaxハンドラの新機能を使用しています。ハンドラの使用率が高い現在のプロジェクトでは、ページ全体をリフレッシュせずにアップロードする必要があります。

マイスクリプト:

function postfile() { 
var filename = $("#image").val(); 
     $.ajax({ 
      type: "POST", 
      url: "pages/minutes1.php", 
      secureuri:false, 
     fileElementId:'image', 
      enctype: 'multipart/form-data', 
      data: "submit=yes&image=" + {file: filename}, 
      success: function(msg5){ 
       $("#upform").html(msg5); 
      } 
     }); 
} 

私のPHPとHTML:

<? 
/** 
* The following uploader was not originally suited for text based files<br> 
* Copied and Re-used from previous projects<br> 
*/ 
$submit = $_POST['submit']; 
if($submit=="yes") 
{ 
//define a maxim size for the uploaded images in Kb 
define ("MAX_SIZE","100"); 

//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension. 
function getExtension($str) { 
     $i = strrpos($str,"."); 
     if (!$i) { return ""; } 
     $l = strlen($str) - $i; 
     $ext = substr($str,$i+1,$l); 
     return $ext; 
} 

//This variable is used as a flag. The value is initialized with 0 (meaning no error found) 
//and it will be changed to 1 if an error occurs. 
//If the error occurs the file will not be uploaded. 
$errors=0; 
//checks if the form has been submitted 
    //reads the name of the file the user submitted for uploading 
    $image=$_FILES['image']['name']; 
if(!$image){ 
?> 
<table border="0" width="100%" cellspacing="0" cellpadding="0"> 
<tr> 
<td class="tabcol1 font1" style="text-align: center;"> 
There was no minutes file attached! 
</td></tr> 
<tr> 
<td class="tabcol4 font1" style="text-align: center;"> 
Please wait as you are directed back 
</td></tr> 
</table> 
<? 
}elseif($image) 
    { 
    //get the original name of the file from the clients machine 
     $filename = stripslashes($_FILES['image']['name']); 
    //get the extension of the file in a lower case format 
     $extension = getExtension($filename); 
     $extension = strtolower($extension); 
    //if it is not a known extension, we will suppose it is an error and will not upload the file, 
    //otherwise we will do more tests 
if (($extension != "doc") && ($extension != "txt") && ($extension != "pdf") && ($extension != "odt") && ($extension != "docx") && ($extension != "rtf")) 
     { 
?> 
<b>The file type uploaded is not supported!</b><br><br> 

<b>Supported formats:</b> doc, docx, pdf, odt, txt, rtf 
<? 
} 
     else 
     { 
//get the size of the image in bytes 
//$_FILES['image']['tmp_name'] is the temporary filename of the file 
//in which the uploaded file was stored on the server 
$size=filesize($_FILES['image']['tmp_name']); 

//compare the size with the maxim size we defined and print error if bigger 
if ($size > MAX_SIZE*1024) 
{ 
?> 
File size, too large! (if you need lieniency please contact webmaster) 
<? 
}else{ 
//we will give an unique name, for example the time in unix time format 
$exam = substr("$filename", 0, -2); 
function MakeRandom($minLength, $lastNr){ 

    $i = 0; 
    while ($i <= $minLength){ 
     $randomnr = ($i === $minLength) ? 1 : rand(0, 9); 
     $strNumber = (!$strNumber) ? $strNumber = $randomnr : $strNumber .= $randomnr; 
     $i++; 
    } 

    return $strNumber; 
} 

// show the number 
$num = MakeRandom(5, 1); 
$col = "$exam$num"; 
$image_name = $col.'.'.$extension; 
//the new name will be containing the full path where will be stored (images folder) 
$newname="minutes/".$image_name; 
//we verify if the image has been uploaded, and print error instead 
$copied = copy($_FILES['image']['tmp_name'], $newname); 
if (!$copied){ 
?> 
File uploaded and E-mail sent! 
<? 
} 
} 
} 
} 
}else{ 
?> 
<div id="upform"> 
<form method="post"> 
<table border="0" width="100%" cellspacing="0" cellpadding="0"> 
<tr> 
<td class="font1 tabcol1"> 
<!-- begin 2 column table --> 
<table border="0" width="100%" cellspacing="0" cellpadding="0"> 
<tr> 
<td class="font1"> 
&nbsp;<b>Choose a file to upload:<br> <input id="image" name="image" type="file" /><br /> 
<input type="button" id="submit" name="submit" value="submit" onclick="postfile()"> 
</td> 
<td class="font1"></td> 
</tr> 
</table> 
<!-- End 2 column table --> 
</td></tr> 
</table> 
</form> 
</div> 
<? 
} 
?> 

私は取得しています問題は、私は、ファイルを追加した場合でも、および提出押され、

それは私にイメージエラーを与えません。

私はスクリプトを間違って書いたと思いますが、構文エラーは見つかりませんでした。

すべてのサポートをいただければ幸いです!

は、事前にありがとう:)

答えて

2

$アヤックスは、ファイルをサポートしていない通常のフォームの提出がないのと同じようにアップロードします。この問題を回避するための一般的な提案は、アップロードスクリプトをiframeに埋め込んで、アップロード時にページをリロードしないようにすることです。

ここにはexampleがあります。