PhP to upload a filer to a server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Freebenzine
Forum Newbie
Posts: 3
Joined: Fri Mar 02, 2012 6:56 am

PhP to upload a filer to a server

Post by Freebenzine »

Hi,
I am working on a script to allow anonymous users to upload files. I started with this simple code, which sadly I cannot make to work:

<?php
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['Upfile']['name']);

if (move_uploaded_file($_FILES['Upfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "File uploading failed.\n";
}
?>

The corresponding form is:

<form name="upload" action="Upload.php" method="POST" ENCTYPE="multipart/formdata">
Select the file to upload: <input type="file" name=Upfile">
<input type="submit" name="upload" value="upload">
</form>

I have not yet included the security codes, still it does not work. The message I receive is: "File uploading failed". The server does allow anonymous uploads. Do I need to edit the .htaccess file? Any tips will be greatly appreciated.

Is there some better guide than http://www.scanit.be...file-upload.pdf on the security of file uploads?

Cheers
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PhP to upload a filer to a server

Post by Celauran »

The code works fine on my machine. Have you checked that apache has write permission to your uploads directory?
Freebenzine
Forum Newbie
Posts: 3
Joined: Fri Mar 02, 2012 6:56 am

Re: PhP to upload a filer to a server

Post by Freebenzine »

HI,

Thanks for the reply. How can I check whether apache has write permission to my uploads directory?
Cheers
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PhP to upload a filer to a server

Post by Celauran »

Open up your FTP client and check the permissions on the directory. They'll most likely need to be 777 (drwxrwxrwx)
Freebenzine
Forum Newbie
Posts: 3
Joined: Fri Mar 02, 2012 6:56 am

Re: PhP to upload a filer to a server

Post by Freebenzine »

Thanks, it works now.
Is there some better guide than http://www.scanit.be...file-upload.pdf on the security of file uploads?
Cheers
Post Reply