2017-02-04 22 views
2

所以我有一個問題,讓我的文件上傳到我想 它。它執行一切,但不會上傳文件。它只是 說:上傳一個文件在PHP不能工作

你的文件有問題。

這是我的代碼:感謝您的幫助!

P.S.我試圖上傳的文件是11kb大,所以尺寸不是 問題。此外,權限不是問題。

<p>The file must be an image and less than 2 MB.</p> 
<form action="filerupp.php" method="post" enctype="multipart/form-data"> 
    <p>Choose file:<br/> 
    <input type="file" name="fileToUpload" id="fileToUpload"><br/> 
    <input type="submit" value="Upload image" name="submit"></p> 
</form> 
<p>Files are <a href='pictures'>here</a>.</p> 

這裏是我的PHP代碼:

$target_dir = "pictures/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 

// Check if image file is a actual image or fake image 
if(isset($_POST["submit"])) { 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
    if($check !== false) { 
     echo "<p>The File is an image" . $check["mime"] . ".<br/>"; 
     $uploadOk = 1; 
    } else { 
     echo "<p>The File is not an image.<br/>"; 
     $uploadOk = 0; 
    } 
} 
// Check if file already exists 
if (file_exists($target_file)) { 
    echo "<p>Sorry, the file already exists.<br/>"; 
    $uploadOk = 0; 
} 
// Check file size 
if ($_FILES["fileToUpload"]["size"] > 500000) { 
    echo "Sorry, the file är för stor.<br/>"; 
    $uploadOk = 0; 
} 
// Allow certain file formats 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
&& $imageFileType != "gif") { 
    echo "Sorry, only JPG, JPEG, PNG & GIF.<br/>"; 
    $uploadOk = 0; 
} 
// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
    echo "Sorry, your file cannot be uploaded.</p>"; 

// if everything is ok, try to upload file 
} else { 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
     echo "<p>The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded and is <a href='pictures'>here</a>.</p>"; 
    } else { 
     echo "<p>Sorry, there was something wrong with your file.</p>"; 
    } 
} 
+0

檢查文件夾 – urfusion

+0

的權限http://php.net/manual/en/features.file-upload.post-method.php按照此鏈接或可能檢查您的文件夾權限上傳圖像的位置。如果沒有解決與我聯繫。 skype:harryr891 –

+0

這是在Linux還是Windows? – infinigrove

回答

0

這個腳本的文件大小上傳限制只有約一半magabyte所以likley圖像比大。即使是2兆字節也是一個非常小的限制,可能更好的是使其達到10兆字節。

0

所以我終於明白了這個問題,多虧了我看到的評論。感謝所有試圖幫助的人。我的代碼沒有錯,它是我自己的freakin文件夾的許可。我檢查了服務器的權限,他們沒事,但我沒有檢查我自己的文件夾-_-謝謝你,問題解決了!