Newbies and Uploaders

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
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Newbies and Uploaders

Post by Teonnyn »

I've been trying to get a script for an uploader working - though this isn't exactly a standard uploader. It receives it's information from Flash and then transfers it to the destination folder. However, nothing I've done as of yet has gotten this thing going. I've made sure the Actionscript has the correct settings. A problem occured when I set both script and destination folder to 777 - it gave me an 500 error and refused to let me see the PHP script period.

This is the code.. something in it doesn't really feel right, but I can't put my finger on the problem:

Code: Select all

 
<?PHP
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['Filedata']['name']);
 
if(move_uploaded_file($_FILES['Filedata']['tmp_name'], $target_path))
{
    echo "The file ". basename( $_FILES['Filedata']['name']). " has been uploaded";
}
else
{
    echo "There was an error uploading the file, please try again!";
}
?>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Newbies and Uploaders

Post by requinix »

Doesn't seem odd to me. It's just a very basic script.

Have you checked the server log as to what the 500 error was about?
(Your script probably doesn't need execute permission: 0644 is the typical set up. The directory, though, needs to be 0777.)

And your code should still check that the type of file being uploaded is allowed. If you're about to say "The Flash does" that's not good enough. I could bypass the Flash and upload, say, a PHP file. If stuff in the /uploads directory is publicly available (probably is) then my PHP script could run...
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: Newbies and Uploaders

Post by Teonnyn »

How do I check my server log? Told you I was a newbie. :lol:
I was told both upload script and directory need to be 777
And finally... how do I check to see if it's allowed? It should be, the server I am running from is a shared hosting and I'm in control of most of the functions on my account.

EDIT * 3: Tried once more to upload on purpose in order to generate the correct error messages, and it's generating two seperate:
[Sun Jan 11 03:03:13 2009] [error] [client 72.234.244.122] File does not exist: /home/****/public_html/500.shtml
[Sun Jan 11 03:03:13 2009] [error] [client 72.234.244.122] Premature end of script headers: /home/****/public_html/system/flash/upload.php
SoftException in Application.cpp:238: File "/home/geekcard/public_html/system/flash/upload.php" is writeable by group
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Newbies and Uploaders

Post by requinix »

Teonnyn wrote:I was told both upload script and directory need to be 777
Incorrect. The upload script should be 0644 and the directory as 0777. There are few circumstances where you should use different values.
Teonnyn wrote:And finally... how do I check to see if it's allowed? It should be, the server I am running from is a shared hosting and I'm in control of most of the functions on my account.
If what's allowed? I'm talking about the file being uploaded, as in: Are you allowing people to only upload images? Only documents and spreadsheets?
If there should be some restriction on what people can and cannot upload then the PHP script needs to check for it. At the very least the file uploaded must not be a PHP file, otherwise somebody could run their own PHP code on your server - which, needless to say, is very bad.
Teonnyn wrote:[Sun Jan 11 03:03:13 2009] [error] [client 72.234.244.122] File does not exist: /home/****/public_html/500.shtml
This means the server is configured to show a custom error response with the 500.shtml file. However it doesn't exist. Don't worry about this error.
Teonnyn wrote:[Sun Jan 11 03:03:13 2009] [error] [client 72.234.244.122] Premature end of script headers: /home/****/public_html/system/flash/upload.php
You should never see this error pop up while using PHP. It means something bad happened and PHP itself broke when trying to run your file. Good news is that odds are something outside of PHP made it happen - for example, antivirus software or security settings.
Teonnyn wrote:SoftException in Application.cpp:238: File "/home/geekcard/public_html/system/flash/upload.php" is writeable by group
In this case it was the latter of those two possibilities: there is a restriction on the machine that some types of files (including your upload.php) cannot be "writable by group".
Permissions look like 0ABC where A represents the owner ("User"), B represents the user group they belong to ("Group"), and C is for everybody else ("Other"). Right now, A=7 B=7 and C=7. Those numbers are the result of some addition: 4+2+1. 4 means that something can be read, 2 means it can be written to, and 1 means it can be executed.
For Group to write to something, B must be 2, 3 (1+2), 6 (2+4), or 7 (1+2+4).

The error says that the file cannot be writable by Group. If that's true it probably can't be writable by Other either. So in effect, that advice you received about giving the script 0777 permissions is what's causing the problem you have now.

Like I said: give your PHP files 0644 permissions and your directories 0777 permissions.
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: Newbies and Uploaders

Post by Teonnyn »

Okay, file is accepting the upload now but it doesn't seem to be appearing in the correct directory or anywhere on the server at all when I actually test it.
I inserted a simple test upload form that'll be removed later and thus far it's been giving me the standard error: "There was a problem uploading the file. Please try again!"
The file itself is set to 644 and the directory is 777. I did notice however, that an error log has been generated in the upload.php base directory and it's giving me this:
[10-Jan-2009 21:04:51] PHP Warning: move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: Unable to move '/tmp/phpTCZvnc' to 'public_html/system/portraits/1697.jpg' in /home/*******/public_html/system/flash/upload.php on line 5
Maybe /tmp/ should be set to 777 as well?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Newbies and Uploaders

Post by requinix »

/tmp gets special permissions, which it should already have since it's a very important directory.

I can't help but notice the path it's trying to file into, portraits/, doesn't have any mention of "uploads" like the code you first posted does. Can you shed any light onto that?
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: Newbies and Uploaders

Post by Teonnyn »

Changed code. In the true file it's /ports/ and the folder is /ports/. They match where they should.
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: Newbies and Uploaders

Post by Teonnyn »

Code: Select all

<?PHP
$target_path = "public_html/system/ports/";
$target_path = $target_path . basename( $_FILES['Filedata']['name']);
 
if(move_uploaded_file($_FILES['Filedata']['tmp_name'], $target_path))
{
 echo "The file ". basename( $_FILES['Filedata']['name']). " has been uploaded";
}
else
{
echo "There was an error uploading the file, please try again!";
}
?>
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: Newbies and Uploaders

Post by Teonnyn »

Okay, I figured out the problem. It's the directory route, but I'm unsure how to direct it correctly.
It only seems to like it if I try to upload directly into the Flash/PHP files directory.
Post Reply