PHP File Upload Script

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
KostaKondra
Forum Newbie
Posts: 16
Joined: Thu Nov 19, 2009 8:26 am

PHP File Upload Script

Post by KostaKondra »

I have been pulling my hair out for the last 2 days trying to get this simple script to work:

uploadform.php:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
uploader.php

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$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!";
}
?>
</body>
</html>
Very simple code right?

So here's the question? Why doesn't it work!!!

Seriously. This is what bugs me. Not that it doesn't work, but that it works once... randomly. And then never works again. The code hasn't been changed but it has worked once (or twice). ANd then it just stops working.

Why???

Also what does <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
with the value, is that in bytes or kylobyes?

Regards
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP File Upload Script

Post by requinix »

Take another look at the manual page explaining how to do file uploads. Pay special attention to what it says about errors.

And if you still have problems, explain what "does not work" means.
KostaKondra
Forum Newbie
Posts: 16
Joined: Thu Nov 19, 2009 8:26 am

Re: PHP File Upload Script

Post by KostaKondra »

Where is my php.ini file located and even if I do find it where is the php.ini file located on my server? Should I call them and find out? By the way thank you for that information.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP File Upload Script

Post by requinix »

If you use phpinfo() it'll tell you where the php.ini file (it parsed) is located. You can also use ini_get() to get specific values in your code.
If someone is hosting your site (like shared hosting) then they probably provide a way to check php.ini values. For cPanel, there's an app for that (r)(tm).

The short version of what you should check first:
$_FILES[...]["error"] will contain an error code. 0 (all good) and 4 (no file was uploaded) are okay - anything else means a problem. Check that the file was uploaded successfully before trying to deal with it.


Oh, yeah. The file size is measured in bytes. Which, remember, is not 1000 times smaller than a kilobyte.
KostaKondra
Forum Newbie
Posts: 16
Joined: Thu Nov 19, 2009 8:26 am

Re: PHP File Upload Script

Post by KostaKondra »

Oh, yeah. The file size is measured in bytes.
tasairis wrote:Which, remember, is not 1000 times smaller than a kilobyte.
What do you mean not 1000 times smaller than a kilobye. Isn't that exactly what it is? 1000 times smaller than a kilobyte?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP File Upload Script

Post by requinix »

No...

A byte is 1024 times smaller than a kilobyte, which is 1024 times smaller than a megabyte, which is 1024 times smaller than a gigabyte, and so on.
That means if you use a value of 10,000,000 you are not giving a value of 10MB but something more like 9.5MB. 10MB is 10,485,760 bytes.
Post Reply