2016-01-20 73 views
1

我在項目文件夾中上傳文件時遇到了問題。我正在解釋我的代碼如下。使用PHP無法上傳文件夾內的文件

$imageName=generateRandomNumber().'_'.$_FILES["uploadme"]["name"]; 
$target_dir = "upload1/"; 
    $target_file = $target_dir . basename($imageName); 
    $uploadOk = 1; 
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
    $check = getimagesize($_FILES["uploadme"]["tmp_name"]); 
    if($check !== false) { 
     echo "File is an image - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } else { 
     echo "File is not an image."; 
     $uploadOk = 0; 
    } 
     if (file_exists($target_file)) { 
     echo "Sorry, file already exists."; 
     $uploadOk = 0; 
    } 
    // Check file size 
    if ($_FILES["uploadme"]["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["uploadme"]["tmp_name"], $target_file)) { 
     echo "The file ". basename($_FILES["uploadme"]["name"]). " has been uploaded."; 
    } else { 
     echo "Sorry, there was an error uploading your file."; 
    } 
    } 

這裏我得到的圖像,並添加它一個隨機數,但上傳圖片時不能在upload1 folder.Here內保存我收到Sorry, there was an error uploading your file message.Please幫我解決這個問題。

+1

檢查文件夾的權限。 – urfusion

+0

確保您的文件夾路徑從腳本文件的位置正確 –

+0

您使用的是Linux操作系統嗎?如果是這樣,則是文件夾權限問題。在終端 –

回答

0

右鍵單擊該文件夾並選擇權限選項卡,然後通過允許從文件夾創建和刪除文件來授予文件夾權限。