Luke
Luke

Reputation: 527

Upload File PHP EC2 not working no errors

I am trying to create an upload page where users can upload their profile images to my EC2 instance. I created a directory called profile-images on EC2 and I have the following code. When I upload the file I receive no errors. However, the file is not being uploaded. I have these permissions on the profile-images directory: drwxrwsrwx 2 ec2-user www 4096 Mar 23 19:11 profile-images. Any help would be greatly appreciated!

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";
 };
}

Upvotes: 0

Views: 357

Answers (1)

user10957435
user10957435

Reputation:

move_uploaded_file() requires a relative path (see example 1), not an absolute one:

(taken from the comments)

$fileDestination ='../profile-images/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);

Upvotes: 1

Related Questions