0

I've tried heaps of different things to get my php file upload to work, even trimming it right back to the absolute basics. I've tried different scripts and this and that but it's still giving me errors.

So I have this php script:

if (isset($_POST['submit'])) {
   $target_path = "uploads/";
   $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
   if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
      echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded";
   } else {
      echo "There was an error uploading the file, please try again!";
   }
}

So as you can see, absolute bare minimum. This is my simple HTML form:

<form action="" enctype="multipart/form-data" name="uploadform" method="post" >
   <input size="10" style="border:0px;" name="uploadedfile" type="file">
   <input type="submit" name="submit" class="buttonUpload" value="Upload">
</form>

Any file I try and upload gives me the error. This isn't the first piece of code I've tried. I tried the code example of w3schools, that again gave me the file upload error.

I've checked my php.ini and allow file uploads is on.

It's on my local machine so no need to chmod the upload folder.

Any suggestions on why this may be happening ?

Cheers!

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
Jamie Sutton
  • 163
  • 12
  • 1
    *"but it's still giving me errors"* - Being what exactly? – Funk Forty Niner Jan 06 '16 at 22:14
  • Sorry, error may be the wrong word. It's returning the "There was an error uploading the file, please try again!". There's no specific php error or entry in my error log. – Jamie Sutton Jan 06 '16 at 22:16
  • what about to display `error_reporting(E_ALL); ini_set('display_errors', 1);` – Funk Forty Niner Jan 06 '16 at 22:18
  • That doesn't display any errors. Still just echo's what is above. – Jamie Sutton Jan 06 '16 at 22:19
  • you on * NIX or Windows, other? xampp/wamp/mamp? if you made any changes to system files and didn't restart all services, that could be it. *"It's on my local machine so no need to chmod the upload folder."* - Hm, you sure about that? – Funk Forty Niner Jan 06 '16 at 22:22
  • I'm running UniServerZ on Windows. I've restarted all services and it's still producing the above. I don't understand what the issue is! – Jamie Sutton Jan 06 '16 at 22:24
  • The `move_uploaded_file` can fail for a lot of reasons. Are you sure the target directory is writable? You can use `is_writable()` to test. – Jeremy Harris Jan 06 '16 at 22:28
  • Yes, the target directory is writable. Just used is_writable() to test. – Jamie Sutton Jan 06 '16 at 22:33
  • Have you tried to `var_dump($_FILES); exit;` before this code and make sure it contains what you think it does? – Jeremy Harris Jan 06 '16 at 22:36
  • Can you elaborate on that please Jeremy? – Jamie Sutton Jan 06 '16 at 22:39
  • There are a lot of ways to check things and debug a problem. One of the simplest is to use the `var_dump()` function and pass it a variable to see the contents. You would usually follow it with an `exit;` statement so that the code doesn't continue execution. The result is the variable contents gets dumped to your screen. It lets you make sure a file is in the $_FILES array to rule out a form issue, or make sure paths are correct. You can dump your `$target_path` and make sure that it is correct also for example. – Jeremy Harris Jan 06 '16 at 22:41
  • Very interesting. It looks like it's returning an empty array `array(0) { }` – Jamie Sutton Jan 06 '16 at 22:43
  • have a look at these answers: http://stackoverflow.com/questions/3586919/why-would-files-be-empty-when-uploading-files-to-php – Jeff Jan 06 '16 at 23:40
  • Thanks Jeff. Have looked through all of those suggestions and all his suggestions I am doing. Cheers. – Jamie Sutton Jan 07 '16 at 00:58

0 Answers0