2017-01-30 75 views
0

我試圖上傳和重命名的圖像文件的PHP,但他沒有將文件移動到我想要的文件夾中。有人可以告訴我什麼是錯的碼?我如何上傳文件到一個特定的文件夾使用php

 $filename = $_POST["avatar"]; 
    $expl = explode('.', $filename); 
    $file_basename = $user; // give new name 
    $file_ext = $expl[1]; // get file extention 
    $filesize = $_FILES["avatar"]["size"]; 
    $allowed_file_types = array('gif', 'jpg', 'pjpg', 'png'); 
    $target_dir = "images/avatars/"; 
    $newfilename = $file_basename . '.' . $file_ext; 
    $target_file = $target_dir . $newfilename; 

    if (in_array($file_ext, $allowed_file_types) && ($filesize < 2000000000)) { 
     if (file_exists($target_dir . $newfilename)) { 
      echo "You have already uploaded this file."; 
     } else { 
      move_uploaded_file($_FILES['avatar']['tmp_name'], $target_dir); 
      if (!move_uploaded_file($_FILES['avatar']['tmp_name'], $target_file)) { 
       echo "There was an error uploading the file, please try again!"; 
      } else { 
       $avaq = "UPDATE portfoliotext SET avatar = '$newfilename'"; 
       $resava = mysqli_query($conn, $avaq); 
       echo "File uploaded successfully."; 
      } 
     } 
    } elseif ($filesize > 2000000000) { 
     echo "The file you are trying to upload is too large."; 
    } else { 
     echo "Only these file typs are allowed for upload: " . implode(', ', $allowed_file_types); 
    } 
} else { 
    echo "Invalid file"; 
} 
+1

1:檢查你已經得到了'ENCTYPE = 「的multipart/form-data的」'屬性設置。 2:檢查目錄的所有權和權限(做**不** 777)2:驗證文件擴展名幾乎是毫無意義的,你的腳本會拒絕像* my.lovely.horse.jpg * – CD001

+0

這樣的圖像也許這樣做^^ – scoopzilla

回答

0

我收到了一些額外的右括號和額外的else條款的錯誤。

也許試試這個:

$filename = $_POST["avatar"]; 
$expl = explode('.', $filename); 
$file_basename = $user; // give new name 
$file_ext = $expl[1]; // get file extention 
$filesize = $_FILES["avatar"]["size"]; 
$allowed_file_types = array('gif', 'jpg', 'pjpg', 'png'); 
$target_dir = "images/avatars/"; 
$newfilename = $file_basename . '.' . $file_ext; 
$target_file = $target_dir . $newfilename; 

if (in_array($file_ext, $allowed_file_types) && ($filesize < 2000000000)) { 
    if (file_exists($target_dir . $newfilename)) { 
     echo "You have already uploaded this file."; 
    } else { 
     move_uploaded_file($_FILES['avatar']['tmp_name'], $target_dir); 
     if (!move_uploaded_file($_FILES['avatar']['tmp_name'], $target_file)) { 
      echo "There was an error uploading the file, please try again!"; 
     } else { 
      $avaq = "UPDATE portfoliotext SET avatar = '$newfilename'"; 
      $resava = mysqli_query($conn, $avaq); 
      echo "File uploaded successfully."; 
     } 
    } 
} elseif ($filesize > 2000000000) { 
    echo "The file you are trying to upload is too large."; 
} else { 
    echo "Only these file typs are allowed for upload: " . implode(', ', $allowed_file_types); 
} 
相關問題