2014-02-05 44 views
-1

我上傳的形式是爲什麼我的上傳腳本不能工作?沒有錯誤

<form action="accept-file.php" method="post" enctype="multipart/form-data"> 
Your Photo: <input type="file" name="photo" size="25" /> 
<input type="submit" name="submit" value="Submit" /> 
</form> 

而且accept-file.php

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 

//if they DID upload a file... 
if($_FILES['photo']['name']) 
{ 
    //if no errors... 
    if(!$_FILES['photo']['error']) 
    { 
     //now is the time to modify the future file name and validate the file 
     $new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file 
     if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB 
     { 
      $valid_file = false; 
      $message = 'Oops! Your file\'s size is to large.'; 
     } 

     //if the file has passed the test 
     if($valid_file) 
     { 
      //move it to where we want it to be 
      move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name); 
      $message = 'Congratulations! Your file was accepted.'; 
     } 
    } 
    //if there is an error... 
    else 
    { 
     //set that to be the returned message 
     $message = 'Ooops! Your upload triggered the following error: '.$_FILES['photo']['error']; 
    } 
} 

//you get the following information for each file: 
$_FILES['field_name']['name'] 
$_FILES['field_name']['size'] 
$_FILES['field_name']['type'] 
$_FILES['field_name']['tmp_name'] 

?> 

它是從表面上工作正常教程直接複製。正如你所看到的,我曾經在頂部強制過一些錯誤報告,但是當我提交表單時,我得到的只是一個空白屏幕(瀏覽器加載file-accept.php),上傳的文件沒有出現在uploads/(chmoded爲777)。

編輯: 現在我正這些錯誤:

陣列([照片] =>陣列([名稱] => k3Jb9gv.jpg [型] =>圖像/ JPEG [tmp_name的值] =>/tmp/phpzc4fLT [error] => 0 [size] => 384262))

注意:未定義的索引:field_name在..............的第38,39行,40,41

+0

您應該收到警告? – putvande

+0

你看過你的錯誤日誌只是爲了確定嗎? –

+0

你是否檢查過'$ message'的值是在什麼地方? – crush

回答

1

該行給出了問題:

$new_file_name = strtolower($_FILES['photo']['tmp_name']); 

你應該改變,要別的東西,如:

$new_file_name = strtolower($_FILES['photo']['name']); 

這是因爲否則你的文件名是你的文件(包括目錄)的整個臨時網址。這會給你一個警告,說你不允許在那裏上傳它。

此外,您需要將$valid_file設置爲true某處,可能在您檢查有效文件之前。

//if they DID upload a file... 
if($_FILES['photo']['name']) 
{ 
    print_r($_FILES); 
    //if no errors... 
    if(!$_FILES['photo']['error']) 
    { 
     $valid_file = true; 
     //now is the time to modify the future file name and validate the file 
     $new_file_name = strtolower($_FILES['photo']['name']); //rename file 
     if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB 
     { 
      $valid_file = false; 
      $message = 'Oops! Your file\'s size is to large.'; 
     } 

     //if the file has passed the test 
     if($valid_file) 
     { 
      //move it to where we want it to be 
      move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name); 
      $message = 'Congratulations! Your file was accepted.'; 
     } 
    } 
    //if there is an error... 
    else 
    { 
     //set that to be the returned message 
     $message = 'Ooops! Your upload triggered the following error: '.$_FILES['photo']['error']; 
    } 
} 
+0

仍然無法正常工作。 – James

+0

是的,在'move_uploaded_file($ _ FILES ['photo'] ['tmp_name'],'uploads /'.$ new_file_name)' – putvande

+0

我仍然只是得到一個空白的'accept-file.php'頁面。似乎我甚至不能通過故意拼錯函數等來強制錯誤 – James

相關問題