我正在尝试创建一个上传页面,用户可以在其中将其个人资料图像上传到我的EC2实例。我在EC2上创建了一个名为profile-images
的目录,并且具有以下代码。当我上传文件时,我没有收到任何错误。但是,该文件未上载。我对profile-images
目录具有以下权限:drwxrwsrwx 2 ec2-user www 4096 Mar 23 19:11 profile-images
。任何帮助将不胜感激!
if(isset($_POST['submit'])){
$file = $_FILES['file_name'];
$fileName = $_FILES['file_name']['name'];
$fileTmpName = $_FILES['file_name']['tmp_name'];
$fileSize = $_FILES['file_name']['size'];
$fileError = $_FILES['file_name']['error'];
$fileType = $_FILES['file_name']['type'];
$fileExt = explode('.',$fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png', 'pdf');
if(in_array($fileActualExt, $allowed)){
if($fileError == 0){
if($fileSize < 1000000){
$fileNameNew = uniqid('',true).".".$fileActualExt;
$fileDestination ='http://server_path/profile-images/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
echo "success";
} else {
echo "Your file is too big";
}
} else {
echo "There was an error uploading your file";
}
} else {
echo "You can not upload files of this type";
};
}
答案 0 :(得分:1)
move_uploaded_file()
requires a relative path(请参见示例1),而不是绝对值:
(摘自评论)
$fileDestination ='../profile-images/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);