# Solved: PHP $_FILES - file upload problems



## TheRobatron (Oct 25, 2007)

I'm writing my own custom content management system for one of my sites, which includes an upload page to add images to the gallery. I tried using a number of scripts I found on the internet, but they don't seem to work. The latest one I found was:


```
$uploaddir = '../images/gallery/';
						$uploadfile = $uploaddir . basename($_FILES['galleryFile']['name']) . "png";						
						if (move_uploaded_file($_FILES['galleryFile']['tmp_name'], $uploadfile)) {
							echo "File is valid, and was successfully uploaded.\n";
						} else {
							echo "Error\n";
						}
```
Any advice on why this isn't working or another script would be greatly appreciated.


----------



## Big-K (Nov 22, 2003)

what DOES it do? does it tell you its valid and not upload or does it say 'Error'?


----------



## TheRobatron (Oct 25, 2007)

It drops through to else {} and displays 'error'.
Also the PHP version on the server is 4.3.11 if that helps.


----------



## MMJ (Oct 15, 2006)

echo $_FILES['galleryFile']['error']


----------



## Big-K (Nov 22, 2003)

something else i notice is this line

$uploadfile = $uploaddir . basename($_FILES['galleryFile']['name']) . "png";

if the 'name' attribute is, say 'john.png', then basename will keep it as 'john.png' since you didnt give the second argument. Since you have it tack on 'png'(either way it should have the preceding period), it will be named john.pngpng
I doubt this has to do with your problem, but just so you know


----------



## TheRobatron (Oct 25, 2007)

Thanks for you replies but no success so far 

echo $_FILES['galleryFile']['error']; didn't print anything.



Big-K said:


> if the 'name' attribute is, say 'john.png', then basename will keep it as 'john.png'


Thanks for that - I misunderstood the basename() suffix attribute.

Any other ideas?


----------



## TheRobatron (Oct 25, 2007)

I have finally got it working!  I read and re-read the PHP manual page on file uploads, until I realised that I had missed out the enctype='multipart/form-data' and then fixed a few spelling mistakes in my variables. It was a silly mistake  but I'm always learning .

Thanks for your replies.


----------

