2013-05-17 23 views
0

我是一個在PHP的noob,我需要一些幫助。 我有一個上傳腳本,上傳東西到我的服務器。 所以我上傳的文件後,我得到一個消息your file was upload,我想在我的HTML上傳頁面(主頁)被張貼了這個消息和代碼是這樣的:回聲從腳本到html的東西

 if(empty($errors)===true){ 
      move_uploaded_file($file_tmp,"upload/".$file_name); 
      echo "Your file was upload!"; <- I want this line to be printed in other page // 
     }else{ 
      print_r($errors); 
     } 
} 

編輯: 我發現錯誤給我的腳本,如果你能幫助我與這兩請:

$file_name=$_FILES['file']['name']; 
$file_tmp =$_FILES['file']['tmp_name']; 
$file_type=$_FILES['file']['type']; 
$file_ext=strtolower(end(explode('.',$_FILES['file']['name']))); 

$extensions = array("rar","zip","jpeg","jpg","png","gif"); 
if(in_array($file_ext,$extensions)=== false){ 
$error[]= "Extension not allowed, please choose a RAR or ZIP file or if you upload an image use JPEG, JPG, PNG or GIF format.</br> Thank you!"; 
} 

這是我的代碼,限制上傳文件,但它不會採取限制,可以ANY1告訴我爲什麼嗎?

希望我明白了。 謝謝!

+0

嘗試使用會話在html.first中的任何位置打印您的消息將該消息存儲在變量中,而不是在恰當的位置顯示。 –

回答

1

您可以通過以下方式修改腳本。

... if(empty($errors)===true){ 
      move_uploaded_file($file_tmp,"upload/".$file_name); 
      header("Location: otherpage.php?msg=success");    
     }else{ 

      header("Location: otherpage.php?msg=failure"); 
      print_r($errors); 
     } 
} ... 

在otherpage.php,

if(isset($_GET['msg']) && $_GET['msg'] == 'success') { 

     echo "File uploaded sucessfully"; 

    } 

編輯:

你已經寫的print_r($錯誤)。我假設$錯誤是一個數組。在這種情況下,您可以使用json_encode($ errors)將此變量傳遞給url,如下所示。

$err = json_encode($errors); 
$urlEncode = urlencode($err); 
//now pass this to the url like this 
header("Location: otherpage.php?msg=failure&err=".$urlEncode); 

//in otherpage.php, you have to decode it. 
if(isset($_GET['err'])) { 

    $errDecode = urldecode($_GET['err']); 
    $err = json_decode($errDecode); 
    print_r($err); //prints the error 
} 

擴展:

你的腳本似乎是正確的,你可以嘗試在下面的方式。

$extensions = array("rar","zip","jpeg","jpg","png","gif"); 
    if(!in_array($file_ext,$extensions)){ 
    $error[]= "your error statement"; 
} 
+0

謝謝Abhishek :) – Victor

+0

嘗試這兩種方式'。它不會拿我的例外。它仍然會上傳我給他的任何文件......並且非常感謝你提供的錯誤信息部分! :) – Victor

+0

太好了......對於擴展來說,首先嚐試回顯擴展名,即回顯變量$ file_ext。看看它是否正確,前面沒有點。 –

0

將您的回聲放在您希望顯示的頁面上,而不是在單獨的頁面上。或者發送回顯字符串作爲表單提交的一部分,但這可能不適合這種情況。但是,您可以設置應在下一頁打印的內容,但需要事先進行設置。