dbep
dbep

Reputation: 757

Image upload failing using PHP - move_uploaded_file

I have been searching stack overflow and other tutorials for how to upload files using php. The below script works until it gets to the if statement with a condition of move_uploaded_file($file_temp, $file_destination) at which point it echoes "file not uploaded".

The if statement containing the chmod function is my attempt to change the file permissions to 0777 (which I'm pretty sure gives read and write access to everyone) on the upload folder since that has been the suggestions of many related stack overflow answers. I don't think the problem is file permissions though at this point.

So basically I have no idea what is wrong with this. Help is appreciated :)

<form action="upload.php" method="post" enctype="multipart/form-data">
  Your Photo: <input type="file" name="image" />
  <input type="submit" name="submit" value="Submit" />
</form>

<?php
 print_r($_FILES);

 // file properties
 $file_name = $_FILES['image']['name'];
 $file_tmp_name = $_FILES['image']['tmp_name'];
 $file_size = $_FILES['image']['size'];
 $file_error = $_FILES['image']['error'];

 // get the extension of the file
 $file_ext = explode('.', $file_name);
 $file_ext = strtolower(end($file_ext));

 var_dump(file_exists('uploads'));

 // this is false
 if(chmod('uploads', 0777) ){
   echo " booooh";
  }
  else{
    echo "naaah";
   }

 $file_name_new = uniqid('', true) . '.' . $file_ext;
 echo "<br>";
 echo $file_destination = '/uploads/' . $file_name;        

 // this is false too
 if(move_uploaded_file($file_temp, $file_destination)) {
   echo "<br>$file_destination<br>";
   echo "hello world<br>";
 }
 else {
   echo "<br>file not uploaded<br>";
 }

 ?>

Upvotes: 0

Views: 516

Answers (1)

fusion3k
fusion3k

Reputation: 11689

You write:

move_uploaded_file( $file_temp, $file_destination );

$file_temp is not defined in your script. The original filePath is $file_tmp_name.

move_uploaded_file( $file_tmp_name, $file_destination );

Also note that you never use $file_name_new.

Additional note: the destination path must be absolute file path: /uploads/ directory must be at the top level of your directory tree, not under Document Root.

Upvotes: 2

Related Questions