2012-04-01 155 views
0
<? 
if(isset($_POST['submit'])){ 

$url = 'http://www.connectedcampus.org/ckeditor/images/uploads/'; 

if (($_FILES['upload'] == "none") OR (empty($_FILES['upload']['name']))) 
{ 
    $message = "No file uploaded."; 
} 
else if ($_FILES['upload']["size"] == 0) 
{ 
    $message = "The file is of zero length."; 
} 
else if (($_FILES['upload']["type"] != "image/pjpeg") AND ($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/png")) 
{ 
    $message = "The image must be in either JPG or PNG format. Please upload a JPG or PNG instead."; 
} 
else if($_FILES["file"]["size"] > 1000000){ 
    $message = "File size too big!"; 
} 
else if(file_exists($url.$_FILES["file"]["name"])) 
    { 
    $message=$_FILES["file"]["name"] . " already exists. "; 
    } 
else if (!is_uploaded_file($_FILES['upload']["tmp_name"])) 
{ 
    $message = "Invalid File!"; 
} 
else { 
    $message = "success!"; 
    $move = move_uploaded_file($_FILES['upload']['tmp_name'], $url.$_FILES['upload']['name']); 
    echo $_FILES['upload']['tmp_name']."<br>"; 
    echo $url.$_FILES['upload']['name']; 
    if(!$move) 
    { 
    $message = "Error moving uploaded file."; 
    } 
    $url = $url.$_FILES['upload']['name']; 
} 
echo $message; 
</script>"; 
} 
?> 

這版畫 「錯誤移動上傳的文件」 這意味着move_uploaded_file($ _ FILES [ '上傳'] [ 'tmp_name的值'],$網址。$ _ FILES [ '上傳'] ['名']) 返回false。但我肯定是500%,我已經正確創建了http://www.connectedcampus.org/ckeditor/images/uploads/文件夾....那麼爲什麼會出現錯誤?PHP圖片上傳奇怪的問題

好了,現在改爲:

$url = 'bloguploads/'; 

    if (($_FILES['upload'] == "none") OR (empty($_FILES['upload']['name']))) 
    { 
     $message = "No file uploaded."; 
    } 
    else if ($_FILES['upload']["size"] == 0) 
    { 
     $message = "The file is of zero length."; 
    } 
    else if (($_FILES['upload']["type"] != "image/pjpeg") AND ($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/png")) 
    { 
     $message = "The image must be in either JPG or PNG format. Please upload a JPG or PNG instead."; 
    } 
else if($_FILES["file"]["size"] > 1000000){ 
     $message = "File size too big!"; 
} 
else if(file_exists($url.$_FILES["file"]["name"])) 
    { 
     $message=$_FILES["file"]["name"] . " already exists. "; 
    } 
    else if (!is_uploaded_file($_FILES['upload']["tmp_name"])) 
    { 
     $message = "Invalid File!"; 
    } 
    else { 
     $message = ""; 
     $move = move_uploaded_file($_FILES['upload']['tmp_name'], $url.$_FILES['upload']['name']); 
     if(!$move) 
     { 
      $message = "Error moving uploaded file."; 
     } 
     $url = $url.$_FILES['upload']['name']; 
     } 
echo $message; 
</script>"; 
?> 

現在的問題是,它說: 「已經存在」。爲什麼?我可以通過ftp客戶端看到文件尚未上傳

+2

讀/寫權限可能? – hjpotter92 2012-04-01 08:00:09

+0

@ hjpotter92 chmod($ _ FILES ['upload'] ['tmp_name'],777); $ message =「success!」; $ move = move_uploaded_file($ _ FILES ['upload'] ['tmp_name'],$ url。$ _ FILES ['upload'] ['name']); &更改上傳文件夾的權限以及...沒有任何作品 – 2012-04-01 08:23:28

+0

它說「已存在」,而不是「Filename.jpg已存在」的事實應該是一個提示... – 2012-04-01 10:05:46

回答

1

move_uploaded_file適用於文件系統,不適用於網址。

,而不是$url

move_uploaded_file($_FILES['upload']['tmp_name'], $url.$_FILES['upload']['name']); 

使用文件系統路徑,即:

move_uploaded_file($_FILES['upload']['tmp_name'], "/home/www/uploads/".$_FILES['upload']['name']); 
+0

$ url ='/ ckeditor/images/uploads /'; 仍然顯示錯誤上傳文件! : -/ – 2012-04-01 08:22:56

+0

如果您嘗試使用相對路徑,請從頭開始刪除'/'。但更好的想法是花一些時間並瞭解絕對/相對路徑,當前工作目錄以及如何在PHP中處理它們。 – 2012-04-01 08:30:05

+0

請參閱上面問題中的編輯。我糾正了路徑。 現在很奇怪它說「已經存在」 – 2012-04-01 08:52:24