我是PHP新手,还在学习…今天我不明白为什么文件是从目标目录$target_dir上传的。我改变了$target_dir很多次,但我总是得到相同的结果。代码看起来很好…
任何想法?
谢谢. .
function avatarUpload(){
$target_dir = "../uploads/avatars/";
$target_file = 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["avatar"]))
{
$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))
{
do
{
$rand = rand(100,10000);
$target_file = $rand .= $target_file;
}
while( file_exists($target_file) );
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000000)
{
//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;
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)== true)
{
// echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
return $target_file;
} else
{
// echo "Sorry, there was an error uploading your file.";
return $target_file;
}
}
return $target_file;
}
忘记在文件名后面加上目标目录。
$target_file = $target_dir .basename($_FILES["fileToUpload"]["name"]);