2011-04-16 106 views
1

我有一個簡單的文件上傳,這感謝stackoverflow現在完全工作,但是當我將PHP代碼複製到我的主佈局,一旦初始化上傳文件,但它是錯誤的格式或大小,它迴應錯誤,它打破了它下面的HTML。我認爲它與「退出」有關。每個回聲之後?但可能是錯誤的。PHP代碼打破HTML佈局

<?php 
if($_POST['upload']) { 
if($_FILES['image']['name'] == "") 
{ 
    #there's no file name return an error 
    echo "<br/><b>Please select a file to upload!\n</b>"; 
    exit; 
} 
#we have a filename, continue 

#directory to upload to 
$uploads = '/home/habbonow/public_html/other/quacked/photos'; 
$usruploads = 'photos'; 

#allowed file types 
$type_array = array(image_type_to_mime_type(IMAGETYPE_JPEG), image_type_to_mime_type(IMAGETYPE_GIF), image_type_to_mime_type(IMAGETYPE_PNG), 'image/pjpeg'); 

if(!in_array($_FILES['image']['type'], $type_array)) 
{ 
    #the type of the file is not in the list we want to allow 
    echo "<br/><b>That file type is not allowed!\n</b>"; 
    exit; 
} 

$max_filesize = 512000; 
$max_filesize_kb = ($max_filesize/1024); 

if($_FILES['image']['size'] > $max_filesize) 
{ 
    #file is larger than the value of $max_filesize return an error 
    echo "<br/><b>Your file is too large, files may be up to ".$max_filesize_kb."kb\n</b>"; 
    exit; 
} 

$imagesize = getimagesize($_FILES['image']['tmp_name']); 

#get width 
$imagewidth = $imagesize[0]; 
#get height 
$imageheight = $imagesize[1]; 

#allowed dimensions 
$maxwidth = 1024; 
$maxheight = 1024; 

if($imagewidth > $maxwidth || $imageheight > $maxheight) 
{ 
    #one or both of the image dimensions are larger than the allowed sizes return an error 
    echo "<br/><b>Your file is too large, files may be up to ".$maxwidth."px x ".$maxheight."px in size\n</b>"; 
    exit; 
} 

move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name']) or die ("Couldn't upload ".$_FILES['image']['name']." \n"); 

echo "<br/>The URL to your photo is <b>" . $usruploads . "/" . $_FILES['image']['name'] . "</b>. Please use this when defining the gallery photos"; 

} 

?> 

<form name="uploader" method="post" action="" enctype="multipart/form-data"> 
     <input type="file" name="image" style="width:300px;cursor:pointer" /> 
     <input type="submit" name="upload" value="Upload Image" /> 
</form> 
+0

'退出;'將停止腳本。 PHP代碼後的HTML不會被髮送到瀏覽器。 – Eliasdx 2011-04-16 12:40:53

+0

它給出了什麼錯誤?懇請提及所有 – diEcho 2011-04-16 12:42:34

+0

只適用於這裏完美的工作'move_uploaded_file()'警告後點擊上傳按鈕bcoz文件夾結構是不同於我的電腦 – diEcho 2011-04-16 12:44:18

回答

3

確實,當你撥打exit;這意味着「立即停止所有處理;該腳本完成」。它後面的任何內容(包括HTML)都不會被解釋。

一個更好的組織是使這個代碼功能,效果:

function uploadMyStuffPlease() { 

    if($_POST['upload']) { 
     if($_FILES['image']['name'] == "") 
     { 
      #there's no file name return an error 
      echo "<br/><b>Please select a file to upload!\n</b>"; 
      return; 
     } 
     #we have a filename, continue 

    // .... 

} 

現在,你可以簡單地調用uploadMyStuffPlease(),這將做盡可能多的處理是可以的,也許在提前返回發生錯誤。無論哪種方式,功能返回,所以您的腳本的其餘部分(包括該HTML)仍然可以解釋。

+0

正常工作,如: 'code' <腳本類型= 「文/ JavaScript的」> 功能uploadMyStuffPlease(){ 請選擇要上傳的文件!\ n「; return; }'code' 等....... 然後: 'code' <形式名稱= 「上載」 的方法= 「POST」 行動= 「」 ENCTYPE = 「多部分/格式數據」 的onsubmit = 「uploadMyStuffPlease()」>'code' – ben 2011-04-16 12:56:35

+0

這不是JavaScript。此處不涉及'

1

如果您致電exit;您的PHP腳本將不能輸出任何東西了。這就是佈局破裂的原因。

你也許應該儘量保持HTML部分出你的PHP代碼,特別是避免打開標記,這之後您沒有關閉(即div S或任何東西)。

也就是說,將所有內容放入一個不會在完成時退出腳本的函數是最安全的(參見其他文章)。

+0

+1爲「保持HTML的PHP​​」 – NoxArt 2011-04-16 13:18:21

1
if(isset($_POST['upload'])){ 

OR

if(!empty($_POST['upload'])){ 

,並刪除exit ...

+0

不工作,如果我刪除退出,它只是輸出所有回顯錯誤。謝謝你, – ben 2011-04-16 12:51:50

+0

@ben,你有替換我提供給你的第一行嗎?我想如果你離開'$ _POST ['upload']',它只會檢查布爾值。 – 2011-04-16 13:07:21

+0

是的,我改變了這一行,我試了他們兩個,都沒有退出嘗試;並沒有工作:) – ben 2011-04-16 13:09:54