2015-10-13 175 views
0

我在關注本教程:http://www.w3schools.com/php/php_file_upload.asp。我已經複製並粘貼了PHP和HTML的代碼。在一個文件夾中,我有upload.html,php.ini(與file_uploads = On),upload.php和一個名爲uploads的文件夾,我認爲所有上傳的文件都應該進入。調試上傳圖片文件到一個文件夾

而當我按下提交,一切似乎工作,即成功消息被呼應,"The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."。但是,沒有文件似乎出現在uploads文件夾中。

此外,當我嘗試上傳相同的文件時,我收到錯誤消息echo "Sorry, file already exists.";,這很奇怪,因爲我沒有在我的uploads文件夾中看到該文件。

我目前使用FireFTP來存儲我所有的代碼和我上傳的文件。

這裏是我的upload.html代碼:

<!DOCTYPE html> 

<html> 
<body> 

<form action="upload.php" method="post" enctype="multipart/form-data"> 
    Select image to upload: 
    <input type="file" name="fileToUpload" id="fileToUpload"> 
    <input type="submit" value="Upload Image" name="submit"> 
</form> 

</body> 
</html> 

這裏是我的upload.php

<?php 
$target_dir = "uploads/"; 
$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 "File is an image - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } else { 
     echo "File is not an image."; 
     $uploadOk = 0; 
    } 
} 
// Check if file already exists 
if (file_exists($target_file)) { 
    echo "Sorry, file already exists."; 
    $uploadOk = 0; 
} 
// Check file size 
if ($_FILES["fileToUpload"]["size"] > 500000) { 
    echo "Sorry, your file is too large."; 
    $uploadOk = 0; 
} 
// Allow certain file formats 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
&& $imageFileType != "gif") { 
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
    $uploadOk = 0; 
} 
// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
    echo "Sorry, your file was not uploaded."; 
// if everything is ok, try to upload file 
} else { 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
     echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
    } else { 
     echo "Sorry, there was an error uploading your file."; 
    } 
} 
?> 

此外,有人告訴我,PHP是一個服務器端語言。這實際上意味着什麼?我試圖在網上閱讀一些東西,但它充滿了這麼多行話。這是否意味着我必須使用像FireFTP這樣的語言才能夠使用像PHP這樣的語言?

而我不能只使用chrome打開我的文本文件,並且能夠使用PHP?就像,我右鍵單擊包含我的代碼的文本文件,然後「使用chrome打開」?這是否也與網絡託管有關?

+0

這不是在直播服務器的工作? –

+0

我其實並沒有購買域名,沒有。但是,我使用webhosting和FireFTP來運行此代碼。 –

+0

檢查文件滲透您的網站 –

回答

0

您需要設置uploads文件夾權限讀取,寫入和刪除。

+0

謝謝你的迴應!但是,我該如何做到這一點? –

+0

此外,它開始工作後,我刪除並重新創建文件夾... –

+0

打開您的託管服務器的c面板,並找到此文件夾並設置權限。 –

1

試試這個代碼

$file_exts = array("jpg", "bmp", "jpeg", "gif", "png"); 
     $file_exts = array("jpg", "bmp", "jpeg", "gif", "png"); 
     $upload_exts = end(explode(".", $_FILES["fileToUpload"]["name"])); 
        if ((($_FILES["fileToUpload"]["type"] == "image/gif") 
        || ($_FILES["fileToUpload"]["type"] == "image/jpeg") 
        || ($_FILES["fileToUpload"]["type"] == "image/png") 
        || ($_FILES["fileToUpload"]["type"] == "image/pjpeg")) 
        && ($_FILES["fileToUpload"]["size"] < 2000000) 
        && in_array($upload_exts, $file_exts)) 
          { 
           if ($_FILES["fileToUpload"]["error"] > 0) 
           { 
            header("Location:dashboard.php?err=imgpro1"); 
            //echo "Return Code: " . $_FILES["fileToUpload"]["error"] . "<br>"; 
           } 
           else 
           { 
            $tempupname = time().$_FILES["fileToUpload"]["name"]; 
            $imgpathtostore="uploads/".$tempupname; 
            $imgpathtostoreDB="uploads/".$tempupname; 
            // Enter your path to upload file here 
            move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $imgpathtostore); 
            } 
            } 
            else 
              { 
               header("Location: index.php?err=imgpro"); 
              } 
     } 
相關問題