2014-05-08 131 views
0

只是隨着上傳文件,因爲它實際上是我以前從未做過的事情。我複製了一些here的代碼。基本文件上傳 - 500內部服務器錯誤

我從Namecheap使用cPanel託管,絕對沒有任何更改從默認配置。

我認爲最可能的問題是非常基本的,我還沒有激活。我的HTML看起來像這樣

<html> 
<body> 
<form action="upload_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> 
</body> 
</html> 

和我的PHP看起來像這樣

<?php 
//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'] 
} 

當我嘗試上傳圖片,我得到一個500內部服務器錯誤,當我點擊提交。

我錯過了什麼?

感謝

+0

你檢查了你的網絡服務器錯誤日誌?如果您啓用了錯誤日誌記錄(應該是默認情況下),它將包含錯誤信息,文件和行號,其失敗 – andersevenrud

回答

2

獲得底部去掉的東西:

<?php 
//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']; 
    } 

} 

不知道那是什麼東西了......另外,嘗試檢查您的cPanel Namecheap php.ini中看到什麼最大上傳大小是讓你的用戶得到你的錯誤,而不是一個PHP錯誤或500.

+0

謝謝,這似乎工作。唯一的問題是我無法弄清楚它放置圖像的位置。 從代碼看起來他們會在/ uploads /相對於upload_file.php,但沒有任何內容出現,儘管權限是正確的。 – Sam

+0

@Sam如何在move_uploaded_file上添加if或die錯誤? – Ruby